/* http://native-code.blogspot.com */ #include<stdio.h> #include<conio.h> #include<malloc.h> typedef struct link { int data; struct link *next; } node; int count_nodes(node*); node *ins_beg(node*,int); void display(node*); void main() { int x,ch,c;node *head=NULL; while(1) { printf("/* http://native-code.blogspot.com */"); printf("\n\n1: Insert at first\n2: Display\n3: Count nodes\n4: EXIT\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the value:\n"); scanf("%d",&x); head=ins_beg(head, x); break; case 2: display(head); printf("\n"); break; case 3: c=count_nodes(head); printf("Number of node is: %d", c); printf("\n"); break; case 4: exit(0); default: printf("Wrong choice.\n"); } } getch(); } node *ins_beg(node *h, int i) { node *temp, *r=h; temp=(node *)malloc(sizeof(node)); temp->data=i; 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"); } int count_nodes(node *h) { int c=0; node *p=h; while(p!=NULL) { c=c+1; p=p->next; } return(c); } /* http://native-code.blogspot.com */
Home
» C
» data-structure
» single-linked-list
» How can we count the number of nodes in a single link list?
Post a Comment