Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the main file, what would stack.h , stack.cpp , queue.h , and queue.cpp look like? stack and queue classes have to include... constructor, destructor,

Given the main file, what would stack.h, stack.cpp, queue.h, and queue.cpp look like?

stack and queue classes have to include...constructor, destructor, copy constructor, overloaded assignment operator

This is what I have so far, but its not working:

main.cpp

#include

#include "stack.h"

#include "queue.h"

using namespace std;

void testStack();

// tests all stack package methods

void testQueue();

// tests all queue package methods

int main() {

cout << "test stack...";

testStack();

cout << endl;

cout << "test queue...";

testQueue();

cout << endl;

return 0;

}

void testStack() {

int num;

// constructor

Stack s1(50);

// constructor with size

Stack s2(3);

// initial sizes

cout << "The initial size of stack number one is " << s1.size() << endl;

cout << "The initial size of stack number two is " << s2.size() << endl;

// push to stack one

cout << " Pushing 4 values onto stack, s1 (of capacity 50) " << endl;

for (int i = 0; i < 4; i++)

s1.push(i);

cout << "size of s1 after the values have been pushed is " << s1.size() << endl;

// copy constructor (s1 -->s3)

cout << "Using copy constructor-->Copying s1 into s3 " << endl;

Stack s3(s1);

//same size as s1

cout << "size of s3 is " << s3.size() << endl;

// peek/pop from s1 until empty

cout << "Popping all values from s1 " << endl;

while (!s1.empty()) {

num = s1.peek();

s1.pop();

cout << "popped " << num << " from s1" << endl;

}

cout << "size of s1 (after all values popped) is " << s1.size() << endl;

// peek/pop from s3 until empty

cout << " Popping all values from s3 (same values as s1) " << endl;

while (!s3.empty()) {

num = s3.peek();

s3.pop();

cout << "popped " << num << " from s3" << endl;

}

cout << "size of s3 (after all values poppped) is " << s3.size() << endl;

// resize s2

cout << " Pushing 5 values onto stack s2 (of capacity 3) " << endl;

for (int i = 0; i < 5; i++)

s2.push(i);

cout << "size of s2 (after pushing 4 values) is " << s2.size() << endl;

// assignment operator (s2 into s1)

cout << "Copying s2 into s1 (using assignment operator) " << endl;

s1 = s2;

cout << "size of s1 (same size as s2) is " << s1.size() << endl;

// peek/pop from s2 until empty

cout << " Popping all values from s2 " << endl;

while (!s2.empty()) {

num = s2.peek();

s2.pop();

cout << "popped " << num << " from s2" << endl;

}

cout << "size of s2 (after all values popped) is " << s2.size() << endl;

// peek/pop from s1 until empty

cout << " Popping all values from s1 (same values as s2) " << endl;

while (!s1.empty()){

num = s1.peek();

s1.pop();

cout << "popped " << num << " from s1" << endl;

}

cout << "size of s1 (after all values popped) is " << s1.size() << endl;

}

void testQueue() {

int num;

// constructor

Queue q1, q2;

// initial sizes

cout << "initial size of q1 is " << q1.size() << endl;

cout << "initial size of q2 is " << q2.size() << endl;

// enqueue to q1

cout << " -> Enqueuing 5 values to queue, q1" << endl;

for (int i = 3; i < 8; i++)

q1.enqueue(i);

cout << "size of q1 (after pushing 5 values) is " << q1.size() << endl;

// copy constructor (q1 into q3)

cout << " -> Copying q1 into q3 (using copy constructor)" << endl;

Queue q3(q1);

cout << "size of q3 (same size as q1) is " << q3.size() << endl;

// peek/dequeue from q1 until empty

cout << " -> Emptying all values from q1" << endl;

while (!q1.empty()) {

num = q1.peek();

q1.dequeue();

cout << "dequeued " << num << " from q1" << endl;

}

cout << "size of q1 (after all values dequeued) is " << q1.size() << endl;

// assignment operator

cout << "Copying q3 into q2 (using assignment operator)" << endl;

q2 = q3;

cout << "size of q2 (same size as q3) is " << q2.size() << endl;

// peek/dequeue from q3 until empty

cout << "Emptying all values from q3" << endl;

while (!q3.empty()) {

num = q3.peek();

q3.dequeue();

cout << "dequeued " << num << " from q3" << endl;

}

cout << "size of q3 (after all values dequeued) is " << q3.size() << endl;

// peek/dequeue from q2 until empty

cout << "Emptying all values from q2" << endl;

while (!q2.empty()) {

num = q2.peek();

q2.dequeue();

cout << "dequeued " << num << " from q2" << endl;

}

cout << "size of q2 (after all values dequeued) is " << q2.size() << endl;

}

also stack should include:

