Professional Documents
Culture Documents
Doublylinkedlist
Doublylinkedlist
h>
#include<stdlib.h>
void ib();
void ie();
void ip();
void db();
void de();
void dp();
void rev();
int count();
void display();
void rev_stack();
void pop();
void push();
int top=-1,stack[20];
struct node
int data;
};
int main()
int choice,c;
while(1)
printf("select operation:\n");
scanf("%d",&choice);
switch(choice)
case 1:
ib();
display();
break;
case 2:
ie();
display();
break;
case 3:
ip();
display();
break;
case 4:
db();
display();
break;
case 5:
de();
display();
break;
case 6:
dp();
display();
break;
case 7:
c=count();
break;
case 8:
rev();
display();
break;
case 9:
rev_stack();
display();
break;
case 10:
exit(0);
break;
default:
printf("invalid selection:\n");
break;
getch();
return 0;
void ib()
int num;
scanf("%d",&num);
temp->data=num;
temp->next=head;
head=temp;
head->prev=temp;
temp->prev=NULL;
}
void ie()
{int num;
scanf("%d",&num);
temp->data=num;
temp->next=NULL;
if(head==NULL)
head=temp;
temp->next=head;
temp->prev=NULL;
else
while(p->next!=NULL)
p=p->next;
p->next=temp;
temp->prev=p;
void ip()
int num,pos,i,c=count();
printf("enter position:\n");
scanf("%d",&pos);
temp->data=num;
if(pos<1||pos>c+1)
printf("invalid position\n");
else if(pos==1)
temp->next=head;
temp->prev=NULL;
head=temp;
else
for(i=1;i<pos-1;i++)
p=p->next;
temp->next=p->next;
p->next=temp;
p->next->prev=temp;
temp->prev=p;
void db()
{
struct node* temp=head;
int c=count();
if(head==NULL)
printf("list is empty\n");
else if(c=1)
head=NULL;
free(temp);
else
head=temp->next;
temp->next->prev=NULL;
free(temp);
void de()
int i,c=count();
if(head==NULL)
printf("list is empty\n");
else if(p->next==NULL)
head=NULL;
free(p);
else
for(i=1;i<c-1;i++)
p=p->next;
temp=p->next;
p->next=NULL;
temp->prev=NULL;
free(temp);
void dp()
int c=count(),pos,i;
printf("enter position:\n");
scanf("%d",&pos);
if(head==NULL)
printf("list is empty\n");
else if(pos<1||pos>c)
printf("invalid position\n");
else if(pos==1)
{
head=NULL;
free(p);
else
for(i=1;i<pos-1;i++)
p=p->next;
temp=p->next;
p->next=temp->next;
temp->next->prev=p;
free(temp);
void rev()
int i,j,k,c=count(),temp;
p=q=head;
i=1;
j=c;
while(i<j)
k=1;
while(k<j)
q=q->next;
k++;
temp=p->data;
p->data=q->data;
q->data=temp;
p=p->next;
q=head;
i++;
j--;
void rev_stack()
while(temp!=NULL)
push(temp->data);
temp=temp->next;
temp=head;
while(temp!=NULL)
temp->data=stack[top];
pop();
temp=temp->next;
void push(int d)
top++;
stack[top]=d;
void pop()
{
top--;
int count()
int count=0;
while(temp!=NULL)
temp=temp->next;
count++;
return count;
void display()
printf("list is:\n");
while(temp!=NULL)
{ printf(" %d\t",temp->data);
temp=temp->next;