Ex.
no: SINGLY LINKED LIST
AIM:
To write a ‘C’ program to create a singly linked list implementation.
ALGORITHM:
Step 1:Create two programs named as list.c and listadt.c
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
i. Check whether the list is empty or not.
ii. If the list is note empty, find the previous position of element to be deleted.
iii. The previous node next pointer is updated. That is, next of next memory value is
assigned.
iv. Then free the node.
Step 4:Display the elements from first node to last node.
Step 5:Stop the program
PROGRAM:
// Singly Linked List
#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
printf("\n Enter your Choice");
scanf("%d",&ch);
switch(ch)
case 1:printf("Enter the element to Insert");
scanf("%d",&data);
insert(data);
break;
case 2:printf("Enter the element to Delete");
scanf("%d",&data);
deletion(data);
break;
case 3:display();
break;
case 4:exit(0);
}while(ch<4);
getch();
}
//listadt.c
struct node *find(int);
struct node *findprevious(int);
struct node
int element;
struct node *next;
*List=NULL,*P;
void insert(int X);
void deletion(int X);
void display();
void insert(int X)
struct node *newnode;
int pos;
newnode=malloc(sizeof(struct node));
newnode->element=X;
if(List->next==NULL)
{List->next=newnode;
newnode->next=NULL;
else
printf("\nenter the value after which the element to be inserted:");
scanf("%d",&pos);
P=find(pos);
newnode->next=P->next;
P->next=newnode;}}
struct node *find(int s)
{
P=List->next;
while(P!=NULL&&P->element!=s)
P=P->next;
return P;
void deletion(int X)
struct node *temp;
temp=malloc(sizeof(struct node));
P=findprevious(X);
if(P->next!=NULL)
temp=P->next;
P->next=temp->next;
printf("\n the deleted element is %d",temp->element);
free(temp);}
else
printf("\nelement not found in the list");
struct node *findprevious(int s)
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;
printf("\n the contents of the list are:\n");
while(P!=NULL)
printf("%d-->",P->element);
P=P->next;}
printf("NULL");
OUTPUT:
1.Insert 2.Delete 3.Display 4.Exit
Enter your Choice1
Enter the element to Insert2
Enter your Choice3
the contents of the list are:
2-->NULL
Enter your Choice1
Enter the element to Insert3
enter the value after which the element to be inserted:2
Enter your Choice3
the contents of the list are:
2-->3-->NULL
Enter your Choice2
Enter the element to Delete3
the deleted element is 3
Enter your Choice3
the contents of the list are:
2-->NULL
RESULT:
Thus, the implementation of list using singly linked list has been written and executed
successfully.
Ex.no: DOUBLY LINKED LIST
Step 1:Start the program
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()
i. Get the position of the element to be inserted.
ii. Find that position insert the node at the position, update the previous and next pointer values.
Step 4:Deletion
i. Check whether the list is empty or not.
ii. If the list is note empty, find the previous position of element to be deleted.
iii. The previous node next pointer is updated. That is, next of next memory value is
assigned.
iv. Then free up the node.
Step 4:Display the elements from first node to last node.
Step 5:Stop the program
PROGRAM:
//Implementation of Doubly Linked List
#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.