For this assignment, you will use your knowledge of the static implementations o
ID: 3597303 • Letter: F
Question
For this assignment, you will use your knowledge of the static implementations of stacks and queues to create our own version of a double-ended queue, known as a "deque". We discussed multiple algorithms for pushing/popping and enqueuing/dequeueing. Use whichever approach you prefer.
Submit your solution in a single header file named MyIntDeque.h. Do not submit screenshots. Do not submit a driver program. Submit only the header file.
Use the following UML diagram and attribute description for your implementation.
Attributes:
Note: the above was edited for clarity.
Hints:
Find here below the driver.cpp for this assignment
i need help thanks
Explanation / Answer
//MyIntDeque.h
#include<iostream>
using namespace std;
class MyIntDeque
{
int *IntArray;
int capacity;
int length;
int Qfront;
int Qback;
public:
MyIntDeque(int cap);
void push_back(int val);
void push_front(int val);
void pop_back();
void pop_front();
int front();
int back();
int size();
bool isFull();
bool isEmpty();
void clear();
~MyIntDeque();
void print();
};
------------------------------------------------------------------------------------------------
//MyIntDeque.cpp
#include"MyIntDeque.h"
MyIntDeque::MyIntDeque(int cap)
{
IntArray = new int[cap];
capacity = cap;
length = 0;
Qfront = 0;
Qback = 0;
}
void MyIntDeque::push_back(int val)
{
if (!isFull())
{
IntArray[Qback++] = val;
++length;
}
else
cout << "Queue full" << endl;
}
void MyIntDeque::print()
{
for (int i = 0; i < length; i++)
cout << IntArray[i] << " ";
cout << endl;
}
void MyIntDeque::push_front(int val)
{
int tmp;
if (!isFull())
{
for (int k = length; k > 0; k--)
{
IntArray[k] = IntArray[k - 1];
}
IntArray[Qfront] = val;
++Qback;
++length;
}
}
void MyIntDeque::pop_back()
{
int ret = IntArray[--Qback];
--length;
}
void MyIntDeque::pop_front()
{
int ret = IntArray[0];
--length;
--Qback;
for (int i = 0; i < length; i++)
IntArray[i] = IntArray[i + 1];
}
int MyIntDeque::front()
{
return IntArray[Qfront];
}
int MyIntDeque::back()
{
return IntArray[Qback-1];
}
int MyIntDeque::size()
{
return length;
}
bool MyIntDeque::isFull()
{
if (length == capacity)
return true;
else
return false;
}
bool MyIntDeque::isEmpty()
{
if (length ==0 && Qback ==0&& Qfront == 0)
return true;
else
return false;
}
void MyIntDeque::clear()
{
for (int i = 0; i <= length; i++)
{
if (!isEmpty())
{
pop_back();
}
}
}
MyIntDeque::~MyIntDeque()
{
delete[] IntArray;
}
--------------------------------------------------------------------------
//main.cpp
#include "MyIntDeque.h"
using namespace std;
/*using std::cout;
using std::cin;
using std::endl;*/
int getNumber();
int main()
{
MyIntDeque m(5);
char choice;
int num;
do
{
cout << " 1 - push_back "
<< "2 - push_front "
<< "3 - pop_back "
<< "4 - pop_front "
<< "5 - size "
<< "6 - front "
<< "7 - back "
<< "8 - isFull "
<< "9 - isEmpty "
<< "0 - clear "
<< "Q - quit "
<< " > ";
cin >> choice;
cin.ignore(1000, 10);
cout << " ";
m.print();
switch (choice)
{
case '1':
num = getNumber();
m.push_back(num);
break;
case '2':
num = getNumber();
m.push_front(num);
break;
case '3':
m.pop_back();
break;
case '4':
m.pop_front();
break;
case '5':
cout << "Size of deque: " << m.size() << endl;
break;
case '6':
cout << "Front of deque: " << m.front() << endl;
break;
case '7':
cout << "Back of deque: " << m.back() << endl;
break;
case '8':
cout << "Deque " << (m.isFull() ? "is" : "isn't") << " full. ";
break;
case '9':
cout << "Deque " << (m.isEmpty() ? "is" : "isn't") << " empty. ";
break;
case '0':
m.clear();
break;
case 'Q':
case 'q':
break;
//default:
}
} while (choice != 'Q' && choice != 'q');
}
int getNumber()
{
int num;
cout << "Enter a number to store in the deque: ";
cin >> num;
cin.ignore(1000, 10);
return num;
}
---------------------------------------------------------------------------
//I have added print function to the class so that I can check array elements after each operation,, I am calling this.print function in main ,, u can comment out in main as well in header and MyIntDeque.cpp file Below is the output
> 1
1
Enter a number to store in the deque: 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 1
1 2
Enter a number to store in the deque: 3
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 2
1 2 3
Enter a number to store in the deque: 4
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 2
4 1 2 3
Enter a number to store in the deque: 5
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 5
5 4 1 2 3
Size of deque: 5
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 6
5 4 1 2 3
Front of deque: 5
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 7
5 4 1 2 3
Back of deque: 3
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 3
5 4 1 2 3
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 5
5 4 1 2
Size of deque: 4
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 7
5 4 1 2
Back of deque: 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 6
5 4 1 2
Front of deque: 5
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 4
5 4 1 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 5
4 1 2
Size of deque: 3
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 6
4 1 2
Front of deque: 4
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 7
4 1 2
Back of deque: 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
>
-------------------------------
//one more execution output
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 1
Enter a number to store in the deque: 1
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 1
1
Enter a number to store in the deque: 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 0
1 2
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 5
Size of deque: 0
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
> 5
Size of deque: 0
1 - push_back
2 - push_front
3 - pop_back
4 - pop_front
5 - size
6 - front
7 - back
8 - isFull
9 - isEmpty
0 - clear
Q - quit
>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.