C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Deletion in circular singly linked list at beginningIn order to delete a node in circular singly linked list, we need to make a few pointer adjustments. There are three scenarios of deleting a node from circular singly linked list at beginning. 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. if(head == NULL) { printf("\nUNDERFLOW"); return; } Scenario 2: (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. This will be done by using the following statements. if(head->next == head) { head = NULL; free(head); } Scenario 3: (The list contains more than one node)If the list contains more than one node then, in that case, 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, 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. ptr->next = head->next; Now, free the head pointer by using the free() method in C language. free(head); Make the node pointed by the next of the last node, the new head of the list. head = ptr->next; In this way, the node will be deleted from the circular singly linked list from the beginning. Algorithm
Write UNDERFLOW [END OF LOOP] C Function#include<stdio.h> #include<stdlib.h> void create(int); void beg_delete(); struct node { int data; struct node *next; }; struct node *head; void main () { int choice,item; do { printf("1.Append List\n2.Delete Node from beginning\n3.Exit\n4.Enter your choice?"); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the item\n"); scanf("%d",&item); create(item); break; case 2: beg_delete(); break; case 3: exit(0); break; default: printf("\nPlease Enter valid choice\n"); } }while(choice != 3); } void create(int item) { struct node *ptr = (struct node *)malloc(sizeof(struct node)); struct node *temp; if(ptr == NULL) { printf("\nOVERFLOW"); } else { ptr -> data = item; if(head == NULL) { head = ptr; ptr -> next = head; } else { temp = head; while(temp->next != head) temp = temp->next; ptr->next = head; temp -> next = ptr; head = ptr; } printf("\nNode Inserted\n"); } } void beg_delete() { struct node *ptr; if(head == NULL) { printf("\nUNDERFLOW\n"); } else if(head->next == head) { head = NULL; free(head); printf("\nNode Deleted\n"); } else { ptr = head; while(ptr -> next != head) ptr = ptr -> next; ptr->next = head->next; free(head); head = ptr->next; printf("\nNode Deleted\n"); } } Output 1.Append List 2.Delete Node from beginning 3.Exit 4.Enter your choice?1 Enter the item 12 Node Inserted 1.Append List 2.Delete Node from beginning 3.Exit 4.Enter your choice?2 Node Deleted
Next TopicDoubly Linked List
|