Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

Double ended queue is a more generalized form of queue data structure which allows insertion and removal of elements from both the ends, i.e , front and back.
Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

Implementation of Double ended Queue

Here we will implement a double ended queue using a circular array. It will have the following methods:

  • push_back: inserts element at back
  • push_front: inserts element at front
  • pop_back: removes last element
  • pop_front: removes first element
  • get_back: returns last element
  • get_front: returns first element
  • empty: returns true if queue is empty
  • full: returns true if queue is full

// Maximum size of array or Dequeue
#define SIZE 5
class Dequeue
{
    //front and rear to store the head and tail pointers
    int  *arr;
    int front, rear;
    public :
    Dequeue()
    {
        //Create the array
        arr = new int[SIZE];
//Initialize front and rear with -1
        front = -1;
        rear = -1;
    }
    // Operations on Deque
    void  push_front(int );
    void  push_back(int );
    void  pop_front();
    void  pop_back();
    int  get_front();
    int  get_back();
    bool  full();
    bool  empty();
};

Insert Elements at Front

First we check if the queue is full. If its not full we insert an element at front end by following the given conditions:
If the queue is empty then intialize front and rear to 0. Both will point to the first element.

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)Else we decrement front and insert the element. Since we are using circular array, we have to keep in mind that if front is equal to 0 then instead of decreasing it by 1 we make it equal to SIZE-1.
Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)void Dequeue :: push_front(int key)
{
    if(full())
    {
        cout << "OVERFLOW\n";
    }
    else
    {
        //If queue is empty
        if(front == -1)
            front = rear = 0;
        //If front points to the first position element
        else if(front == 0)
             front = SIZE-1;
        else
            --front;
        arr[front] = key;
    }
}

Insert Elements at back


Again we check if the queue is full. If its not full we insert an element at back by following the given conditions:

  • If the queue is empty then intialize front and rear to 0. Both will point to the first element.
  • Else we increment rear and insert the element. Since we are using circular array, we have to keep in mind that if rear is equal to SIZE-1 then instead of increasing it by 1 we make it equal to 0.

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

void Dequeue :: push_back(int key)
{
    if(full())
    {
        cout << "OVERFLOW\n";
    }
    else
    {
        //If queue is empty
           if(front == -1)
              front = rear = 0;
           //If rear points to the last element
        else if(rear == SIZE-1)
            rear = 0;
        else
            ++rear;
        arr[rear] = key;
    }
}

Delete First Element

In order to do this, we first check if the queue is empty. If its not then delete the front element by following the given conditions :

  • If only one element is present we once again make front and rear equal to -1.
  • Else we increment front. But we have to keep in mind that if front is equal to SIZE-1 then instead of increasing it by 1 we make it equal to 0.

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

void Dequeue :: pop_front()
{
    if(empty())
    {
        cout << "UNDERFLOW\n";
    }
    else
    {
        //If only one element is present
        if(front == rear)
            front = rear = -1;
        //If front points to the last element
        else if(front == SIZE-1)
            front = 0;
        else
            ++front;        
    }
}

Delete Last Element

Inorder to do this, we again first check if the queue is empty. If its not then we delete the last element by following the given conditions :

  • If only one element is present we make front and rear equal to -1.
  • Else we decrement rear. But we have to keep in mind that if rear is equal to 0 then instead of decreasing it by 1 we make it equal to SIZE-1.

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

void Dequeue :: pop_back()
{
    if(empty())
    {
        cout << "UNDERFLOW\n";
    }
    else
    {
        //If only one element is present
        if(front == rear)
            front = rear = -1;
       //If rear points to the first position element
        else if(rear == 0)
            rear = SIZE-1;
        else
            --rear;
    }
}

Check if Queue is empty

It can be simply checked by looking where front points to. If front is still intialized with -1, the queue is empty.
bool Dequeue :: empty()
{
    if(front == -1)
        return true;
    else
        return false;
}

Check if Queue is full

Since we are using circular array, we check for following conditions as shown in code to check if queue is full.
bool Dequeue :: full()
{
    if((front == 0 && rear == SIZE-1)  ||
        (front == rear + 1))
        return true;
    else
        return false;
}

Return First Element

If the queue is not empty then we simply return the value stored in the position which front points.
int Dequeue :: get_front()
{
    if(empty())
    {    cout << "f=" <<front << endl;
        cout << "UNDERFLOW\n";
        return -1;
    }
    else
    {
        return arr[front];
    }
}

Return Last Element

If the queue is not empty then we simply return the value stored in the position which rear points.
int Dequeue :: get_back()
{
    if(empty())
    {
        cout << "UNDERFLOW\n";
        return -1;
    }
    else
    {
        return arr[rear];
    }
}

The document Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Double Ended Queue - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a Double Ended Queue (Deque)?
Ans. A Double Ended Queue, also known as Deque, is a data structure that allows insertion and deletion of elements from both ends. It can be visualized as a queue at both ends or as a doubly linked list.
2. What are the basic operations supported by a Double Ended Queue?
Ans. The basic operations supported by a Double Ended Queue are: - Insertion at the front (enqueueFront) - Insertion at the rear (enqueueRear) - Deletion from the front (dequeueFront) - Deletion from the rear (dequeueRear)
3. How is a Double Ended Queue different from a regular queue?
Ans. A Double Ended Queue differs from a regular queue in the sense that it allows insertion and deletion of elements from both ends, whereas a regular queue only supports insertion at one end and deletion from the other end.
4. What are the advantages of using a Double Ended Queue?
Ans. Some advantages of using a Double Ended Queue include: - It provides flexibility in implementing algorithms that require insertion and deletion from both ends. - It can be used to efficiently implement data structures like stacks, queues, and lists. - It allows easy access to both ends of the queue, making it suitable for certain problem-solving scenarios.
5. In what scenarios is a Double Ended Queue useful?
Ans. A Double Ended Queue can be useful in various scenarios, such as: - Implementing a queue with both FIFO (First-In-First-Out) and LILO (Last-In-Last-Out) functionalities. - Implementing algorithms that require efficient insertion and deletion operations from both ends. - Solving problems that involve maintaining a sliding window of elements, where elements are added or removed from both ends.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

pdf

,

practice quizzes

,

Semester Notes

,

ppt

,

Important questions

,

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

,

Summary

,

mock tests for examination

,

Viva Questions

,

Sample Paper

,

MCQs

,

Exam

,

shortcuts and tricks

,

study material

,

Extra Questions

,

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

,

video lectures

,

Previous Year Questions with Solutions

,

Double Ended Queue | Programming and Data Structures - Computer Science Engineering (CSE)

,

Free

,

past year papers

,

Objective type Questions

;