/* http://native-code.blogspot.com */ #include<stdio.h> #include<conio.h> #include<malloc.h> typedef struct link { int data; struct link *next; } node; node *ins_beg(node*, int); void display(node*); void main() { int x, ch; node *head=NULL; while(1) { printf("\n1-->Insert at first\n2-->Display\n3-->EXIT\n"); scanf("%d", &ch); switch(ch) { case 1: printf("Enter data of the node:\n"); scanf("%d", &x); head=ins_beg(head, x); break; case 2: display(head); printf("\n"); break; case 3: exit(0); default: printf("Wrong choice.\n"); } } getch(); } node *ins_beg(node *h, int x) { node *temp, *r=h; temp=(node*) malloc (sizeof(node)); temp->data=x; temp->next=NULL; if(h==NULL) { h=temp; } else { temp->next=h; h=temp; } return(h); } void display( node *h) { node *r=h; while(r!=NULL) { printf("%d-->", r->data); r=r->next; } printf("NULL"); } /* http://native-code.blogspot.com */
Home
» C
» data-structure
» single-linked-list
» How to insert a new node at the first node of a single link list?
Post a Comment