0% found this document useful (0 votes)
344 views

Double Ended Queue ADT Using Doubly Linked List

The document discusses implementing a double ended queue (dequeue) data structure using a doubly linked list in C programming language. It includes functions to insert and delete elements from both front and rear of the queue as well as a display function to print the queue elements. The main function takes user input to call these functions and test the implementation.

Uploaded by

gouse1210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views

Double Ended Queue ADT Using Doubly Linked List

The document discusses implementing a double ended queue (dequeue) data structure using a doubly linked list in C programming language. It includes functions to insert and delete elements from both front and rear of the queue as well as a display function to print the queue elements. The main function takes user input to call these functions and test the implementation.

Uploaded by

gouse1210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy