C Program to implement of Linked implementation of List

0
611

C Program to implement of Linked implementation of List

CONSTRUCTION:

  1. Start the program.
  2. Initialize and declare variables using structure and pointer.
  3. Develop functions of linked list using the following as

a)Create b)Insert c)Delete d)Display

  1. Allocate memory for each new element as new1=((struct node *)malloc(sizeof (struct node))
  1. Implement linked list pointer for operations.
  2. Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*first,*new1,*p;
void create();
void disp();
void insert();
void del();
int n;
void main()
{
int a;
int ch;
clrscr();
printf("\nSINGLY LINKED LIST");
first=NULL;
do
{
printf("\n\nCHOICES:\n");
printf("\t1.Creating List\n");
printf("\t2.Insertion\n");
printf("\t3.Deletion\n");
printf("\t4.Display\n");
printf("\nenter the choice:");
scanf("%d",&a);
switch(a)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
disp();
break;
default:
printf("\nNo such choice found");
}
printf("\nDo you want to continue(1-yes/0-no)");
scanf("%d",&ch);
}
while(ch==1);
getch();
}
void create()
{
printf("\n\n\tCREATION OF LIST");
printf("\nEnter the values(to break enter 0):\n");
scanf("%d",&n);
while(n!=0)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->info=n;
if(first==NULL)
{
new1->link=NULL;
first=new1;
p=first;
}
else
{
p->link=new1;
p=new1;
p->link=NULL;
}
scanf("%d",&n);
}
}
void disp()
{
printf("\n\t\tDISPLAY");
p=first;
printf("\nDATA\tLINK\n");
if(first!=NULL)
{
do
{
printf("\n%d\t%d",p->info,p->link);
p=p->link;
}
while(p->link!=NULL);
printf("\n%d\t%d",p->info,p->link);
}
else
{
printf("empty list");
}
}
void insert()
{
int xx,yy,zz;
printf("Where do u want to insert: \n1.First\n2.Middle\n3.Last");
scanf("%d",&xx);
switch (xx)
{
case 1:
 new1=(struct node *)malloc(sizeof(struct node));
 printf("\n\t\t\tEnter the value to be inserted");
 scanf("%d",&n);
 new1->info=n;
 new1->link=first;
 first=new1;
 p=first;
 break;
case 2:
 printf("Enter after which element value is to be inserted");
 scanf("%d",&zz);
 p=first;
 do
 {
 if(p->info==zz)
 {
 new1=(struct node *)malloc(sizeof(struct node));
 printf("\nEnter the value to be inserted");
 scanf("%d",&n);
 new1->info=n;
 yy=p->link;
 p->link=new1;
 printf("Inserted elements address%d\n",new1);
 p=new1;
 p->link=yy;
 printf("Next elements address%d\n",p->link);
 }
 p=p->link;
 }
 while(p->link!=NULL);
 break;
case 3:
 printf("\n\t\t\tEnter the value to be inserted");
 scanf("%d",&n);
 new1=(struct node*)malloc(sizeof(struct node));
 new1->info=n;
 p->link=new1;
 printf("%d",p->link);
 p=new1;
 p->link=NULL;
 printf("%d,%d",p->info,p->link);
 break;
 }
}
void del()
{
int x;
printf("\n\t\t\tDELETION");
printf("\n Enter the value to delete");
scanf("%d",&x);
if(first->info==x)
{
first=first->link;
}
else
{
p=first;
while((((p->link)->info)!=x)&&p->link!=NULL)
{
p=p->link;
}
if(p->link==NULL)
{
printf("Element not found");
}
else
{
p->link=(p->link)->link;
}
}
}

 

Leave a Reply