Question: C++ Please modify the code for the stack of char data so that it will hold int data and test the modified stack code using
C++
Please modify the code for the stack of char data so that it will hold int data and test the modified stack code using the TestStackLab.cpp file.
When the program is run, it asks the user to input several integers, each separated by a space and ended with a -1. The program is then to display each integer in reverse order on a separate line. The only thing that needs to be done is to modify the stack.h and stack.cpp files to process int data instead of char data. The program output should be similar to:
===============================================================================
TestStackLab.cpp //Program to demonstrate use of the Stack class. #include "stdafx.h" #include#include "stack.h" using namespace std; int main( ) { Stack s; int number; cout > number; while (number != -1) { s.push(number); cin >> number; } cout =========================================================
stack.h
/DISPLAY 13.17 Interface File for a Stack Class //This is the header file stack.h. This is the interface for the class Stack, //which is a class for a stack of symbols. #ifndef STACK_H // prevent the Stack.h file from being compiled more than one time #define STACK_H #include "stdafx.h" struct StackFrame { char data; StackFrame *link; }; typedef StackFrame* StackFramePtr; class Stack { public: Stack( ); //Initializes the object to an empty stack. Stack(const Stack& a_stack); //Copy constructor. ~Stack( ); //Destroys the stack and returns all the memory to the freestore. void push(char the_symbol); //Postcondition: the_symbol has been added to the stack. char pop( ); //Precondition: The stack is not empty. //Returns the top symbol on the stack and removes that //top symbol from the stack. bool empty( ) const; //Returns true if the stack is empty. Returns false otherwise. private: StackFramePtr top; }; #endif //STACK_H========================================================
stack.cpp
//DISPLAY 13.19 Implementation of the Stack Class //This is the implementation file stack.cpp. //This is the implementation of the class Stack. //The interface for the class Stack is in the header file stack.h. #include "stdafx.h" #includeC:Windowssystem32cmd.exe Enter a series of numbers separated by spaces and ended with a -1 5368 7782 1425 7795 1234 79346 -1 Written backward that is 79346 1234 282 1422 1425 7782 5368 Press any key to cont inue#include #include "stack.h" using namespace std; //Uses cstddef: Stack::Stack( ) : top(NULL) { //Body intentionally empty. } Stack::Stack(const Stack& a_stack) { if (a_stack.top == NULL) top = NULL; else { StackFramePtr temp = a_stack.top; // moves from top to bottom of a_stack StackFramePtr end; // point to end of new stack end = new StackFrame; end->data = temp->data; top = end; temp = temp->link; while (temp != NULL) { end->link = new StackFrame; end = end->link; end->data = temp->data; temp = temp->link; } end->link = NULL; } } Stack::~Stack( ) { char next; while (! empty( )) next = pop( );//pop calls delete. } //Uses cstddef: bool Stack::empty( ) const { return (top == NULL); } void Stack::push(char the_symbol) { StackFramePtr temp_ptr; temp_ptr = new StackFrame; temp_ptr->data = the_symbol; temp_ptr->link = top; top = temp_ptr; } char Stack::pop( ) { if (empty( )) { cout data; StackFramePtr temp_ptr; temp_ptr = top; top = top->link; delete temp_ptr; return result; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
