Question

A deque (pronounced “deck”) is like a queue, except that items may be added and removed...

A deque (pronounced “deck”) is like a queue, except that items may be added

and removed from both the front and the rear. Write either an array-based or

linked implementation for the deque. in C++ PLEASE SHOW A PIC OF THE OUTPUT

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

   

#define MAX_size 10     // Maximum size of array or Dequeue

   

// Deque class

class Deque

{

int array[MAX_size];

int front;

int rear;

int size;

public :

Deque(int size) {

front = -1;

rear = 0;

this->size = size;

    }

   

    // Operations on Deque:

void insertfront(int key);

void insertrear(int key);

void deletefront();

void deleterear();

int getFront();

int getRear();

     

    // Check if Deque is full

bool isFull(){

return ((front == 0 && rear == size-1)||front == rear+1);   

    }

    // Check if Deque is empty

bool isEmpty(){

return (front == -1);

    }

};

   

// Insert an element at front of the deque

void Deque::insertfront(int key)

{

if (isFull()) {

cout << "Overflow!!\n" << endl;

return;

    }

   

    // If queue is initially empty,set front=rear=0; start of deque

if (front == -1) {

front = 0;

rear = 0;

    }

else if (front == 0)              // front is first position of queue

front = size - 1 ;

else // decrement front 1 position

front = front-1;

   

array[front] = key ;            // insert current element into Deque

}

   

// insert element at the rear end of deque

void Deque ::insertrear(int key)

{

if (isFull()) {

cout << " Overflow!!\n " << endl;

return;

    }

   

    // If queue is initially empty,set front=rear=0; start of deque

if (front == -1) {

front = 0;

rear = 0;

    }

else if (rear == size-1)               // rear is at last position of queue

rear = 0;

else                            // increment rear by 1 position

rear = rear+1;

   

array[rear] = key ;         // insert current element into Deque

}

   

// Delete element at front of Deque

void Deque ::deletefront()

{

if (isEmpty())

   {

cout << "Queue Underflow!!\n" << endl;

return ;

    }

   

    // Deque has only one element

if (front == rear)

    {

front = -1;

rear = -1;

    }

else

        // back to initial position

if (front == size -1)

front = 0;

   

else // remove current front value from Deque;increment front by 1

front = front+1;

}

   

// Delete element at rear end of Deque

void Deque::deleterear()

{

if (isEmpty())

    {

cout << " Underflow!!\n" << endl ;

return ;

    }

   

    // Deque has only one element

if (front == rear)

    {

front = -1;

rear = -1;

    }

else if (rear == 0)

rear = size-1;

else

rear = rear-1;

}

// retrieve front element of Deque

int Deque::getFront()

{

if (isEmpty())   {

cout << " Underflow!!\n" << endl;

return -1 ;

    }

return array[front];

}

   

// retrieve rear element of Deque

int Deque::getRear()

{

if(isEmpty() || rear < 0) {

cout << " Underflow!!\n" << endl;

return -1 ;

    }

return array[rear];

}

   

//main program

int main()

{

    Deque dq(5);

cout << "Insert element 1 at rear end \n";

dq.insertrear(1);

   

cout << "insert element 3 at rear end \n";

dq.insertrear(3);

   

cout << "rear element of deque " << " " << dq.getRear() << endl;

   

dq.deleterear();

cout << "After deleterear, rear = " << dq.getRear() << endl;

   

cout << "inserting element 5 at front end \n";

dq.insertfront(5);

   

cout << "front element of deque " << " "

<< dq.getFront() << endl;

   

dq.deletefront();

   

cout << "After deletefront, front = " << dq.getFront() << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
A deque (pronounced “deck”) is like a queue, except that items may be added and removed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Deque) A deque is a data structure consisting of a list of items on which the...

    (Deque) A deque is a data structure consisting of a list of items on which the following operations are possible: a. push (x) : Insert item x on the front end of the deque. b. pop ): Remove the front item from the deque and return it. c. inject(x): Insert item x on the rear end of the deque. d. eject ): Remove the rear item from the deque and return it. Write routines to support the deque that take...

  • Write only function. In c++ 3. a) Write a function, to be included in a queue...

    Write only function. In c++ 3. a) Write a function, to be included in a queue class - circular array implementation (20 p that will print all the items in the queue from front to rear

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...

    Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1" << endl; dq1.insertFront(42); dq1.insertBack(216); cout << dq1.peekFront() << " - 42" << endl; cout << dq1.peekBack() << " - 216" << endl; cout << dq1.size() << " - 2" << endl; Deque dq2(dq1); Deque dq3; dq3 = dq1; cout << dq1.removeFront() << " - 42" << endl; cout << dq1.removeBack() << " - 216" << endl; cout << dq2.peekFront() << " - 42" <<...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • Write a program: Write all the code for a data-structure that can act like a queue...

    Write a program: Write all the code for a data-structure that can act like a queue or a stack. Items can be pushed/popped from the front and pushed/popped from the back. Put 10 numbers into the structure. Then take it out so you can print it backwards. Then, do the same thing, but print it forward. What is the name of this structure? (Use a stl linked list for the internals) C++ Language

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT