Single Linked List Insert and Delete all nodes

#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void insert(struct node **head, int data){ struct node *temp=NULL; temp=(struct node *)malloc(sizeof(struct node)); temp->data=data; if(*head==NULL){ *head=temp; return; } temp->next=*head; *head=temp; } void display(struct node *head){ while (head!=NULL){ printf(" %d ",head->data); head=head->next; } } void deleteNode(struct node *head){ while (head!=NULL){ struct node *tHead=NULL; tHead=head; head=head->next; free(tHead); } } int main() { //code struct node *head=NULL; insert(&head,10); insert(&head,20); insert(&head,30); insert(&head,40); insert(&head,50); display(head); return 0; deleteNode(head); display(head); }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.