# Double Ended Queue or Deque

- Link: https://technotaught.com/double-ended-queue/
- Published: 2018-11-10T20:10:16+05:30
- Author: Ashishkumar Vishwakarma

A Deque or deck is a Double Ended Queue. Allows elements to be added or removed 
on either the ends. Deque is a generalized version of a Queue data structure that
allows insert and delete at both ends.

## Operation in Double Ended Queue

 * Insert an element at the back
 * Insert an element at the front
 * Remove element at front
 * Remove element at the back

### Graphical Representation.

![Double Ended Queue](https://technotaught.com/wp-content/uploads/2018/11/download.
png)

## Program for Deque.

view more:-[Implementation of Quick Sort| Merge Sort| Heap Sort Algorithm.](https://technotaught.com/8-implementation-of-quick-sort-merge-sort-heap-sort-algorithm/#.Xghkf0czY2w)

### Source code.

    ```c
    	/* dequeue*/
    	#include<stdio.h>
    	#include<stdlib.h>
    	struct node
    	{
    		int data;
    		struct node *addr;
    	};
    	struct node *rear,*head;

    	void fdel();
    	void rdel();
    	void ins(int);
    	void disp();

    	int main()
    	{
    		ins(10);	
    		ins(20);
    		ins(30);
    		ins(40);
    		printf("Delete element from last\n");
    		rdel(); //delete element from last
    		rdel(); //delete elemeny from last
    		printf("Delete element from last\n");
    		fdel(); //delete element fron first
    		fdel();//delete element fron first
    		fdel();
    	}
    void ins (int val)
    	{
    		struct node *temp;
    		temp=(struct node *)malloc(sizeof(struct node));
    		temp->data=val;
    		temp->addr=NULL;
    		if (head==NULL)
    		{
    			head=temp;
    			rear=temp;
    		}
    		else 
    		{
    			rear->addr=temp;
    			rear=temp;
    		}
    		disp();
    	}
    void disp()
    	{
    		struct node *i=head;
    		printf("queue: ");
    		while(i!=NULL)
    		{
    		printf("%d ",i->data);
    		i=i->addr;
    		}	
    		printf("\n  ");
    	}

    	void fdel()
    	 {
    	//if queue is empty
    	if(head==NULL)
    		printf("Error:empty\n");
    	//else
    	else
    	{	
    	//more than one node exist
    	head=head->addr;
    	//only one node exist
    	if(head==NULL)
    		rear=NULL;
    	}
    	//display
    	disp();
    	}
    	void rdel()
    	{
    		struct node *i=head;
    		if(head!=NULL)
    		{
    			while(i->addr!=rear)
    			{
    				i=i->addr;
    			}
    			i->addr=NULL;
    			rear=i;
    			if(rear==NULL)
    			{
    				head=NULL;
    			}
    			disp();
    		}
    	}	/* dequeue*/
    ```

 

### Output.

> queue: 10
> queue: 10 20queue:10 20 30queue: 10 20 30 40Delete element from lastqueue:
> 10 20 30queue: 10 20Delete element from lastqueue: 20queue:Error:emptyqueue:

Source:-[greekforgreek](https://www.geeksforgeeks.org/deque-set-1-introduction-applications/)

### Share this:

 * [  X ](https://technotaught.com/double-ended-queue/?share=twitter)
 * [  Facebook ](https://technotaught.com/double-ended-queue/?share=facebook)
 * [More](https://technotaught.com/double-ended-queue/#)

 * [  WhatsApp ](https://technotaught.com/double-ended-queue/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/double-ended-queue/?share=linkedin)
 * [  Email ](https://technotaught.com/double-ended-queue/?subject=%5BShared%20Post%5D%20Double%20Ended%20Queue%20or%20Deque&body=https%3A%2F%2Ftechnotaught.com%2Fdouble-ended-queue%2F&share=email)
 * [  Reddit ](https://technotaught.com/double-ended-queue/?share=reddit)
 * [  Tumblr ](https://technotaught.com/double-ended-queue/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/double-ended-queue/?share=pinterest)
 * [  Telegram ](https://technotaught.com/double-ended-queue/?share=telegram)
 * [  Print ](https://technotaught.com/double-ended-queue/#print?share=print)

### _Related_
