Javatpoint Logo
Javatpoint Logo

Deletion in Circular singly linked list at the end

There are three scenarios of deleting a node in circular singly linked list at the end.

Scenario 1 (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.

Scenario 2(the list contains single element)

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. This will be done by using the following statements.

Scenario 3(the list contains more than one element)

If the list contains more than one element, then in order to delete the last element, we need to reach the last node. We also need to keep track of the second last node of the list. For this purpose, the two pointers ptr and preptr are defined. The following sequence of code is used for this purpose.

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.

Algorithm

  • Step 1: IF HEAD = NULL
  •   Write UNDERFLOW
       Go to Step 8
      [END OF IF]

  • Step 2: SET PTR = HEAD
  • Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
  • Step 4: SET PREPTR = PTR
  • Step 5: SET PTR = PTR -> NEXT
  • [END OF LOOP]

  • Step 6: SET PREPTR -> NEXT = HEAD
  • Step 7: FREE PTR
  • Step 8: EXIT

Deletion in Circular singly linked list at the end

C Function

Output

1.Append List
2.Delete Node from end
3.Exit
4.Enter your choice?1

Enter the item
90

Node Inserted
1.Append List
2.Delete Node from end
3.Exit
4.Enter your choice?2

Node Deleted
	

Next TopicDoubly Linked List




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA