AIM:
To
write a C program to implement doubly linked list.
ALGORITHM:
Step 1: Define a list as a node of a
structure with one data, one pointer
pointing to next element
in the structure and another pointer pointing to
previous element in the structure.
Step 2: Declare the function
prototypes creation (), insertion (), deletion () and
display () to perform the list function.
Step
3: Declare the necessary pointer variables as structure node data type.
Step
4: Get a choice from the user.
Step
5: If the choice is create get first data from the user by calling the function
create () and display
contents of the list.
Step
6: If the choice is insert, get data and its position by calling function
insert (), assign the next and previous field of the given data node
according to the position. Display the contents of the list.
Step
7: If the choice is delete, get the position of data which is going to be
removed from the list and assign next field of previous node
to address.
Step
8: Repeat the steps 4, 5&6 until the choice is exit.
Step
9: Terminate the execution.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node*next,*prev;
}*New,*New1,*temp,*start,*dummy;
void add();
struct node *get_node();
void display();
void Delete();
int find(int num);
int first=1;
void main()
{
char ans='y';
int choice,num,found,flag=0;
start=NULL;
do
{
clrscr();
printf("\nProgram for DLL\n");
printf("\n1.Insert\n2.Delete\n3.Diplay\n4.Search\n5.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
Delete();break;
case 3:
display();break;
case 4:
printf("\nEnter the number to be searched");
scanf("%d",&num);
temp=start;
while((temp!=NULL)&&(flag==0))
{
found=find(num);
flag=found;
}
if(found==1)
printf("\nThe number is present\n");
else
printf("\nThe number is not present\n");
break;
case 5:
exit(0);break;
default:
printf("\nInvalid
option");break;
}
printf("\nDo you want to continue?(y/n)");
ans=getche();
}while(ans=='y'||ans=='Y');
getch();
}
void add()
{
clrscr();
New=get_node();
printf("\nEnter the element");
scanf("%d",&New->data);
if(first==1)
{
start=New;
first=0;
}
else
{
dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;
dummy->next=New;
New->prev=dummy;
}
}
struct node *get_node()
{
New1=malloc(sizeof(struct node));
New1->next=NULL;
New1->prev=NULL;
return(New1);
}
void display()
{
clrscr();
temp=start;
if(temp==NULL)
printf("\nThe DLL is empty\n");
else
{
while(temp!=NULL)
{
printf("\n\n%d=>",temp->data);
temp=temp->next;
}
printf("NULL");
}
getch();
}
int find(int num)
{
if(temp->data==num)
return 1;
else
temp=temp->next;
return 0;
}
void Delete()
{
int num,flag=0;
int found;
int last=0;
clrscr();
temp=start;
if(temp==NULL)
printf("\nDLL is not created");
else
{
printf("Enter the number to be deleted");
scanf("%d",&num);
while((flag==0)&&(temp!=NULL))
{
found=find(num);
flag=found;
}if(found==0)
printf("\nNumber not found");
else
{if(temp==start)
{
start=start->next;
temp->next=NULL;
start->prev=NULL;
free(temp);
getch();
printf("\nThe strating node is deleted");
}else
{if(temp->next==NULL)
last=1;
else
last=0;
(temp->next)->prev=temp->prev;
(temp->prev)->next=temp->next;
temp->prev=NULL;
temp->next=NULL;
free(temp);
if(last)
printf("\nLast node is deleted");
else
printf("\nIntermediate node is deleted");
}}}}
OUTPUT
Program for DLL
1.Insert
2.Delete
3.Diplay
4.Search
5.Exit
Enter your choice:1
Enter the element10
Do you want to continue?(y/n)
Enter your choice:1
Enter the element20
Do you want to continue?(y/n)
Enter your choice:2
Enter the number to be deleted20
Last node is deleted
Do you want to continue?(y/n)
Enter your choice:3
10=>NULL
Do you want to continue?(y/n)
Enter your choice:4
Enter your choice:4
Enter the number to be searched10
The number is present
Do you want to continue?(y/n)
Enter your choice:5
RESULT: Thus a C program is
written to implement doubly linked list and executed
successfully.
No comments:
Post a Comment