Linked List
Linked List
o Linked List can be defined as collection of objects called nodes that are randomly
stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.
Insertion
Deletion
Display
Insertion
In a single linked list, the insertion operation can be performed in three ways. They are as follows...
1
2 **Algorithm to insert a new node at the beginning**
3 Step 1: IF AVAIL = NULL
Write OVERFLOW
4 Go to Step 7
5 [END OF IF]
6 Step 2: SET NEW_NODE = AVAIL
7 Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET NEW_NODE-> DATA = VAL
8 Step 5: SET NEW_NODE-> NEXT = START
9 Step 6: SET START = NEW_NODE
10 Step 7: EXIT
11
5.
Deleting the node after a given node in a linked list
1
2 **Algorithm to delete the node after a given node**
3 Step 1: IF START = NULL
4 Write UNDERFLOW
Go to Step 1
5
[END OF IF]
6 Step 2: SET PTR = START
7 Step 3: SET PREPTR = PTR
8 Step 4: Repeat Steps 5 and 6 while PREPTR->DATA != NUM
9 Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
10 [END OF LOOP]
11 Step 7: SET TEMP = PTR
12 Step 8: SET PREPTR->NEXT = PTR->NEXT
13 Step 9: FREE TEMP
14 Step 10: EXIT
15
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence. Therefore, in a doubly linked list,
a node consists of three parts: node data, pointer to the next node in sequence (next
pointer) , pointer to the previous node (previous pointer). A sample node in a doubly
linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data
part, is shown in the following image.
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. }
The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.
Example:
Here is an example of the circular doubly linked list.