C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Deletion in singly linked list at beginningDeleting 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. ptr = head; 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. free(ptr) Algorithm
Write UNDERFLOW C function#include<stdio.h> #include<stdlib.h> void create(int); void begdelete(); struct node { int data; struct node *next; }; struct node *head; void main () { int choice,item; do { printf("\n1.Append List\n2.Delete node\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: begdelete(); 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 *)); if(ptr == NULL) { printf("\nOVERFLOW\n"); } else { ptr->data = item; ptr->next = head; head = ptr; printf("\nNode inserted\n"); } } void begdelete() { struct node *ptr; if(head == NULL) { printf("\nList is empty"); } else { ptr = head; head = ptr->next; free(ptr); printf("\n Node deleted from the begining ..."); } } Output 1.Append List 2.Delete node 3.Exit 4.Enter your choice?1 Enter the item 23 Node inserted 1.Append List 2.Delete node 3.Exit 4.Enter your choice?2 Node deleted from the begining ...
Next Topic#
|