Linked lists are a way to store data with structures so that the programmer can automatically create a new place to store data whenever necessary. Specifically, the programmer writes a struct or class definition that contains variables holding information about something, and then has a pointer to a struct of its type. Each of these individual struct or classes in the list is commonly known as a node.
Language : C
[sourcecode language='c']
//Linearly Linked List
#include
#include
#include
void insertion();
void display();
void lsearch();
void del();
struct node{
int a;
struct node *next;
}*list,*temp,*save,*pred;
void main()
{ int a;
char ch;
list=(struct node *)malloc(sizeof(struct node));
list=NULL;
do{
printf(“\n1.Insertion\n2.Display\n3.Linear Search\n4.Delete: “);
scanf(“%d”, &a);
switch(a)
{
case 1: insertion();
break;
case 2:display();
break;
case 3:lsearch();
break;
case 4: del();
break;
default :
printf(“\nWrong Choice!!!”);
}
printf(“\nMENU ?”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y’ || ch==’Y');
}
void insertion()
{
int x,a,pos,ctr,af;
char ch;
do {
temp=(struct node *)malloc(sizeof(struct node));
printf(“\nEnter the element : “);
scanf(“%d”, &x);
printf(“\n1.Front\t2.Specify Position\t3.Rear”);
scanf(“%d”, &a);
switch(a)
{
case 1: //Insert at Front
temp->a=x;
temp->next=list;
list=temp;
break;
case 2: //Insert at a Position
printf(“Enter the position : “);
scanf(“%d”, &pos);
save=list;
ctr=0;
pred=NULL;
while(ctr
if(list==NULL)
{
printf("Wrong position!");
return;
}
pred=list;
list=list->next;
ctr++;
}
printf(“Insert 1.After or 2.Before %d :”, list->a);
scanf(“%d”, &af);
if(af==1)
{
temp->a=x;
temp->next=list->next;
list->next=temp;
}
else
{ list=pred;
temp->a=x;
temp->next=list->next;
list->next=temp;
}
list=save;
break;
case 3: //Insert at Rear
save=list;
if(list==NULL) //if EMPTY Performs insert at FRONT
{
temp->a=x;
temp->next=list;
list=temp;
break;
}
while(list->next!=NULL)
list=list->next;
temp->a=x;
temp->next=NULL;
list->next=temp;
list=save;
break;
default :
printf(“\nWrong Choice!!!”);
}
printf(“Continue Insertion (Y/N)?”);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’Y’ || ch==’y');
display();
}
void display()
{
if(list==NULL)
{
printf(“\nThe list is empty!”);
return;
}
save=list;
while(list!=NULL)
{
printf(“\t%d”, list->a);
list=list->next;
}
list=save;
}
void lsearch()
{ int flag,x;
printf(“\nEnter Element to Search : “);
scanf(“%d”, &x);
save=list;
while(list!=NULL)
{ flag=0;
if(x==list->a)
{
flag=1;
break;
}
list=list->next;
}
list=save;
if(flag==1)
printf(“\nElement Found!”);
else
printf(“\nElement NOT Found!”);
}
void del()
{ int del,ctr,pos;
char ch;
if(list==NULL)
{
printf(“\nUnderflow!!!”);
return;
}
do{
printf(“\n1.Delete from Front\n2.Specify Position\n3.Delete from Rear : “);
scanf(“%d”, &del);
switch(del)
{
case 1:
printf(“\nThe deleted element is : %d”, list->a);
free(list);
list=list->next;
break;
case 2:
temp=NULL;
printf(“Enter the position : “);
scanf(“%d”, &pos);
save=list;
ctr=0;
while(ctr
if(list==NULL)
{
printf("Wrong position!");
return;
}
temp=list;
list=list->next;
ctr++;
}
printf(“\nThe deleted element is : %d”, list->a);
temp->next=list->next;
list->next=NULL;
free(list);
list=save;
break;
case 3:
save=list;
temp=NULL;
while(list->next!=NULL)
{
temp=list;
list=list->next;
}
printf(“\nThe deleted element is : %d”, list->a);
temp->next=NULL;
free(list);
list=save;
break;
default :
printf(“\nWrong Choice!!!”);
}
printf(“\nContinue Deletion (Y/N)? : “);
fflush(stdin);
scanf(“%c”, &ch);
}while(ch==’y’ || ch==’Y');
}
[/sourcecode]






