Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1: Set up a main function to read commands from stdin: In this assignment y

ID: 3662192 • Letter: P

Question

Part 1: Set up a main function to read commands from stdin:

In this assignment you will read simple commands from standard input. You will display your name and ID number on a single line to start with and then enter the command processing loop. There will be one command per line, so your main function (pseudo-code) will be something like:

cout << “Student Name, ID# ”; while (true)
{

string cmd = ReadLine();

ProcessCmd(cmd); }

cout << “Done ”;

You must support the following commands:

push (followed by a space and then an integer value) : pushes an integer on to your stack (calls the push member function of your stack)

pop : calls the Pop member function of your stack reverse : calls the Reverse member function of your stack

display : Displays the contents of the stack. See the description of the Display member

function below in part 2 for more specific details.

isempty : Displays either “true” or “false” on a line based on whether the stack is empty or not. Put a newline character (‘ ’) after displaying this value.

quit : Breaks the command processing loop and terminates your program. Display the line “Done” as the very last thing before returning from main.

Part 2: Implement the ReversibleStack class:

Write an integer stack class in C++ that uses a singly linked list internally. It must have the following public methods:

void Push(int item) : Pushes an item onto the top of the stack • bool IsEmpty() const : Returns true if the


int Pop() : Pops an item off the top of the stack and returns it

void Reverse() : Reverses the order of items on the stack by reversing the singly linked list

void Display(std::ostream& writer) const : Displays the contents of the stack from top to bottom on one line. Numbers must be separated by a single space and a single newline character should be at the end of the line. This operation must not change the contents of the stack.

The writer parameter is used to write the line. It works like std::cout, so instead of std::cout << “Some string ” your code in this function will display lines with writer << “Some string ”.

Following logically from part a, when you call this function from main pass std::cout as the parameter.

You must not only implement the stack with a singly linked list, but also you must have the push and pop methods run in constant time. You should not be traversing through nodes to the end of your linked list every time you do a push or pop.

The node structure used within the class must be declared privately and code outside of the ReversibleStack class implementation should NEVER have direct access to items in the linked list. Do NOT add functions that return any nodes from the list. You may add helper functions for debugging and/or testing purposes.

in Data:

push 10
push -6
push 5
display
reverse
display
pop
reverse
display
isempty
pop
pop
isempty
quit

outData:

name, ID#

5 -6 10

10 -6 5
5 -6
false
true
Done

Explanation / Answer

Hi,

Below is the solution to your question:

home / study / questions and answers / engineering / computer science / create a c++ project and add the stack.h file and ...

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

#include <cstdlib>
#include <iostream>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
stack stacky;
stacky.push('10');
stacky.push('-6');
stacky.push('-5');
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;
}