Question

In: Computer Science

Study and implement data structure deque (double ended queue, pronounced as "deck"). IN C++ PLEASE

Study and implement data structure deque (double ended queue, pronounced as "deck").

IN C++ PLEASE

Solutions

Expert Solution

#include<iostream>

using namespace std;

#define MAX 100

class Deque

{

  int arr[MAX];

  int front;

  int rear;

  int size;

public :

  Deque(int size)

  {

    front = -1;

    rear = 0;

    this->size = size;

  }

  void insertfront(int key);

  void insertrear(int key);

  void deletefront();

  void deleterear();

  bool isFull();

  bool isEmpty();

  int getFront();

  int getRear();

};

bool Deque::isFull()

{

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

      front == rear+1);

}

bool Deque::isEmpty ()

{

  return (front == -1);

}

void Deque::insertfront(int key)

{

  if (isFull())

  {

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

    return;

  }

  if (front == -1)

  {

    front = 0;

    rear = 0;

  }

  else if (front == 0)

    front = size - 1 ;

  else

    front = front-1;

  arr[front] = key ;

}

void Deque ::insertrear(int key)

{

  if (isFull())

  {

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

    return;

  }

  if (front == -1)

  {

    front = 0;

    rear = 0;

  }

  else if (rear == size-1)

    rear = 0;

  else

    rear = rear+1;

  arr[rear] = key ;

}

void Deque ::deletefront()

{

  if (isEmpty())

  {

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

    return ;

  }

  if (front == rear)

  {

    front = -1;

    rear = -1;

  }

  else

    if (front == size -1)

      front = 0;

    else

      front = front+1;

}

void Deque::deleterear()

{

  if (isEmpty())

  {

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

    return ;

  }

  if (front == rear)

  {

    front = -1;

    rear = -1;

  }

  else if (rear == 0)

    rear = size-1;

  else

    rear = rear-1;

}

int Deque::getFront()

{

  if (isEmpty())

  {

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

    return -1 ;

  }

  return arr[front];

}

int Deque::getRear()

{

  if(isEmpty() || rear < 0)

  {

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

    return -1 ;

  }

  return arr[rear];

}


Related Solutions

A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports...
A deque (short for double-ended queue, but pronounced “deck”) is an abstract data type that supports adding and removing at both the front and back. So, at a bare minimum, a deque has four operations: addFront(), removeFront(), addBack(), removeBack(). Suppose you have a deque D containing the numbers (1, 2, 3, 4, 5, 6, 7, 8), in this order. Suppose further that you have an initially empty queue Q. Give pseudo-code description of a method that uses only D and...
1. A double-ended queue, or deque, is a data structure consisting of a list of items...
1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x): insert item x on the back end of the queue addToFront(x): insert item x on the front end of the queue getBack(): returns the element on the back end of the queue getFront(): returns the element on the front end of the queue removeBack(): remove the back item from the queue removeFront(): remove the front item...
A deque (pronounced deck) is an ordered set of items from which items may be deleted...
A deque (pronounced deck) is an ordered set of items from which items may be deleted at either end and into which items may be inserted at either end. Call the two ends left and right. This is an access-restricted structure since no insertions or deletions can happen other than at the ends. Implement a deque as a doubly-linked circular list with a header. Write InsertRight and DeleteLeft.
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is...
Create a Deque class based on the following description of deque (double-ended queues). A dequeue is a double-ended queue. You can insert items at either end and delete them from either end. Therefore, the deque offers two insert operations and two remove operations: insertLeft() and insertRight() removeLeft() and removeRight(). Deques may also have "peek" methods ( peekLeft() and peekRight() )that let you see the left or right item in the deque without remove the item from the deque. For this...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return empty queue // enqueue and dequeue each return an int error code int enqueue(int, Queue *) int dequeue(Queue *, int *) int getQsize(Queue *) // returns # of items in queue void freeQueue(Queue *) // Free *all* space Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time. You will need to document each of your functions to...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Solve in C++ program. Modify the use of queue data structure such that the array used...
Solve in C++ program. Modify the use of queue data structure such that the array used to implement the queue is dynamically allocated for a fast food autoservice
A deque is a data structure consisting of a list of items, on which the following...
A deque is a data structure consisting of a list of items, on which the following operations are possible: • push(x): Insert item x on the front end of the deque. • pop(): Remove the front item from the deque and return it. • inject(x): Insert item x on the rear end of the deque. • eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT