Module 3 Part 1
Module 3 Part 1
Module 3 Part 1
Memory resizing is not possible. i.e. array size is fixed- it is a static data structure.
Array requires continuous memory locations to store data.
Wastage of memory
1. LINKED LIST
A linked list is an ordered collection of finite, homogeneous data elements called nodes
where the linear order is maintained by means of links or pointers.
A linked list is a dynamic data structure where the amount of memory required can be
varied during its use.
In the linked list, the adjacency between the elements is maintained by means of links or
pointers.
A link or pointer actually is the address (memory location) of the subsequent element.
An element in a linked list is a specially termed node, which can be viewed as shown in the
figure.
A node consists of two fields : DATA (to store the actual information) and LINK (to point
to the next node)
A linked list is called "linked" because each node in the series has a pointer that points to the
next node in the list.
Head: pointer to the first node
The last node points to NULL
Depending on the requirements the pointers are maintained, and accordingly the linked list
can be classified into three major groups:
1. Single linked list
2. Circular linked list
3. Double linked list.
In any single linked list, every "Node" contains two fields, data and link.
The data field is used to store actual value of that node and link field is used to store the
address of the next node in the sequence.
Each node contains only one link which points to the subsequent node in the list.
The header node points to the 1st node in the list
The link field of the last node contain NULL(ᶲ) value.
Here one can move from left to right only. So it is also called one-way list
Two ways:
1. Static representation using array
2. Dynamic representation using free pool storage
1. Static representation
Two arrays are maintained:
– One for data and other for links.
2. Dynamic representation
The efficient way of representing a linked list is using the free pool of storage.
There is a
– memory bank : Collection of free memory spaces &
– memory manager: a program
Whenever a node is required, the request is placed to the memory manager.
It will search the memory bank for the block. If found, it will be granted.
Garbage collector: Another program that returns the unused node to the memory bank.
Returning a node to memory bank
4. Merging
Two linked list L1 and L2.
Merge L2 after L1
1. Set ptr= head1
2. While(ptr->link!= NULL) do step 3 else goto step 4
3. ptr=ptr->link
4. ptr->link=head2
5. Return(head2)
6. Head=head1
7. Stop
Advantages:
1. Accessibility of a member node – here every member node is accessible from any
node by merely chaining through the list
eg: Finding of earlier occurrence or post occurrence of a data will be easy
2. Null link problem- Null value in next field may create problem during the
execution of the program if proper care is not taken
3. Some easy-to-implement operations - Operations like merging, splitting, deletion,
dispose of an entire list etc can be done easily with circular list
Disadvantages:
If not cared, system may get trap into in infinite loop
It occurs when we are unable to detect the end of the list while moving from one
node to the next
Solution: Special node can be maintained with data part as NULL and this node
does not contain any valid information. So its just a wastage of memory space