Linked list insert delete display
void insertNodeAtBeginning(int data) { struct node *newNode; newNode = (struct node*)malloc(sizeof(struct node)); if(newNode == NULL) { printf("Unable to allocate memory."); } else { newNode->data = data;
// Link data part newNode->next = head;
// Link address part head = newNode;
// Make newNode as first node printf("DATA INSERTED SUCCESSFULLY\n"); } } /* * Display entire list */ void displayList() { struct node *temp; /* * If the list is empty i.e. head = NULL */ if(head == NULL) { printf("List is empty."); } else { temp = head; while(temp != NULL) { printf("Data = %d\n", temp->data);
// Print data of current node temp = temp->next;
// Move to next node } } }
void deleteFirstNode() { struct node *toDelete; if(head == NULL) { printf("List is already empty."); } else { toDelete = head; head = head->next; printf("\nData deleted = %d\n", toDelete->data); /* Clears the memory occupied by first node*/ free(toDelete)
printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n"); } }