SLL&DLL
SLL&DLL
AIM:
ALGORITHM:
Step 2:Insert
i. Allocate memory for new node the new node has two parts, one is element and another one is
next pointer.
ii. Check whether the list is empty or not. If the list is empty add as a first node.
iii. If the list is not empty add a new node in to last node of the list, update the previous node next
pointer is the new node.
Step 3:Deletion
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include"listadt.c"
void main()
int data,ch;
clrscr();
printf("1.Insert\t2.Delete\t3.Display\t4.Exit");
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&data);
insert(data);
break;
scanf("%d",&data);
deletion(data);
break;
case 3:display();
break;
case 4:exit(0);
}while(ch<4);
getch();
}
//listadt.c
struct node
int element;
*List=NULL,*P;
void display();
void insert(int X)
int pos;
newnode=malloc(sizeof(struct node));
newnode->element=X;
if(List->next==NULL)
{List->next=newnode;
newnode->next=NULL;
else
scanf("%d",&pos);
P=find(pos);
newnode->next=P->next;
P->next=newnode;}}
P=List->next;
while(P!=NULL&&P->element!=s)
P=P->next;
return P;
void deletion(int X)
temp=malloc(sizeof(struct node));
P=findprevious(X);
if(P->next!=NULL)
temp=P->next;
P->next=temp->next;
free(temp);}
else
P=List;
while(P->next!=NULL&&P->next->element!=s)
P=P->next;
return P;
void display()
if(List->next==NULL)
printf("list is empty");
else
{
P=List->next;
while(P!=NULL)
printf("%d-->",P->element);
P=P->next;}
printf("NULL");
OUTPUT:
2-->NULL
2-->3-->NULL
2-->NULL
RESULT:
Thus, the implementation of list using singly linked list has been written and executed
successfully.
Step 2:Insert()
i. Allocate memory for new node the new node has three parts, Such as previous, next pointer and
element.
ii. Check whether the list is empty or not. If the list is empty add as a first node.
iii. Other wise update the previous node next pointer as new node. The new node’s previous pointer
is updated according to the previous node memory.
Step 3:Addafter()
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next,*prev;
}*first=NULL,*last=NULL;
void insertend()
{
struct node *new,*temp;
new=(struct node*)malloc(sizeof(struct node));
printf("Enter the data ");
scanf("%d",&new->data);
new->next=NULL;
if(first==NULL)
{
new->prev=NULL;
first=new;
}
else
{
last->next=new;
new->prev=last;
}
last=new;
}
void insertbeg()
{
struct node *new;
new=(struct node*)malloc(sizeof(struct node));
printf("Enter the data ");
scanf("%d",&new->data);
if(first==NULL)
{
new->prev=NULL;
new->next=NULL;
first=new;
last=new;
}
new->prev=NULL;
new->next=first;
first=new;
}
void insertafter()
{
int pos,i;
struct node *new,*old;
new=(struct node*)malloc(sizeof(struct node));
printf("Enter the data ");
scanf("%d",&new->data);
printf("Enter the position after which data to be added ");
scanf("%d",&pos);
old=first;
for(i=0;i<pos-1;i++)
{
old=old->next;
if(old==NULL)
{
printf("There are less than %d elements",pos);
return;
}
}
old->next->prev=new;
new->next=old->next;
new->prev=old;
old->next=new;
}
void delnode()
{
int x;
struct node *old,*a,*b;
printf("Enter data to be deleted ");
scanf("%d",&x);
old=first;
while(old->data!=x&&old!=NULL)
{
old=old->next;
}
if(old==NULL)
{
printf("Data does not exist\n");
return;
}
if(old==first)
{
first=old->next;
first->prev=NULL;
}
else
{
if(old==last)
{
last=old->prev;
last->next=NULL;
}
else
{
a=old->prev;
b=old->next;
a->next=b;
b->prev=a;
}
}
free(old);
}
void display()
{
struct node *old;
old=first;
printf("List contains:\n");
while(old!=NULL)
{
printf("%d->",old->data);
old=old->next;
}
}
void main()
{
int ch;
clrscr();
printf("Menu\n");
printf("1.Insert at the end\n");
printf("2.Insert at the beginning\n");
printf("3.Insert in between nodes\n");
printf("4.Delete a node\n");
printf("5.Display list \n");
printf("6.Exit\n");
do
{
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:insertend();
break;
case 2:insertbeg();
break;
case 3:insertafter();
break;
case 4:if(first==NULL)
printf("List underflow\n");
else
delnode();
break;
case 5:if(first==NULL)
printf("List underflow\n");
else
display();
break;
case 6:break;
default:printf("Wrong Choice\n");
}
}
while(ch!=6);
getch();
}
OUTPUT:
Menu
1.Insert at the end
2.Insert at the beginning
3.Insert in between nodes
4.Delete a node
5.Display list
6.Exit
Enter your choice: 1
Enter the data 20
Enter your choice: 2
Enter the data 10
Enter your choice: 3
Enter the data 45
Enter the position after which data to be added 1
Enter your choice: 3
Enter the data 67
Enter the position after which data to be added 2
Enter your choice: 5
List contains:
10->45->67->20->Enter your choice: 4
Enter data to be deleted 67
Enter your choice: 5
List contains:
10->45->20->Enter your choice:6
RESULT:
Thus, the implementation of list using Doubly linked list has been written and executed
successfully.