Reference no: EM132189960
Will be working with two files lab7.cpp and stack.h ( implemnetation )
/* Lab 7 Application file Calls to functions from the enhanced stack class. */ #include <cstdlib> #include <iostream> #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:\n"; // Call print_ends // Change the bottom of the stack to 'P' using change_bottom // Pop the stack cout << "\n\nThe stack top and bottom after changes are:\n"; // Call print_ends // Print the number of elements in the stack using elements function cout << "\n\nThere are now " << << " elements in the stack"; cout << "\n\n"; return 0; }
stack.h file
#include <iostream> using 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 << "\n\nStack error: pop"; cout << "\nPopping an empty stack"; cout << "\nReturning 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 << "\n\nStack error: push"; cout << "\nPushing 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; } Questions:
1. Write an arithmetic expression that will compute the number of elements in a stack given the value of top.
2. Does this expression give the correct answer for an empty stack? Explain.
3. What is the index of the top element of the stack.
4. What is the index of the bottom element of the stack?
5. Write a C++ expression that gives the value stored at the top of the stack.
6. Write a C++ expression that gives the value stored at the bottom of the stack.
Make the following changes to the implementation file:
- 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.