Circular Linked List and DLL 16
Circular Linked List and DLL 16
Algorithm
Case 1: List is empty.
If the list is empty we will simply return.
Deletion in circular singly linked list at
beginning
• The list is Empty
– If the list is empty then the condition head ==
NULL will become true, in this case, we just need
to print underflow on the screen and make exit.
if(head == NULL)
{
printf("\nUNDERFLOW");
return;
}
• The list contains single node
If the list contains single node then, the
condition head → next == head will become true.
In this case, we need to delete the entire list and
make the head pointer free.
if(head->next == head)
{
head = NULL;
free(head);
}
• The list contains more than one node
• we need to traverse the list by using the
pointer ptr to reach the last node of the list.
This will be done by using the following
statements.
ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
At the end of the loop,
ptr->next = head->next;
free(head);
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
-1 indicates NULL
Insertion in doubly linked list at beginning
Algorithm :
Step 1: SET NEW_NODE = PTR
Step 2: SET NEW_NODE -> PREV = NULL
Step 3: SET NEW_NODE -> NEXT = START
Step 4: SET head -> PREV = NEW_NODE
Step 5: SET head = NEW_NODE
Step 6: EXIT
Insertion in doubly linked list at the end
Algorithm
Algorithm
Algorithm
STEP 1: IF HEAD = NULL
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR
STEP 6: EXIT
Deletion in doubly linked list at the end
ALGORITHM
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Algorithm
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]