C++ Data Structures
Write the C++ (not just pseudocode) for a client program that creates a stack of the strings “Jamie”, “Jane” and “Jill” in that order with “Jamie” at the top of the stack, “Jane” next and “Jill” at the bottom. Peek at each item and then pop it off the stack to show Jamie is at the top, Jane next and Jill at the bottom.
Expert Answer
using namespace std;
class stack {
private:
string *arr;
int top;
int size;
public:
stack(){
arr = new string[10];
top = -1;
size = 10;
}
stack(int n) {
arr = new string[n];
top = -1;
size = n;
}
void push (string s) {
if (top == size-1)
return;
arr[++top] = s;
}
string pop () {
if (top >= 0)
return arr[top–];
return NULL;
}
string peek () {
if (top >= 0)
return arr[top];
return NULL;
}
};
int main() {
stack mystack;
mystack.push(“Jill”);
mystack.push(“Jane”);
mystack.push(“Jamie”);
cout << “top: ” << mystack.peek() << endl;
cout << “Pop from stack..” << endl;
mystack.pop();
cout << “top: ” << mystack.peek() << endl;
cout << “Pop from stack..” << endl;
mystack.pop();
cout << “top: ” << mystack.peek() << endl;
}
Here you go champ.. Personalized array based stack implementation for you… Hope you like it. Let me know if you need any changes.