The linked list is a chain of structures in which each structures consists of data as well as as pointer , which store the address (links) of the next logical structure in the list.A linked list is a data structures used to maintain a dynamic series of data .Each nodes consists two parts field . First field holds the value or data part and second field holds the reference to the next node .
Example :
Program :
Output :
Example :
Program :
#include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }; void add(struct node **, int); // double pointer void display(struct node *); // single pointer void main() { int ch,num; struct node *h=NULL; clrscr(); while(1) { printf("\t1.Add a no: \n"); printf("\t2.Display:\n"); printf("\t3.Exit\n"); printf("Enter ur choice:\n"); scanf("%d",&ch); switch(ch) { case 1: printf("\tEnter no:\n"); scanf("%d",&num); add(&h,num); break; case 2:display(h); break; case 3:exit(0); default :printf("\tInvalid no \n"); } } } void add(struct node **s,int num) { struct node *temp,*r; temp=(struct node*)malloc(sizeof(struct node)); temp->data=num; temp->next=NULL; if(*s==NULL) *s=temp; else { r=*s; while(r->next!=NULL) { r=r->next; } r->next=temp; } } void display(struct node *s) { struct node * ptr; ptr=s; printf("\tDispaly:\t"); while(ptr!=NULL) { printf("%d->",ptr->data); ptr=ptr->next; } printf("NULL \n\n"); }
Output :