Question

In: Computer Science

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.

Solutions

Expert Solution

code:

#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node* next;
Node* prev;
};

class Deque
{
private:
Node* front;
Node* rear;
int count;

public:
Deque()
{
front = NULL;
  
  
rear = NULL;
  
       count = 0;
}
bool isEmpty()
{
if(count == 0)
       return true;
       else
return false;
}
int DeleteLeft()
{
if ( isEmpty() ) {
cout<<"deque is empty... can't delete'";
return -1;
}

int ret = front->data;

Node* tmp = front;
if ( front->next != NULL )
{
front = front->next;
front->prev = NULL;
}
else
{
front = NULL;
}
count--;
delete tmp;

return ret;
}

void InsertRight(int element)
{
Node* tmp = new Node();
tmp->data = element;
tmp->next = NULL;
tmp->prev = NULL;

if ( isEmpty() ) {
front = rear = tmp;
}
else {
rear->next = tmp;
tmp->prev = rear;
rear = tmp;
}

count++;
}


  
};

int main()
{
Deque q;
if ( q.isEmpty() )
{
cout << "Deque is empty" << endl;
}
q.InsertRight(100);
q.InsertRight(200);
q.InsertRight(300);


  
Deque q1;
if ( q1.isEmpty() )
{
cout << "Deque is empty" << endl;
}

q1.InsertRight(100);
q1.InsertRight(200);
q1.InsertRight(300);


cout << q1.DeleteLeft() << endl;
cout << q1.DeleteLeft() << endl;
cout << q1.DeleteLeft() << endl;

}


Related Solutions

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
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...
Identify which sets of quantum numbers are valid for an electron. Each set is ordered (...
Identify which sets of quantum numbers are valid for an electron. Each set is ordered ( n,ℓ, m ℓ , m s ). Check all that apply. Check all that apply. 2,2,-1, 1/2 1,3,0,1/2 3,2,-2,1/2 4,3,4,-1/2 1,0,0,1/2 4,3,3,-1/2 3,2,0,1/2 2,0,0,-1/2 0,2,1,1/2 1,-1,-1,-1/2 3,2,1,0 1, Part B Identify the sets of quantum numbers that describe all the electrons in the ground state of a neutral beryllium atom, Be . Each set is ordered (n,ℓ,mℓ,ms ). Drag the appropriate items to their...
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for...
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for an electron in an atom? Check all that apply. 3, 1, -2, -1/2.....2, 4, 1, -1/2.....2, 1, 0, -1.....5, 3, 4, 1/2.....4, 2, 1, -1/2.....-4, 3, 1, 1/2..... 4, 2, -2, 1/2..... 2, 1, 0, 1/2
Part C Which of the following set of quantum numbers (ordered n , ℓ , mℓ...
Part C Which of the following set of quantum numbers (ordered n , ℓ , mℓ , ms ) are possible for an electron in an atom? Check all that apply. a) 5, 2, 1, -1/2 B) 3, 3, 1, -1/2 C) 4, 2, 1, -1/2 D) 3, 2, 2, -1/2 E) 3, 2, -3, 1/2 F) 2, 1, 0, 1 G) 4, 3, 4, -1/2 H) -1, 0, 0, -1/2
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for...
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for an electron in an atom? Select all that are possible. 4, 3, -2, -1/2 4, 1, +2, +1/2 1, 0, 0, 0 3, 2, -2, -1/2 2, 1, 0, +1
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for...
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for an electron in an atom? Check all that apply. Check all that apply. 5, 3, 0, 1/2 -3, 2, 2, -1/2 4, 2, -1, -1/2 3, 1, -2, -1/2 5, 3, 4, 1/2 2, 1, 0, 1 2, 4, 1, -1/2 5, 3, -3, 1/2
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for...
Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for an electron in an atom? Check all that apply. 3, 3, 1, -1/2 4, 2, -2, 1/2 -3, 2, 2, -1/2 3, 1, -2, -1/2 4, 2, -1, -1/2 5, 3, 0, 1/2 4, 2, 3, -1/2 2, 1, 0, 1
Set out two circumstances in which it may be necessary to complete financial statements from incomplete...
Set out two circumstances in which it may be necessary to complete financial statements from incomplete records and discuss the risks involved in preparing these financial statements.
Part C Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are...
Part C Which of the following set of quantum numbers (ordered n, ℓ, mℓ, ms) are possible for an electron in an atom? Check all that apply. Hints Check all that apply. 2, 1, -2, 1/2 3, 2, 0, -2 2, 2, 2, 1/2 4, 2, -1, -1/2 5, 3, 4, 1/2 -1, 0, 0, -1/2 4, 3, -2, 1/2 3, 1, 0, -1/2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT