Question: Write a stack member function called elements that returns the number of elements in a stack (the stack should not be changed): int elements ();
Write a stack member function called elements that returns the number of elements in a stack (the stack should not be changed):
int elements ();
Write a stack member function called print_ends that prints the top element in the stack and then the bottom element in the stack with a space between them. It should not print a label, just the values from the stack.
void print_ends ();
Write a stack member function called change_bottom that sets the element at the bottom of the stack to its parameter:
void change_bottom (char new_item);
Make the following changes to the application file (there are comments to help instruct you where to place each item):
Declare a stack called stacky.
Push the characters F, L, O, W, E, and R on the stack.
Call your print_ends function to print the stack.
Change the bottom of the stack to P using change_bottom
Pop the stack.
Call your print_ends function to print the stack
Print the number of elements in the stack (using the elements function to find the number of elements). Plug the call into the already present cout.
Here is the stack class:
#includeusing namespace std; // Implementation file for the stack const int stack_size = 1000; class stack { private: // array of elements in the stack char data [stack_size]; // index to the top of the stack int top; public: // stack is a constructor, creates an empty stack stack (); // removes an element from the stack and returns it char pop (); // adds an element to the top of the stack void push (char item); // returns true if the stack is empty, else false bool empty (); // returns true if the stack is full, else false bool full (); }; // stack is a constructor, creates an empty stack stack::stack () { top = -1; } // removes an element from the stack and returns it char stack::pop () { // if the stack is empty, print an error if (empty ()) { cout << " Stack error: pop"; cout << " Popping an empty stack"; cout << " Returning a space"; return ' '; } else // OK to pop the stack { top--; return data [top + 1]; } } // adds an element to the top of the stack void stack::push (char item) { // if the stack is full, print an error message if (full ()) { cout << " Stack error: push"; cout << " Pushing onto a full stack"; } else // OK to push { top++; data [top] = item; } } // returns true if the stack is empty, else false bool stack::empty () { return top == -1; } // returns true if the stack is full, else false bool stack::full () { return top == stack_size - 1; } Here is the main program where you have to make the changes to:
/* Lab 7 Application file Calls to functions from the enhanced stack class. */ #include#include #include "stack.h" // Note: stack implementation file is included using namespace std; int main(int argc, char *argv[]) { // declare a stack called stacky // Push characters F, L, O, W, E, and R on the stack cout << "The initial stack top and bottom are: "; // Call print_ends // Change the bottom of the stack to 'P' using change_bottom // Pop the stack cout << " The stack top and bottom after changes are: "; // Call print_ends // Print the number of elements in the stack using elements function cout << " There are now " << << " elements in the stack"; cout << " "; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