#ifndef STACK_H #define STACK_H class Stack { public: Stack(int); Stack(const Stack &); Stack& operator=(const Stack &); ~Stack(); void push(int); void pop(); int peek() const; bool empty() const; int size() const; private: int *arr; int capacity; int top; void resize(); }; #endif 

and queue should include:

#ifndef QUEUE_H  #define QUEUE_H  class Queue { public: Queue(); Queue(const Queue &); Queue& operator=(const Queue &); ~Queue(); void enqueue(int); void dequeue(); int peek() const; bool empty() const; int size() const; private: struct Node { int value; Node *next; }; Node *front; Node *rear; int numItems; }; #endif  

stack.cpp

//stack.cpp

//holds the functions that push and pop and print

//also where stack is defined/made

#include"stack.h"

#include

using namespace std;

Stack::Stack()

{

buffer = nullptr;

numElements = 0;

topIndex = -1;

}

Stack::~Stack()

{

delete[] buffer;

}

bool Stack::empty() const

{

return topIndex;

}

unsigned int Stack::size() const

{

return numElements;

}

int& Stack::top()

{

return buffer[topIndex];

}

const int& Stack::top() const

{

int &data= buffer[topIndex];

return data;

}

void Stack::push(const int& t)

{

int *temp = new int[numElements + 1];

for (unsigned int i = 0; i < numElements; i++)

temp[i] = buffer[i];

temp[numElements] = t;

numElements++;

topIndex++;

delete[]buffer;

buffer = temp;

}

void Stack::pop()

{

if (!empty())

{

int *temp = new int[numElements - 1];

for (unsigned int i = 0; i < numElements-1; i++)

temp[i] = buffer[i];

numElements--;

topIndex--;

delete[]buffer;

buffer = temp;

}

}

void Stack::print()

{

cout << "[ ";

for (unsigned int i = 0; i < numElements; ++i)

{

cout << buffer[i] << " ";

}

cout << "]" << endl;

}

stack.h

//stack.h

//Creates the stacks and seperates between public and private

#ifndef STACK_H

#define STACK_H

#include

using namespace std;

class Stack

{

public:

class EmptyStack{};

// exception class thrown when attempt made to pop from empty stack

explicit Stack(int size = 50);

// constructor

Stack(const Stack& s);

// copy constructor

Stack& operator=(const Stack& rhs);

// overloaded assignment operator

~Stack();

// destructor

void push(int data);

// adds data to stack

int pop();

// removes and returns data from stack

// can throw EmptyStack exception

bool isEmpty() const;

// returns status of stack

private:

int* list; // stack

int max;// size of stack

int top;// top of stack

bool full() const;

// returns status of stack

void resize();

// allocates more stack memory

};

#endif

queue.h

//queue.h

//

#ifndef QUEUE_H

#define QUEUE_H

class Queue

{

public:

class EmptyQueue{};

// exception class thrown when an attempt is made to dequeue from an

// empty queue

Queue();

// constructor

Queue(const Queue& aQueue);

// copy constructor

Queue& operator=(const Queue& rhs);

// overloaded assignment operator

~Queue();

// destructor

void enqueue(int data);

// adds data to queue

int dequeue();

// removes and returns first element from queue

// could throw EmptyQueue exception

bool isEmpty() const;

// returns state of queue

private:

struct QNode{

int d;

QNode* next;

};

QNode* front;

QNode* tail;

void copyList(QNode* list);

// makes a copy of list on this.front

};

#endif

queue.cpp

//queue.cpp

//creates queue functions and helps modify nodes

#include"queue.h"

#include

using namespace std;

Queue::Queue()

{

head = NULL;

tail = NULL;

numElements = 0;

}

Queue::~Queue()

{

Node *temp;

temp = head;

while (head != NULL)

{

delete temp;

temp = head->next;

head = head->next;

}

tail = NULL;

numElements = 0;

}

bool Queue::empty() const

{

return numElements == 0;

}

unsigned int Queue::size() const

{

return numElements;

}

void Queue::pop()

{

if (head == NULL)

return;

if (head == tail)

{

Node *temp = head;

delete temp;

head = tail = NULL;

numElements--;

return;

}

Node *temp = head;

head = head->next;

delete temp;

numElements--;

}

void Queue::push(const int& x)

{

Node *newNode = new Node();

newNode->element = x;

newNode->next = NULL;

if (head == NULL)

head = tail = newNode;

else

{

tail->next = newNode;

tail = newNode;

}

numElements++;

}

int& Queue::front()

{

return head->element;

}

const int& Queue::front() const

{

int &element = head->element;

return element;

}

int& Queue::back()

{

return tail->element;

}

const int& Queue::back() const

{

int &element = head->element;

return element;

}

void Queue::print()

{

cout << "[ ";

for (Node* cur = head; cur != NULL; cur = cur->next)

{

cout << cur->element << " ";

}

cout << "]" << endl;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions