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

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++ 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...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
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...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue...
Description( IN C++) NO TAIL POINTERS!! The purpose of this challenge is to implement a queue using a linked list as a backing data structure Requirements Write the following struct struct JobNode { string name; JobNode * next; }; Create a class called Queue. In this class, create a private variable JobNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used to dynamically create new nodes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT