answersLogoWhite

0

Double-ended queue using c language

Updated: 12/10/2022
User Avatar

Wiki User

14y ago

Best Answer

/* c++ program to implement double ended queue using doubly linked list with templates*/
#include
#include
#include
template
struct node
{
t data;
node *llink;
node *rlink;
};


template
class dequeue
{
private:
node *front,*rear;
public:
dequeue()
{
front=rear=NULL;
}


node *getnode()
{
node *newnode=new node;
newnode->llink=NULL;
newnode->rlink=NULL;
return newnode;
}


void insfrnt();
void insrear();
void delfrnt();
void delrear();
void disfrnt();
void disrear();
};


template
void dequeue::insfrnt()
{
node *newnode=getnode();
cout<<"Enter The Element That Has To Enqueued At The Front"<cin>>newnode->data;
if(front==NULL && rear==NULL)
front=rear=newnode;
else
{
newnode->rlink=front;
front->llink=newnode;
front=newnode;
}
}


template
void dequeue::insrear()
{
node *newnode=getnode();
cout<<"Enter The Element That has to be Enqueued at the Rear"<cin>>newnode->data;
if(front==NULL && rear==NULL)
front=rear=newnode;
else
{
rear->rlink=newnode;
newnode->llink=rear;
rear=newnode;
}
}


template
void dequeue::delfrnt()
{
if(front==NULL)
cout<<"Dequeue Underflow"<else
{
node *temp;
temp=front;
if(front==rear)
front=rear=NULL;
else
{
front=temp->rlink;
front->llink=NULL;
}
cout<data<<" is the element deleted from the dequeue"<delete temp;
}
}


template
void dequeue::delrear()
{
if(rear==NULL)
cout<<"Dequeue Underflow"<else
{
node *temp,*prev;
temp=rear;
if(front==rear)
front=rear=NULL;
else
{
rear=temp->llink;
rear->rlink=NULL;
}

cout<data<<" is the element deleted from the dequeue"<delete temp;
}
}


template
void dequeue::disfrnt()
{
if(front==NULL)
cout<<"Dequeue Underflow"<else
{
node *temp;
temp=front;
cout<<"front";
while(temp!=NULL)
{
cout<<"->"<data;
temp=temp->rlink;
}
cout<<"<-rear"<}
}


template
void dequeue::disrear()
{
if(rear==NULL)
cout<<"Dequeue Underflow"<else
{
node *temp;
temp=rear;
cout<<"rear";
while(temp!=NULL)
{
cout<<"->"<data;
temp=temp->llink;
}
cout<<"->front"<}
}


void main()
{
dequeue dq_int;
dequeue dq_float;
dequeue dq_char;
int choice;
do
{
clrscr();
cout<<"DEQUEUE :"<cout<<"\t Double Ended Queue (abb :- deque ; pronounced as deck) is an abstract data structure for which elements can be added at the front as well as at the rear. It is often called as Head-Tailed Linked List"<cout<<"\t========================"<cout<<"\t\tDequeue Menu"<cout<<"\t========================"<cout<<"[1] Dequeue Of Integer Values"<cout<<"[2] Dequeue Of Floating Values"<cout<<"[3] Dequeue Of Characters"<cout<<"[4] Exit"<cout<<"\t Enter Your Choice"<cin>>choice;
switch(choice)
{
case 1:
clrscr();
int chi;
do
{
cout<<"\t======================="<cout<<"\tDequeue Of Integer Values"<cout<<"\t======================="<cout<<"[1] Insert Element At Front"<cout<<"[2] Insert Element At Rear"<cout<<"[3] Delete Element At Front"<cout<<"[4] Delete Element At Rear "<cout<<"[5] Display Element(s) At Front"<cout<<"[6] Display Element(s) At Rear"<cout<<"[7] Back To Main Menu"<cout<<"\tEnter Your Choice"<cin>>chi;
switch(chi)
{
case 1: dq_int.insfrnt();
break;
case 2: dq_int.insrear();
break;
case 3: dq_int.delfrnt();
break;
case 4: dq_int.delrear();
break;
case 5: dq_int.disfrnt();
break;
case 6: dq_int.disrear();
break;
}
}
while(chi<=6);
break;




case 2:
clrscr();
int chF;
do
{
cout<<"\t======================="<cout<<"\tDequeue Of Floating Values"<cout<<"\t======================="<cout<<"[1] Insert Element At Front"<cout<<"[2] Insert Element At Rear"<cout<<"[3] Delete Element At Front"<cout<<"[4] Delete Element At Rear "<cout<<"[5] Display Element(s) At Front"<cout<<"[6] Display Element(s) At Rear"<cout<<"[7] Back To Main Menu"<cout<<"\tEnter Your Choice"<cin>>chi;
switch(chi)
{
case 1: dq_float.insfrnt();
break;
case 2: dq_float.insrear();
break;
case 3: dq_float.delfrnt();
break;
case 4: dq_float.delrear();
break;
case 5: dq_float.disfrnt();
break;
case 6: dq_float.disrear();
break;
}
}
while(chi<=6);
break;


case 3:
clrscr();
int chc;
do
{
cout<<"\t======================="<cout<<"\tDequeue Of Integer Values"<cout<<"\t======================="<cout<<"[1] Insert Element At Front"<cout<<"[2] Insert Element At Rear"<cout<<"[3] Delete Element At Front"<cout<<"[4] Delete Element At Rear "<cout<<"[5] Display Element(s) At Front"<cout<<"[6] Display Element(s) At Rear"<cout<<"[7] Back To Main Menu"<cout<<"\tEnter Your Choice"<cin>>chc;
switch(chc)
{
case 1: dq_char.insfrnt();
break;
case 2: dq_char.insrear();
break;
case 3: dq_char.delfrnt();
break;
case 4: dq_char.delrear();
break;
case 5: dq_char.disfrnt();
break;
case 6: dq_char.disrear();
break;
}
}
while(chi<=6);
break;





case 4: exit(0);
break;


}
}
while(choice<=3);
}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Double-ended queue using c language
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

C language using list?

C is a programming.it is defined by the c language


Data structure algorithms using C?

array,linklist,queue,stack,tree,graph etc...


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


How do you create modules in c language?

by using structure in c.........


Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue&lt;Item&gt; reverse (Queue&lt;Item&gt; queue) { Stack&lt;Item&gt; stack; Item item; while (queue.remove(&amp;item)) { stack.push(item); } while(stack.pop(&amp;item)) { queue.add(item); } return queue; }

Related questions

Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.


C language using list?

C is a programming.it is defined by the c language


Data structure algorithms using C?

array,linklist,queue,stack,tree,graph etc...


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


How do you create modules in c language?

by using structure in c.........


Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue&lt;Item&gt; reverse (Queue&lt;Item&gt; queue) { Stack&lt;Item&gt; stack; Item item; while (queue.remove(&amp;item)) { stack.push(item); } while(stack.pop(&amp;item)) { queue.add(item); } return queue; }


In Which language Oracle Developed?

Oracle is developed using C language...


Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP


How do you draw Logic Gates in C language?

The C language is not a graphics language and you cannot draw logic gates using it. C is a programming language, and it is possible to use a graphics library to do so, but you did not specify which library you were using. Please restate the question.


Has C got a built-in queue library?

No.


A program for queue ADT by using arrays in c plus plus?

Arrays are not suitable for implementing queues because while they are ideal for adding to the end, the are not ideal for extraction from the beginning. For that you need a deque. Regardless, the STL (standard template library) already provides an efficient queue ADT in std::queue.


What are the disadvantages of using c?

There is basically no disadvantage of using a language like C. It depends on the programmer how he uses his skills.