Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an array of Strings, and using a stack, reverse the order of the Strings

ID: 3762630 • Letter: C

Question

Create an array of Strings, and using a stack, reverse the order of the Strings in the array.

For example, the strings in the array might be “Joe”, Fred, “Mary”. The reversed array would be “Mary”, Fred, “Joe”.

You should create a Stack class and a StackElement class for this assignment, then use these classes in your software. Your stack should be a linked data structure, similar to a linked list.

The main method should initialize an array of Strings – with an explicit array declaration. You should have at least five Strings in your array, but the software should work no matter how many Strings are in the array.

Your stack class should maintain a stack pointer and the size of the stack, and should have methods to:

Push a new element onto a stack (This method should put data onto the stack, which means it must first put the data in a stack element.)

Pop an element off the stack and return the data in the element.

Print a list of the data in the stack.

Return the current stack size.

You should use NetBeans for the software.

Explanation / Answer

#include<iostream>

using namespace std;

class Stack

{

public:

Stack(int MaxSize) :

MAX_NUM( MaxSize )

{

Data = new string[MAX_NUM];

CurrElemNum = 0;

}

Stack(const Stack &OtherStack);

~Stack(void)

{

delete[] Data;

}

  

void Push(const string &Item) {

Data[CurrElemNum++] = Item;

}

string Pop(void) {

return Data[--CurrElemNum];

}

bool isEmpty() {

return CurrElemNum == 0 ? 1 : 0;

}

  

  

protected:

string *Data;

int CurrElemNum;

const int MAX_NUM;

};

int main() {

Stack s(5);

s.Push("jary");

s.Push("mary");

s.Push("gabe");

s.Push("jim");

s.Push("andy");

  

cout<<"In rev order: ";

while (!s.isEmpty()) {

cout<<s.Pop()<<", ";

}

  

cout<<" ";

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote