/* 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_last(node*, int); void display(node*); void main() { int ch, d; node *head=NULL; while(1) { printf("/* http://native-code.blogspot.com */"); printf("\n1-->Insert at last\n2-->Display\n3-->EXIT\n"); scanf("%d", &ch); switch(ch) { case 1: printf("Enter data of the node:\n"); scanf("%d", &d); head=ins_last(head, d); break; case 2: display(head); printf("\n"); break; case 3: exit(0); default: printf("Wrong choice.\n"); } } getch(); } node *ins_last(node *h, int d) { node *temp, *r=h; temp=(node*) malloc(sizeof(node)); temp->data=d; temp->next=NULL; if(h==NULL) h=temp; else { while(r->next!=NULL) r=r->next; r->next=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 last node of a single link list?
Post a Comment