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

//Program to demonstrate use of the Stack class. #include <iostream> #include \"

ID: 3581429 • Letter: #

Question

//Program to demonstrate use of the Stack class.

#include <iostream>
#include "stack.h"
using namespace std;

int main( )
{
Stack s;

int number;

cout << "Enter a series of numbers separated by spaces and ended with a -1" << endl;
cin >> number;
while (number != -1)
{
s.push(number);
cin >> number;
}

cout << "Written backward that is: " << endl;
while ( ! s.empty( ) )
cout << s.pop( ) << endl;

return 0;
}

-----------------------------------------------------------------------------------------------------------

//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

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

---------------------------------------------------------------------------------

//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 <iostream>
#include <cstddef>
#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 << "Error: popping an empty stack. ";

       exit(1);
}

char result = top->data;

StackFramePtr temp_ptr;
temp_ptr = top;
top = top->link;

delete temp_ptr;

return result;
}

------------------------------------------------------

i have 1 error at   Stack s; in the testscorelab file. i use code:block to build

Explanation / Answer

Stack.h :

#ifndef STACK_H // prevent the Stack.h file from being compiled more than one time
#define STACK_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 <iostream>
#include <cstddef>
#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 << "Error: popping an empty stack. ";
exit(1);
}
char result = top->data;
StackFramePtr temp_ptr;
temp_ptr = top;
top = top->link;
delete temp_ptr;
return result;
}
int main( )
{
Stack s;
int number;
cout << "Enter a series of numbers separated by spaces and ended with a -1" << endl;
cin >> number;
while (number != -1)
{
s.push(number);
cin >> number;
}
cout << "Written backward that is: " << endl;
while ( ! s.empty( ) )
cout << s.pop( ) << endl;
return 0;
}

Output:

Enter a series of numbers separated by spaces and ended with a -1
65 66 67 68 69 -1
Written backward that is:
E
D
C
B
A