18. Program to implement
doubly linked list?
Program
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link,*pre,*nxt;
}*start=NULL,*temp,*prev,*newnode;
/*-------Creating the node-----*/
/*-------Creating the node-----*/
void createnode()
{
int item;
newnode=malloc(sizeof(struct node));
printf("\nEnter the element\n");
scanf("%d",&item);
newnode->data=item;
newnode->nxt=NULL;
newnode->pre=NULL;
}
/*-------Displaying the linked list------*/
/*-------Displaying the linked list------*/
void display()
{
temp=start;
printf("\nList is \n");
while(temp->nxt!=NULL)
{
printf("%d->",temp->data);
prev=temp;
temp=temp->nxt;
}
printf("%d",temp->data);
}
/*--------Inserting at the end-------*/
/*--------Inserting at the end-------*/
void insend()
{
createnode();
if(start==NULL)
start=newnode;
else {
temp=start;
while(temp->nxt!=NULL)
{
temp=temp->nxt;
}
temp->nxt=newnode;
newnode->pre=temp;
newnode->nxt=NULL;
}
}
/*--------Creating the linked list-------*/
/*--------Creating the linked list-------*/
void create()
{
int n,i;
printf("Enter the no.of nodes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
insend();
}
}
/*-------Inserting at the beginning------*/
/*-------Inserting at the beginning------*/
void inserbig()
{
temp=start;
createnode();
start=newnode;
start->nxt=temp;
temp->pre=start;
start->pre=NULL;
display();
}
/*-------Inserting in between the nodes------*/
/*-------Inserting in between the nodes------*/
void insbtnod()
{
int pos,k=0;
createnode();
printf("Enter the position of the node\n");
scanf("%d",&pos);
temp=start;
while(k<pos-1)
{
prev=temp;
temp=temp->nxt;
k++;
}
newnode->nxt=prev->nxt;
newnode->pre=temp->pre;
temp->pre=newnode;
prev->nxt=newnode;
}
/*-------Insert operations------*/
/*-------Insert operations------*/
void insert()
{
int ch;
printf("1.insert at beginning\n2.insert at end\n3.insert between 2 nodes\n");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insbig();
break;
case 2:
insend();
break;
case 3:
insbtnod();
break;
default:
printf("Wrong choice\n");
}
display();
}
/*------Deletion from the beginning-----*/
/*------Deletion from the beginning-----*/
void delbig()
{
if(start==NULL)
printf("Empty stack\n");
else
{
temp=start;
start=temp->nxt;
printf("element deleted:%d",temp->data);
free(temp);
}
}
/*------Deletion at the end-----*/
void delend()
void delend()
{
if(start==NULL)
printf("Empty stack\n");
else
{
temp=start;
if(temp->nxt==NULL)
start=NULL;
else
{
while(temp->nxt!=NULL)
{
prev=temp;
temp=temp->nxt;
}
}
printf("element deleted:%d",temp->data);
prev->nxt=NULL;
}
}
/*------Deletion in between the nodes-----*/
/*------Deletion in between the nodes-----*/
void delbtnod()
{
int k=1,pos;
printf("Enter the position\n");
scanf("%d",&pos);
temp=start;
while(k<pos)
{
prev=temp;
temp=temp->nxt;
k++;
}
temp->nxt->pre=temp->pre;
prev->nxt=temp->nxt;
printf("Element deleted :%d",temp->data);
free(temp);
}
/*------Delete operations-----*/
/*------Delete operations-----*/
void delet()
{
int ch;
printf("1.delete from beginning\n2.delete from end\n3.delete in between
nodes\n");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
delbig();
break;
case 2:
delend();
break;
case 3:
delbtnod();
break;
default:
printf("Wrong choice\n");
}
display();
}
/*------Counting the number of nodes------*/
/*------Counting the number of nodes------*/
void count()
{
int count;
if(start==NULL)
{
printf("NO LIST\n");
}
else
{
count=0;
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
count++;
}
printf("COUNT :=%d\t",count-1);
}
}
/*----Searching in the doubly linked list----*/
/*----Searching in the doubly linked list----*/
void search()
{
int count=0,p=0,item;
if(start==NULL)
printf("NO LIST\n");
else
{
printf("Enter item to be searched\n");
scanf("%d",&item);
temp=start;
do
{
p++;
if(temp->data==item)
{
count++;
printf("NO: found at position :%d\t",p);
}
temp=temp->nxt;
}while(temp!=NULL);
if(count==0)
printf("NO: not found");
}
}
void main()
{
int ch;
char c;
clrscr();
do
{
printf("MENU\t\t\n1.creation\n2.insertion\n3.deletion\n4.count\n5.search\n
6.display\n");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
count();
break;
case 5:
search();
break;
case 6:
display();
break;
}
printf("\n\nDo you want to continue y/n\n");
scanf(" %c",&c);
}while((c=='y')||(c=='Y'));
getch();
}
Output
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
1
Enter the no.of nodes
2
Enter the element
56
Enter the element
78
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
2
1.insert at beginning
2.insert at end
3.insert between 2 nodes
Enter your choice
1
Enter the element
77
List is
77->56->78
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
2
1.insert at beginning
2.insert at end
3.insert between 2 nodes
Enter your choice
2
Enter the element
88
List is
77->56->78->88
Do you want to continue y/n
Y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
2
1.insert at beginning
2.insert at end
3.insert between 2 nodes
Enter your choice
3
Enter the element
12
Enter the position of the node
4
List is
77->56->78->12->88
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
3
1.delete from beginning
2.delete from end
3.delete in between nodes
Enter your choice
1
element deleted:77
List is
56->78->12->88
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
3
1.delete from beginning
2.delete from end
3.delete in between nodes
Enter your choice
2
element deleted:88
List is
56->78->12
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
3
1.delete from beginning
2.delete from end
3.delete in between nodes
Enter your choice
3
Enter the position
2
Element deleted :78
List is
56->12
Do you want to continue y/n
y
MENU
1.creation
2.insertion
3.deletion
4.count
5.search
6.display
Enter your choice
5
Enter item to be searched
12
NO: found at position :2
Do you want to continue y/n
n
No comments:
Post a Comment