Linkedlist (Unit3) Ds
Linkedlist (Unit3) Ds
A singly linked list is a linear data structure in which the elements are not stored in contiguous
memory locations and each element is connected only to its next element using a pointer.
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.
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to expand the
size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory. Inserting any
element in the array needs shifting of all its predecessors.
One way chain or singly linked list can be traversed only in one direction. In other words,
we can say that each node contains only next pointer, therefore we can not traverse the
list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects are stored
in a linked list as shown in the figure.
In
the above figure, the arrow represents the links. The data part of every node
contains the marks obtained by the student in the different subject. The last node
in the list is identified by the null pointer which is present in the address part of the
last node. We can have as many elements we require, in the data part of the list.
Operations on Singly Linked List
There are various operations which can be performed on singly linked list. A list of all such
operations is given below.
Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
Insertion
The insertion into a singly linked list can be performed at different positions. Based on
the position of the new node being inserted, the insertion is categorized into the following
categories.
It involves inserting any element at the front of the list. We just need to a few link
adjustments to make the new node as the head of the list.
o Allocate the space for the new node and store data into the data part of the node. This
will be done by the following statements.
1. ptr->next = head;
o At the last, we need to make the new node as the first node of the list this will be done by
using the following statement.
1. head = ptr;
Algorithm
o Step 1: IF PTR = NULL
WriteOVERFLOW
GotoStep7
[END OF IF]
1. ptr->data = item;
2. ptr -> next = NULL;
o Since, ptr is the only node that will be inserted in the list hence, we need to make this
node pointed by the head pointer of the list. This will be done by using the following
Statements.
1. Head = ptr
In the second case,
o The condition Head = NULL would fail, since Head is not null. Now, we need to declare a
temporary pointer temp in order to traverse through the list. temp is made to point the
first node of the list.
1. Temp = head
o Then, traverse through the entire linked list using the statements:
1. temp = head;
2. while (temp -> next != NULL)
3. {
4. temp = temp -> next;
5. }
6. temp->next = ptr;
7. ptr->next = NULL;
Algorithm
o Step1: IFPTR=NULLWriteOVERFLOW
GotoStep1
[END OF IF]
o Step 2: SET NEW_NODE = PTR
o Step 3: SET PTR = PTR - > NEXT
o Step 4: SET NEW_NODE - > DATA = VAL
o Step 5: SET NEW_NODE - > NEXT = NULL
o Step 6: SET PTR = HEAD
o Step 7: Repeat Step 8 while PTR - > NEXT != NULL
o Step8: SETPTR=PTR->NEXT
[END OF LOOP]
o Step 9: SET PTR - > NEXT = NEW_NODE
o Step 10: EXIT
1. emp=head;
2. for(i=0;i<loc;i++)
3. {
4. temp = temp->next;
5. if(temp == NULL)
6. {
7. return;
8. }
9.
10. }
o Allocate the space for the new node and add the item to the data part of it. This will be
done by using the following statements.
now, we just need to make the next part of the temp, point to the new node ptr. This will
insert the new node ptr, at the specified position.
WRITE OVERFLOW
GOTO STEP 12
END OF IF
WRITE"DESIREDNODENOTPRESENT"
GOTOSTEP12
ENDOFIF
END OF LOOP
Deleting a node from the beginning of the list is the simplest operation of all. It just need
a few adjustments in the node pointers. Since the first node of the list is to be deleted,
therefore, we just need to make the head, point to the next of the head. This will be done
by using the following statements.
1. ptr = head;
2. head = ptr->next;
Now, free the pointer ptr which was pointing to the head node of the list. This will be
done by using the following statement.
1. free(ptr)
Algorithm
o Step 1: IF HEAD = NULL
WriteUNDERFLOW
GotoStep5
[END OF IF]
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.
1. ptr = head
2. head = NULL
3. free(ptr)
In the second scenario,
The condition head → next = NULL would fail and therefore, we have to traverse the node
in order to reach the last node of the list.
For this purpose, just declare a temporary pointer temp and assign it to head of the list.
We also need to keep track of the second last node of the list. For this purpose, two
pointers ptr and ptr1 will be used where ptr will point to the last node and ptr1 will point
to the second last node of the list.
1. ptr = head;
2. while(ptr->next != NULL)
3. {
4. ptr1 = ptr;
5. ptr = ptr ->next;
6. }
Now, we just need to make the pointer ptr1 point to the NULL and the last node of the
list that is pointed by ptr will become free. It will be done by using the following
statements.
1. ptr1->next = NULL;
2. free(ptr);
Algorithm
o Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
[END OF LOOP]
A doubly linked list (DLL) is a special type of linked list in which each node contains a
pointer to the previous node as well as the next node of the linked list. Doubly Linked
List is a Data Structure, which is a variation of the Linked List, in which the
transversal is possible in both the directions, forward and backward easily as
compared to the Singly Linked List, or which is also simply called as a Linked List.
So if a Linked List ⇒ A → B →. C
Then a Doubly Linked List ⇒ A ⇆ B ⇆ C
If you can recall how the Linked List was represented using 2 parts: Value and the next pointer.
The Doubly Linked List has 3 parts: Value, Next pointer, and the Previous pointer.
The Previous pointer is the fundamental difference between the Doubly Linked List and the
Linked List, which enables one to transverse back also in the Doubly Linked List.
As per the above illustration, following are the important points to be considered.
• Each Doubly Linked List Element contains pointers to next and Previous Elements and
the data field.
• The First Element will have its Previous pointer as Null, to mark the start of the List.
• The Last Element will have its Next pointer as Null, to mark the end of the List
Basic Operations
Following are the basic operations supported by a list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Insert Last − Adds an element at the end of the list.
• Delete Last − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward manner.
• Display backward − Displays the complete list in a backward manner.
Algorithm
1. START
2. Create a new node with three variables: prev, data, next.
3. Store the new data in the data variable
4. If the list is empty, make the new node as head.
5. Otherwise, link the address of the existing first node to the next variable of the new node,
and assign null to the prev variable.
6. Point the head to the new node.
7. END
Deletion at the Beginning
This deletion operation deletes the existing first nodes in the doubly linked list. The head
is shifted to the next node and the link is removed.
Algorithm
1. START
2. Check the status of the doubly linked list
3. If the list is empty, deletion is not possible
4. If the list is not empty, the head pointer is shifted to the next node.
5. END
Insertion at the End
In this insertion operation, the new input node is added at the end of the doubly linked
list; if the list is not empty. The head will be pointed to the new node, if the list is empty.
Algorithm
1. START
2. If the list is empty, add the node to the list and point the head to it.
3. If the list is not empty, find the last node of the list.
4. Create a link between the last node in the list and the new node.
5. The new node will point to NULL as it is the new last node.
6. END
• It is used by web browsers for backward and forward navigation of web pages
• LRU ( Least Recently Used ) / MRU ( Most Recently Used ) Cache are constructed using
Doubly Linked Lists.
• Used by various applications to maintain undo and redo functionalities.
• In Operating Systems, a doubly linked list is maintained by thread scheduler to keep
track of processes that are being executed at that time.0
Circular linked list
In a circular Singly linked list, the last node of the list contains a pointer to the
first node of the list. We can have circular singly linked list as well as circular
doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started.
The circular singly liked list has no beginning and no ending. There is no null value present
in the next part of any of the nodes.
The circular linked list is a linked list where all nodes are connected to form a
circle. In a circular linked list, the first node and the last node are connected to
each other which forms a circle. There is no NULL at the end.
There are generally two types of circular linked lists:
• Circular singly linked list: In a circular Singly linked list, the last node
of the list contains a pointer to the first node of the list. We traverse
the circular singly linked list until we reach the same node where we
started. The circular singly linked list has no beginning or end. No null
value is present in the next part of any of the nodes.
int data;
};
However, due to the fact that we are considering circular linked list in the memory
therefore the last node of the list contains the address of the first node of the list.
Insertion into circular singly linked list at
beginning
There are two scenario in which a node can be inserted in circular singly linked list at
beginning. Either the node will be inserted in an empty list or the node is to be inserted
in an already filled list.
Firstly, allocate the memory space for the new node by using the malloc method of C
language.
In the first scenario, the condition head == NULL will be true. Since, the list in which, we
are inserting the node is a circular singly linked list, therefore the only node of the list
(which is just inserted into the list) will point to itself only. We also need to make the head
pointer point to this node. This will be done by using the following statements.
1. if(head == NULL)
2. {
3. head = ptr;
4. ptr -> next = head;
5. }
In the second scenario, the condition head == NULL will become false which means that
the list contains at least one node. In this case, we need to traverse the list in order to
reach the last node of the list. This will be done by using the following statement.
1. temp = head;
2. while(temp->next != head)
3. temp = temp->next;
At the end of the loop, the pointer temp would point to the last node of the list. Since, in
a circular singly linked list, the last node of the list contains a pointer to the first node of
the list. Therefore, we need to make the next pointer of the last node point to the head
node of the list and the new node which is being inserted into the list will be the new
head node of the list therefore the next pointer of temp will point to the new node ptr.
the next pointer of temp will point to the existing head node of the list.
1. ptr->next = head;
Now, make the new node ptr, the new head node of the circular singly linked list.
1. head = ptr;
in this way, the node ptr has been inserted into the circular singly linked list at beginning.
Algorithm
o Step 1: IF PTR = NULL
WriteOVERFLOW
GotoStep11
[END OF IF]
[END OF LOOP]
• Firstly, allocate the memory space for the new node by using the malloc method of C language.
In the first scenario, the condition head == NULL will be true. Since, the list in which, we are
inserting the node is a circular singly linked list, therefore the only node of the list (which is just
inserted into the list) will point to itself only. We also need to make the head pointer point to this
node. This will be done by using the following statements.
1. if(head == NULL)
2. {
3. head = ptr;
4. ptr -> next = head;
5. }
In the second scenario, the condition head == NULL will become false which means that the
list contains at least one node. In this case, we need to traverse the list in order to reach the last
node of the list. This will be done by using the following statement.
1. temp = head;
2. while(temp->next != head)
3. temp = temp->next;
At the end of the loop, the pointer temp would point to the last node of the list. Since, the new
node which is being inserted into the list will be the new last node of the list. Therefore the
existing last node i.e. temp must point to the new node ptr. This is done by using the following
statement.
The new last node of the list i.e. ptr will point to the head node of the list.
In this way, a new node will be inserted in a circular singly linked list at the beginning.
Algorithm
o Step 1: IF PTR = NULL
WriteOVERFLOW
GotoStep1
[END OF IF]
[END OF LOOP]
There are three scenarios of deleting a node from circular singly linked list at beginning.
1. if(head == NULL)
2. {
3. printf("\nUNDERFLOW");
4. return;
5. }
1. ptr = head;
2. while(ptr -> next != head)
3. ptr = ptr -> next;
At the end of the loop, the pointer ptr point to the last node of the list. Since, the last
node of the list points to the head node of the list. Therefore this will be changed as now,
the last node of the list will point to the next of the head node.
1. ptr->next = head->next;
Now, free the head pointer by using the free() method in C language.
1. free(head);
Make the node pointed by the next of the last node, the new head of the list.
1. head = ptr->next;
In this way, the node will be deleted from the circular singly linked list from the beginning.
Algorithm
o Step 1: IF HEAD = NULL
WriteUNDERFLOW
GotoStep8
[END OF IF]
o Step 2: SET PTR = HEAD
o Step 3: Repeat Step 4 while PTR → NEXT != HEAD
o Step 4: SET PTR = PTR → next
[END OF LOOP]
1. if(head->next == head)
2. {
3. head = NULL;
4. free(head);
5. }
1. ptr = head;
2. while(ptr ->next != head)
3. {
4. preptr=ptr;
5. ptr = ptr->next;
6. }
now, we need to make just one more pointer adjustment. We need to make the next
pointer of preptr point to the next of ptr (i.e. head) and then make pointer ptr free.
WriteUNDERFLOW
GotoStep8
[END OF IF]
[END OF LOOP]