GU FullStack Classes Programming Code
ANSWER
To finish the assignment, you must construct a stack implementation that uses vectors as the container and make numerous changes to the provided code. You should also include exception handling and show how to use STL queue methods. The updated code that satisfies these conditions is shown below:
the cpp
Copy the following code: #include <vector> #include <iostream> #include <queue> #include <stdexcept> As an exception
using the std namespace;
// Custom exceptions class using the StackException class public exception StackException: { string message;
public: }} message(msg) \} StackException(const string& msg)
const throw() const char* what() { return message.c_str(); } };
// Use vector as the container template for stackType (<class Type>).
stack type class { private: vector<Type> list; int stack top; int maxSize;
The stack type is public (int size = 100). {} if (size <= 0) size = 100; // Default size maxSize = size; stackTop = -1; list.resize(maxSize);
In the void initializeStack(), stackTop is set to -1;
return stackTop == maxSize – 1;} bool isFullStack() const {
return stackTop == -1; bool isEmptyStack() const {
push(void, const Type& newItem) If (isFullStack()) is true, then { throw StackException(“Stack overflow: Attempt to push onto a full stack.”);
sequence[stackTop] = newItem; stackTop++;
// Using an overload to push several values onto the stack void push(const vector<Type>& items) { for (const Type& item : items) { push(item);
The void pop() function { if (isEmptyStack()) stackTop–; } throw StackException(“Stack underflow: Attempt to pop from an empty stack.”);
Type top() const { -if (isEmptyStack()) { -throw StackException(“Stack underflow: Attempt to access the top of an empty stack.”);
list[stackTop] be returned;
Const stackType<Type>& otherStack === bool operator Const {} ; stackTop!= otherStack.stackTop; if (stackTop!= otherStack.stackTop) ; return false;
for (i <= stackTop; i = 0; i++) It is as follows: { if (list[i]!= otherStack.list[i]) return false; }
come back true; } };
stackType<int> stack1(50); stackType<int> stack2(50); int main() {
stack1.push(23); stack1.push(45); stack1.push(38); stack1.initializeStack();
pile2 = pile 1;
In the event where stack1 == stack2, the code would output “stack1 and stack2 are identical” \\ endl; if not, it would output “stack1 and stack2 are not identical” \\ endl;
stack2.push(32); stack2.pop();
after pop and push operations on stack2 ****, cout \\ “****” \\ endl;
In the event where stack1 == stack2, the code would output “stack1 and stack2 are identical” \\ endl; if not, it would output “stack1 and stack2 are not identical” \\ endl;
pull2.stack(11);
after another push operation on stack2 ****, cout \\ “****” \\ endl;
In the event where stack1 == stack2, the code would output “stack1 and stack2 are identical” \\ endl; if not, it would output “stack1 and stack2 are not identical” \\ endl;
// Using stack type classes to illustrate how to utilize an STL queue queue queue<stackType<int>> stackQueue;
Push Queue stackQueue(stack1); Push Queue(stack2);
“**** Using STL queue methods ****” \\ cout \\ endl;
stackType<int> while (!stackQueue.empty()) {} the currentStack is equal to stackQueue.front(); stackQueue.pop();
if (!currentStack.isEmptyStack()) { cout \\ currentStack.top() \\ endl; cout \\ “Front of the queue: “;
alternatively The output is cout \\ “Empty stack” \\ endl;
0 is returned;
Inside this code
The stack items are now stored in a vector<Type> thanks to modifications made to the stackType class.
Stack underflow (popping from an empty stack), stack overflow (pushing onto a full stack), and accessing the top of an empty stack now have exception handling.
You can now push several values into the stack by using the overloaded push function, which now accepts a vector of items.
The code shows how to store stackType objects in an STL queue and retrieve and remove elements from the queue using the front() and pop() functions.
QUESTION
Description
Use the stackADT class and stackType classes given to you in Ch. 18 and code supplied here to complete this assignment.
#include <iostream>
#include “myStack.h”
using namespace std;
int main()
{
stackType<int> stack1(50);
stackType<int> stack2(50);
stack1.initializeStack();
stack1.push(23);
stack1.push(45);
stack1.push(38);
stack2 = stack1;
if (stack1 == stack2)
cout << “stack1 and stack2 are identical” << endl;
else
cout << “stack1 and stack2 are not identical” << endl;
stack2.pop();
stack2.push(32);
cout << “**** After pop and push operations on stack2 ****” << endl;
if (stack1 == stack2)
cout << “stack1 and stack2 are identical” << endl;
else
cout << “stack1 and stack2 are not identical” << endl;
stack2.push(11);
cout << “**** After another push operation on stack2 ****” << endl;
if (stack1 == stack2)
cout << “stack1 and stack2 are identical” << endl;
else
cout << “stack1 and stack2 are not identical” << endl;
return 0;
}
Modify the stack implementation such that it uses a vectors as the container instead of an array.
Overload the pure virtual function push such that it takes a vector as an argument which pushes multiple values onto the stack.
Throw exceptions when:
An attempt is made to pop from an empty stack (display message)
An attempt is made to push onto a full stack (display message)
top() is called on an empty stack (display message).
A negative or zero value for stack size is passed to the constructor (In this case, when handling the error, automatically recall the constructor with a value of 100).
Using the STL queue container, add multiple stackType classes to a queue. Demonstrate an ability to use STL queue methods such as front() and pop().
ASSIGNMENT
Please pay careful attention to all assignment requirements. You are graded on your ability to implement each requirement.
In Week 8 we be modifying code given to you in Chapter 18 of the book and testing your ability to use the classes as intended.
See the assignment requirements for required modifications to the classes given.
The Stack classes given to you in Chapter 18 are using an array as the data structure to contain the data in the stack. Your job is to modify this code so that a vector is used to contain the data instead.
i.e.
Replace this
Type *list;
with this
vector<Type> list;
Then modify the rest of the code to work with that vector syntax instead of the array pointer syntax.
You will also be adding exception handling for four scenarios.
Finally, be sure to see the section on testing the STL queue class as part of your main program. Use your stack Classes to add to a queue and demonstrate some common STL queue methods.
![Place Your Order Here](http://scholarywriters.com/wp-content/uploads/2023/08/Bottom-of-every-post.png)