Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

#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 a;

// 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 a;

// 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 q; ministack s;

// 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 using namespace std;

// ministack template template class ministack { private: T* stackArray; int numOfItems;

public: //Constructor ministack();

// ministack operations void push(T); void pop(); T top(); int size(); };

// Constructor template ministack::ministack() { stackArray = new T[MINISTACK_H]; numOfItems = 0; }

// pushes the argument onto the stack. template void ministack::push(T item) { // check for overflow if (numOfItems >= MINISTACK_H) { cout << "The stack is full. "; } else { // put the value at the end of the array stackArray[numOfItems] = item; numOfItems++; return; } }

// removes the value at the top of the stack template void ministack::pop() { // check for underflow if (numOfItems == 0) { cout << "The stack is empty. "; } else { // remove the last element numOfItems--; return; } }

// returns the value at the top of the stack template T ministack::top() { // check for underflow if (numOfItems == 0) { cout << "The stack is empty. "; } else { // return the last element of the stack return stackArray[numOfItems - 1]; } }

// returns the size of the stack template int ministack::size() { return numOfItems; }

#endif

MiniQueue.h

/*********************************** * MiniQueue.h ***********************************/ #ifndef MINIQUEUE_H #define MINIQUEUE_H 10

#include using namespace std;

// MiniQueue template template class miniqueue { private: T* queueArray; // Points to the queue array int numOfItems; // Number of items in the queue public: // Constructor miniqueue();

// miniqueue operations void push(T item); void pop(); T front(); int size(); };

// This constructor creates an empty queue template miniqueue::miniqueue() { queueArray = new T[MINIQUEUE_H]; numOfItems = 0; }

// inserts a value at the end of the queue. template void miniqueue::push(T item) { // check for overflow if (numOfItems >= MINIQUEUE_H) cout << "The queue is full. "; else { // Insert new item queueArray[numOfItems] = item; // Update item count numOfItems++; return; } }

// removes the value at the start of the queue template void miniqueue::pop() { int i = 0; // check for underflow if (numOfItems == 0) cout << "The queue is empty. "; else { // shift the array by 1 position to left to remove the first element for (i; i < numOfItems - 1; i++) { queueArray[i] = queueArray[i + 1]; } // Update item count numOfItems--; return; } }

// returns the front element of the queue template T miniqueue::front() { // check for underflow if (numOfItems == 0) cout << "The queue is empty. "; else { return queueArray[0]; } }

// returns the current size of the queue template int miniqueue::size() { return numOfItems; }

#endif

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

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions