Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#include<stdio.

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;

struct node* next;

struct node* prev;

};

struct node* head=NULL;

int main()

int choice,c;

while(1)

printf("\n1.insert at begining\n2.insert at end \n3.insert at position\n4.delete from begining


\n5.delete from end\n6.delete from position\n7.length of list\n8.reverse \n9.reverse using
stack\n10.exit\n");

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();

printf("length of list is:%d\n",c);

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;

struct node* temp;

printf("enter number :\n");

scanf("%d",&num);

temp=(struct node* )malloc(sizeof(struct node));

temp->data=num;

temp->next=head;

head=temp;

head->prev=temp;

temp->prev=NULL;

}
void ie()

{int num;

struct node* temp;

struct node* p=head;

printf("enter number :\n");

scanf("%d",&num);

temp=(struct node* )malloc(sizeof(struct node));

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();

struct node* temp;

struct node* p=head;

printf("enter number :\n");


scanf("%d",&num);

printf("enter position:\n");

scanf("%d",&pos);

temp=(struct node* )malloc(sizeof(struct node));

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()

{struct node* temp;

struct node* p=head;

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;

struct node* temp;

struct node* p=head;

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;

struct node * p,*q;

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()

struct node* temp=head;

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()

struct node* temp=head;

int count=0;

while(temp!=NULL)

temp=temp->next;

count++;

return count;

void display()

struct node* temp=head;

printf("list is:\n");

while(temp!=NULL)

{ printf(" %d\t",temp->data);

temp=temp->next;

You might also like