Create a C++ project and add the stack.h file and lab7.cpp files to it. Take a l
ID: 3759117 • Letter: C
Question
Create a C++ project and add the stack.h file and lab7.cpp files to it.
Take a look at the stack implementation.
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
_________________________________________________________
LAB7.CPP FILE
___________________________________________________________________
STACK.H FILE
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
stack stacky;
stacky.push('F');
stacky.push('L');
stacky.push('O');
stacky.push('W');
stacky.push('E');
stacky.push('E');
stacky.push('R');
cout << "The smallest element in the stack is " ;
stacky.smallest();
cout << " The initial stack top and bottom are: ";
stacky.print_ends();
stacky.pop();
stacky.pop();
cout << " The stack top and bottom after changes are: ";
stacky.print_ends();
cout << " The smallest element in the stack is " ;
stacky.smallest();
cout << " ";
system("PAUSE");
return EXIT_SUCCESS;
}
***************************************************
#include <iostream>
using namespace std;
const int stack_size = 1000;
class stack
{
private:
char data [stack_size];
int top;
public:
stack ();
char pop ();
void push (char item);
bool empty ();
bool full ();
void print_ends();
char smallest ();
};
stack::stack ()
{
top = -1;
}
char stack::pop ()
{
if (empty ())
{
cout << " Stack error: pop";
cout << " Popping an empty stack";
cout << " Returning a space";
return ' ';
}
else
{
top--;
return data [top + 1];
}
}
void stack::push (char item)
{
if (full ())
{
cout << " Stack error: push";
cout << " Pushing onto a full stack";
}
else
{
top++;
data [top] = item;
}
}
bool stack::empty ()
{
return top == -1;
}
bool stack::full ()
{
return top == stack_size - 1;
}
void stack::print_ends()
{
cout << data [top] << " " << data [stack_size];
}
char stack::smallest ()
{
int i;
char small;
small = 0;
for (i = 0; i < data [stack_size]; i++)
if (data[i] < small)
{
small = data[i];
}
return small;
}