Need help not sure whats going on here... /*write a \"copy constructor\" for the
ID: 659749 • Letter: N
Question
Need help not sure whats going on here...
/*write a "copy constructor" for the Stack class. Code for the class can be found
please fill in the implementation for the "copy constructor"*/
//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
#define STACK_H
namespace csc161
{
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;
};
}//csc161
#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;
namespace csc161
{
//Uses cstddef:
Stack::Stack() : top(NULL)
{
//Body intentionally empty.
}
Stack::Stack(const Stack& a_stack) {
//Copy constructor
}
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;
}
//Uses iostream:
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;
}
}// csc161
-----------------------------------------------------------------------------------------
//Program Using the Stack Class
//Program to demonstrate use of the Stack class.
#include <iostream>
#include "stack.h"
using namespace std;
using namespace csc161;
int main( )
{
Stack s;
char next, ans;
do
{
cout << "Enter a word: ";
cin.get(next);
while (next != ' ')
{
s.push(next);
cin.get(next);
}
cout << "Written backward that is: ";
while ( ! s.empty( ) )
cout << s.pop( );
cout << endl;
cout << "Again?(y/n): ";
cin >> ans;
cin.ignore(10000, ' ');
}while (ans != 'n' && ans != 'N');
return 0;
}
Explanation / Answer
// Copy constructor, Deep copy Stack::Stack(const Stack& aStack) { if (aStack.topPtr == NULL) topPtr = NULL; // original list is empty else { // copy first node topPtr = new StackNode; topPtr->item = aStack.topPtr->item; // copy rest of list StackNode *newPtr = topPtr; // new node pointer for (StackNode *origPtr = aStack.topPtr->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new StackNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; //tail } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.