Question: ////////////////////////////////////////////////////// // Topics: circular arrays, templated classes, dynamic arrays // // Implement a stackQueue using a // circular array implementation as discussed in class. //

////////////////////////////////////////////////////// // Topics: circular arrays, templated classes, dynamic arrays // // Implement a stackQueue using a // circular array implementation as discussed in class. // Your data structure must increase its array size dynamically // if the array runs out of room. // // Abstractly, your stackQueue represents a list of items // with a front and back from which items may be added // or removed. If you choose to add and remove from just // one end, you effectively have a stack. If you choose // to add to the back and remove from the front, (or vice versa) // you effectively have a queue, thus the name "stackQueue". // My code is almost complete but there is some minor erros, please help! // Please add comments and be detailed, thank you! Will rate (: /////////////////////////////////////////////////////// 

#ifndef stackQueue_h

#define stackQueue_h

#include

using namespace std;

template

class stackQueue

{

private:

//declare your array variable and

//any additional variables you need

//to solve the problem

T * items;

int front;

int back;

int capacity;

public:

stackQueue()

{

capacity = 5;

items = new T[capacity];

front = -1;

back = -1;

}

//Insert x to the "back" of the list of items.

void addBack(T x)

{

if (front == -1)

{

front = 0;

back = 0;

items[back] = x;

}

else if (back == capacity - 1)

{

back = 0;

}

if (back == front)

{

back = capacity - 1;

resize();

}

else if (back + 1 == front)

{

resize();

}

else

{

back++;

items[back] = x;

}

}

//Add x to the "front" of the list of items.

void addFront(T x)

{

if (front == -1)

{

front = 0;

back = 0;

items[front] = x;

}

else if (front == 0)

{

if (back == capacity - 1)

{

resize();

}

front = capacity - 1;

items[front] = x;

}

else if (front - 1 == back)

{

resize();

front = capacity - 1;

items[front] = x;

}

else

{

front--;

items[front] = x;

}

}

//Remove and return the item currently at the "back" of the list

T removeBack()

{

int index = back;

if (front == -1)

{

cout << "Empty.." << endl;

return 0;

}

else if (back == front)

{

front = -1;

back = -1;

return items[index];

}

else if (back == 0)

{

back = capacity - 1;

return items[index];

}

else

{

back--;

return items[index];

}

}

//Remove and return the item currently at the "front" of the list

T removeFront()

{

int index = front;

if (front == -1)

{

cout << "Empty.." << endl;

return 0;

}

else if (front == back)

{

front = -1;

back = -1;

return items[index];

}

else if (front == capacity - 1)

{

front = 0;

return items[index];

}

else

{

front++;

return items[index];

}

}

//Is the stackQueue empty?

bool empty()

{

if (front == -1)

{

return true;

}

else

{

return false;

}

}

void resize()

{

int index = 0;

int i = front;

T * temp = new T[capacity * 2];

while (i != back)

{

temp[index] = items[i];

index++;

if (i == capacity - 1)

{

i = 0;

}

else

{

i++;

}

}

temp[index] = items[back];

delete[] items;

items = temp;

capacity = capacity * 2;

front = 0;

back = index;

}

};

#endif /* stackQueue_h */ //********************************************************main file ********************************************************

#include

#include

#include

#include "stackQueue.h"

using namespace std;

int main() {

stackQueue obj;

obj.addBack(1);

obj.addBack(2);

cout << obj.removeBack() << endl; // 2

cout << obj.removeFront() << endl; // 1

obj.addFront(1);

obj.addFront(2);

obj.addFront(3);

cout << obj.removeBack() << endl; // 1

cout << obj.removeBack() << endl; // 2

cout << obj.removeBack() << endl; // 3

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!