This program is perfectly run, but I need to write copy constructor. Please help
ID: 657934 • Letter: T
Question
This program is perfectly run, but I need to write copy constructor. Please help to write the copy constructor. I provide all codes.
///// HEADER FILE/////////
///// stack.h /////////////
//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
////// 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;
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
////// stackApp.cpp///////
//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
#include #include "Stack.h" #include using namespace std; namespace stacksavitch{ //Uses cstddef Stack::Stack() : top(NULL){ //Intentionally left empty } Stack::Stack(const Stack& a_stack){ if(a_stack.top==NULL){ top=NULL; // original list is empty } else{ // copy first node top=new StackFrame; top->data=a_stack.top->data; // copy rest of list StackFrame *newPtr=top; // new node pointer for(StackFrame *origPtr=a_stack.top->link;origPtr!=NULL; origPtr=origPtr->link){ newPtr->link=new StackFrame; newPtr=newPtr->link; newPtr->data=origPtr->data; } newPtr->link=NULL; // tail } } 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){ StackFrame *newPtr=new StackFrame; // set data portion of new node newPtr->data=the_symbol; //insert the new node newPtr->link=top;// newPtr->next pointer points to top top=newPtr; } //Uses iostream char Stack::pop(){ if(empty()){ coutlink; delete temp_ptr; return result; } }// stacksavitchRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.