Sie sind auf Seite 1von 2

Week 4:Write C programs to implement a double ended queue ADT using doubly linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*left,*right;
};
struct node
*front=NULL,*rear=NULL,*cur,*temp;
void insertrear()
{
cur=(struct node*)malloc(sizeof(struct
node));
printf("\n enter data to insert ");
scanf("%d",&cur->data);
cur->right=NULL;
if(rear==NULL&&front==NULL)
rear=front=cur;
else
{
rear->right=cur;
cur->left=rear;
rear=cur;
}
}
void insertfront()
{
cur=(struct node*)malloc(sizeof(struct
node));
printf("\n enter data to insert ");
scanf("%d",&cur->data);
cur->right=NULL;
if(rear==NULL&&front==NULL)
rear=front=cur;
else
{
cur->right=front;
front->left=cur;
front=cur;
}
}
void delfront()
{
if(front==NULL)
printf("\ndeQueue is empty\n");
else if(front==rear)
{
printf("\nDeleted data is %d
",front->data);

front=rear=NULL;
}
else
{
temp=front;
printf("\nDeleted data is %d ",temp>data);
front=front->right;
front->left=NULL;
free(temp);
}
}
void delrear()
{
if(front==NULL)
printf("\ndeQueue is empty\n");
else if(front==rear)
{
printf("\nDeleted data is %d
",rear->data);
front=rear=NULL;
}
else
{
temp=rear;
printf("\nDeleted data is %d",temp>data);
if(front==rear)
front=rear=NULL;
rear=rear->left;
rear->right=NULL;
free(temp);
}
}
void display()
{
if(front==NULL)
printf("\ndeQueue is empty\n");
else
{
temp=front;
while(temp!=NULL)
{
printf("<-%d ->",temp->data);
temp=temp->right;
}
}
}
int main()

{
int ch;
printf("\n1.Insert Front\n2.Delete
front\n3.Insert rear\n4.Delete
Rear\n5.Display\n6.Exit");
while(1)
{
printf("\nEnter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: insertfront();
break;
case 2: delfront();
break;
case 3: insertrear();
break;
case 4: delrear();
break;
case 5: display();
break;
case 6: exit(0);
}//end of switch
}//end of while
}

Enter your choice 1


enter data to insert 10
Enter your choice 3
enter data to insert 40
Enter your choice 3
enter data to insert 50
Enter your choice 3
enter data to insert 60
Enter your choice 5
<-10 -><-20 -><-30 -><-40 -><-50 -><-60 ->
Enter your choice 2
Deleted data is 10
Enter your choice 2
Deleted data is 20
Enter your choice 2
Deleted data is 30
Enter your choice 4

OUTPUT:

Deleted data is 60
Enter your choice 4

$ ./a.out
1.Insert Front
2.Delete front
3.Insert rear
4.Delete Rear
5.Display
6.Exit
Enter your choice 1

Deleted data is 50
Enter your choice 4
Deleted data is 40
Enter your choice 4
Queue is empty
Enter your choice 2

enter data to insert 30


Queue is empty
Enter your choice 1
Enter your choice 6
enter data to insert 20

Das könnte Ihnen auch gefallen