Question: Write the C + + implementation of the pop member function of the Stack class: pop ( ) : deletes the value at the top

Write the C++ implementation of the pop member function of the Stack class:
pop(): deletes the value at the top of the stack and returns it. Assume the stack is not empty (no need to check).
The push function is given.
Write code in main() to test this function. Write a loop to enter an unknown number of strings, one string per line. The loop stops when you enter 0. As you are entering strings, they are to be pushed onto a stack. Once done, pop them out of the stack and display them one per line.
Ex.: If the user enters
one
two
three
0
the output should be:
three
two
one
Ex.: If the user enters 0 the output should be:
Empty stack!
/**~*~*~*
CIS 22C
Project: Stack of strings (pop)
Written by:
IDE:
*~*/
#include
#include
using namespace std;
class Stack_str
{
private:
// Structure for the stack nodes
struct StackNode {
string value; // Value in the node
StackNode *next; // Pointer to next node
};
StackNode *top; // Pointer to the stack top
int length;
public:
Stack_str(){ top = NULL; length =0; }//Constructor
// ~Stack_str(); // Destructor
// Stack operations
bool isEmpty(); /* Write your code here */
bool push(string);
string pop();
string peek();
int getLength();
};
/**~*~*~*
Member function push: pushes the argument onto the stack.
*~**/
bool Stack_str::push(string item)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = top;
top = newNode;
length++;
return true;
}
/**~*~*~*
Member function pop pops the value at the top
of the stack off, and returns it
Assume stack is not empty
*~**/
/* Define the pop function */
int main(){
Stack_str s;
string item;
/* Write your code here to test the push and pop functions */
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!