Question
In C++ I had to create classes MiniStack and MiniQueue but now I want to edit MiniStack.h and MiniQueue.h so that whenever new memory is
In C++ I had to create classes MiniStack and MiniQueue but now I want to edit MiniStack.h and MiniQueue.h so that whenever new memory is allocated the pointer returned is valid. The data type of the values will be flexible, using atemplate. The classes should be able to store up to 10 values in an array while also not editing the Main.cpp file.
The requirements were:
Theminiqueue class should have the following functions:
- Constructor
- Push - add a value to the end of the queue
- Pop - remove a value from the front
- Front - return the value at the front
- Size - return count of values
Theministack class should have the following functions:
- Constructor
- Push - add a value to the top of the stack
- Pop - remove a value from the top
- Top - return the value at the top
- Size - return count of values
I am also concerned if my pop,top, and front functions are working properly. I don't believe they are since I can't see them operating within my terminal. My code so far is below:
Main.cpp #include
#include "MiniQueue.h" #include "MiniStack.h"
void test_queue(); void test_stack(); void test_string();
/*********************************** * Main ***********************************/ int main() { test_queue(); test_stack(); test_string(); return 0; }
/*********************************** * test_queue() ***********************************/ void test_queue() { int i; miniqueue
// Header
cout << "Integer Queue -------------------- ";
// Push onto list
for (i = 0; i < 12; i++) a.push(i); cout << endl;
// Pop off stack
while (a.size() > 0) { cout << a.front() << " "; a.pop(); }; cout << endl << endl;
// Check underflow
a.pop(); i = a.front(); cout << endl; }
/*********************************** * test_stack() ***********************************/ void test_stack() { int i; ministack
// Header
cout << "Integer Stack -------------------- ";
// Push onto list
for (i = 0; i < 12; i++) a.push(i); cout << endl;
// Pop off stack
while (a.size() > 0) { cout << a.top() << " "; a.pop(); }; cout << endl << endl;
// Check underflow
a.pop(); i = a.top(); cout << endl; }
/*********************************** * test_string() ***********************************/ void test_string() { int i; string val[7] = { "Homer","Lisa","Bart","Maggie","Moe","Ned","Lovejoy" }; miniqueue
// Header
cout << "String Queue & Stack -------------------- ";
// Push onto list
for (i = 0; i < 7; i++) { q.push(val[i]); s.push(val[i]); };
// Pop off queue
while (q.size() > 0) { cout << q.front() << " "; q.pop(); }; cout << endl;
// Pop off stack
while (s.size() > 0) { cout << s.top() << " "; s.pop(); }; cout << endl << endl; }
MiniStack.h
/*********************************** * MiniStack.h ***********************************/ #ifndef MINISTACK_H #define MINISTACK_H 10
#include
// ministack template template
public: //Constructor ministack();
// ministack operations void push(T); void pop(); T top(); int size(); };
// Constructor template
// pushes the argument onto the stack. template
// removes the value at the top of the stack template
// returns the value at the top of the stack template
// returns the size of the stack template
#endif
MiniQueue.h
/*********************************** * MiniQueue.h ***********************************/ #ifndef MINIQUEUE_H #define MINIQUEUE_H 10
#include
// MiniQueue template template
// miniqueue operations void push(T item); void pop(); T front(); int size(); };
// This constructor creates an empty queue template
// inserts a value at the end of the queue. template
// removes the value at the start of the queue template
// returns the front element of the queue template
// returns the current size of the queue template
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started