Professional Documents
Culture Documents
Aim:
To create a singly Linked list and to perform insertion and deletion operations.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
typedefstruct list
{
intnum;
struct list *next;
}LIST;
LIST *head,*node,*temp;
void create();
void insert();
void del();
void display();
voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}}}
void create()
{
int k=1;
printf("\n enter the element( else enter -1 to stop):");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
if(head==NULL)
head=s1;
else p-
>next=s1;
p=s1;
printf("\n enter the element(else enter -1 to
stop):"); scanf("%d",&k);
}}
void display()
{
temp=head;
printf("\n List elements are \n");
while(temp!=NULL)
{
printf("\n %d--->",temp-
>num); temp=temp->next;
}
getch();
}
void del()
{
int i=0,pos;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
if(pos==1)
{
head=temp->next;
printf("node
deleted"); break;
}
else
{
i++;
if(i+1==pos)
{
temp->next=temp->next-
>next; printf("node deleted");
}
}
temp=temp->next;
}
getch();
}
void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next=p;
printf("value inserted \n");
}
temp=temp->next;
}}
Enter
choice 1
Enter the element -1 to stop
10
Enter the element -1 to stop
20
Enter the element -1 to stop
30
Enter the element -1 to stop
-1
Enter
choice 3
Enter the position to be
deleted 1
Enter
choice 4
List elements are
10
30
Enter
choice 5
RESULT:
Thus the program to perform operations of singly linked list was successfully executed.
Aim:
To create a Doubly Linked list and to perform insertion and deletion operations.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
typedefstruct list
{
intnum;
struct list *prev;
struct list
*next; }LIST;
LIST *head,*node,*temp;
void create();
void insert();
void del();
void display();
voidmain()
{
intch;
clrscr();
head=NULL;
while(1)
{
printf("enter the choice \n");
printf(" 1:create list \n 2:insert an element to list \n 3:delete an element \n 4.display \n 5.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("wrong choice");
break;
}
}
}
void create()
{
int k=1;
printf("\n enter the element(Else enter -1 to stop:");
scanf("%d",&k);
while(k!=-1)
{
LIST *s1,*p;
s1=(struct list *)malloc(sizeof(LIST));
s1->num=k;
s1->next=NULL;
s1->prev=NULL;
if(head==NULL)
head=s1;
else
{
p->next=s1;
s1->prev=p;
}
p=s1;
printf("\n enter the element (Else enter -1 to stop:");
scanf("%d",&k);
}
}
void display()
{
temp=head;
printf("\n List elements are
\n\n"); while(temp!=NULL)
{
printf("%d--->",temp->num);
temp=temp->next;
}
getch();
}
void del()
{
intpos,i=0;
LIST *temp1;
temp=head;
printf("Enter the position to delete \n");
scanf("%d",&pos); while(temp!
=NULL)
{
i++;
if(pos==1)
{
head=temp->next;
head->prev=NULL;
printf("Node deleted
\n"); break;
}
else if(i==pos)
{
temp->prev->next=temp->next;
temp->next->prev=temp-
>prev; printf("node deleted");
}
temp=temp->next;
}
getch();
}
void insert()
{
LIST *p;
int y;
temp=head;
printf("Enter the element after which new element to be inserted
\n"); scanf("%d",&y);
printf("Enter the node value to insert \n");
p=(struct list *)malloc(sizeof(LIST));
scanf("%d",&p->num); while(temp!
=NULL)
{
if(temp->num==y)
{
p->next=temp->next;
temp->next->prev=p;
temp->next=p; p-
>prev=temp; printf("value
inserted \n");
}
temp=temp->next;
}
}
Enter
choice 1
Enter the element(Else enter -1 to stop)
10
Enter the element(Else enter -1 to stop)
20
Enter the element (Else enter -1 to stop)
30
Enter the element (Else enter -1 to stop)
-1
Enter
choice 3
Enter the element to be
deleted 10
Enter
choice 4
List elements are
20 30
Enter
choice 5
RESULT:
Thus the program to perform operations of doubly linked list was successfully executed.