Here is the stack class: // implementation file for the stack class #include <io
ID: 3810087 • Letter: H
Question
Here is the stack class: // implementation file for the stack class #include <iostream> using namespace std;const int stack_size = 100;
class stack { private: char data [stack_size]; // array of stack elements int top; // top is the index of the top element of the stack public: stack (); // creates an empty stack void push (char item); // pushes item on top of stack char pop (); // removes and returns top of stack bool empty (); // returns true if stack is empty bool full (); // returns true if stack is full };
// constructor creates an empty stack stack::stack () { top = -1; }
// push adds item to the top of the stack void stack::push (char item) { // if the stack is full, print an error message if (full ()) { cout << " Stack Class Error: Pushing on a full stack"; cout << " Trying to push " << item; } else // ok to push { top++; data [top] = item; } }
// pop removes and returns the top element of the stack char stack::pop () { // if the stack is empty, print an error message if (empty ()) { cout << " Stack Class Error: popping an empty stack"; cout << " Returning a ?"; return '?'; } else // ok to pop { top--; return data [top + 1]; } }
// empty returns true if the stack is empty, else // it returns false bool stack::empty () { return top == -1; }
// full returns true if the stack is full, else it // returns false bool stack::full () { return top == stack_size - 1; } // implementation file for the stack class #include <iostream> using namespace std;
const int stack_size = 100;
class stack { private: char data [stack_size]; // array of stack elements int top; // top is the index of the top element of the stack public: stack (); // creates an empty stack void push (char item); // pushes item on top of stack char pop (); // removes and returns top of stack bool empty (); // returns true if stack is empty bool full (); // returns true if stack is full };
// constructor creates an empty stack stack::stack () { top = -1; }
// push adds item to the top of the stack void stack::push (char item) { // if the stack is full, print an error message if (full ()) { cout << " Stack Class Error: Pushing on a full stack"; cout << " Trying to push " << item; } else // ok to push { top++; data [top] = item; } }
// pop removes and returns the top element of the stack char stack::pop () { // if the stack is empty, print an error message if (empty ()) { cout << " Stack Class Error: popping an empty stack"; cout << " Returning a ?"; return '?'; } else // ok to pop { top--; return data [top + 1]; } }
// empty returns true if the stack is empty, else // it returns false bool stack::empty () { return top == -1; }
// full returns true if the stack is full, else it // returns false bool stack::full () { return top == stack_size - 1; } CIS 251 Lab 7.doc (Protected View) Word Insert Design Layout References Malings Review view Q Tea me what you to do CIS 251- Lab 7 Make the following changes to the implementation file: Due: March 29, 2017 Write a stack member function called elements that returns the number of elements in a stack (the stack should not be changed): Download the Lab cpp and stack.h files from the Lab 7 area of int element a Create C++ project and add the two files to it. Write a stack member function called print ends that prints the top element in the stack and then the bottom element in the stack uith a Take a look at the stack implementation space between them. It should not print alabel, just the values from the 1 Write an arithmetic expression that will compute the number ofelements void print ends 02 in a stack given the value oftop. Write a stack member function called change bottom that sets the 2. Does this expression give the correct answer for an empty stack? Explain. element at the bottom of the stack to its parameter. void change bottom (char new item) 3. What is that index ofthe top element of the stack Make the following changes to the application file (there are comments to help instruct you where to place each item): 4. What is the index of the bottom element of the stack? Declare a stack called stacky. Push the characters F.Lo w E, andRon the stack. 5. Write a C++ expression that gives the value stored at the top of the stack. Call your print ends function to print the stack. Change the bottom of the stack to P using change bottom 6. Write a C++ expression that gives the value stored at the bottom ofthe stack Pop the stack Call your print ends function to print the stack Print the number of elements in the stack (using the elements function A LOT or LATELY to find the number of elements) Plug the call into the already present Place your completed stack h and Lab 7 cpp source files in the Lab 7 area on Blackboard
Explanation / Answer
1)answer:
variable "top" stores the index value of the top element in stack..(means index of last element in array stack)
since stack is implemented as array... there are top+1 elements present in stack..
expression is :
int n;//variable to store number of elements in stack
n = top+1 ;// is the number of elements in stack //stored in varaible n
2)answer :
n = top+1;//in stack class only
yes, the expression gives correct answer for empty stack..also..
explanation:
the stack is empty means.. number of elements equals to zero..
initially when stack contructor is called, the variable "top" is initialized to -1
//see the constructor method
// constructor creates an empty stack
stack::stack ()
{
top = -1;
}
//so, when stack is empty the variable top value is -1
now
n = top+1;//in stack class only
n = -1+1//because stack is empty
n = 0;//means there are zero elements in stack...
hence it is working for, case where stack is empty
3)answer:
index of the top element of the stack is stored in the varible, top
by using top, we can access the top value of the stack
4)answer:
the bottom index of the stack is 0, when stack is not empty
5)answer:
c++ expression to get the top value of stack is
char c = data[top];//in stack class only..
6)answer:
c++ expression to get the bottom value of stack is
char c = data[0];//in stack class only..