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

Need help not sure whats going on here... /*write a \"copy constructor\" for the

ID: 659669 • 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 code:

    //copy constructor

    Stack::Stack(const Stack& a_stack)

    {

         //condition to check whether the stack is empty or not

         //if it is empty returns sets the top value to NULL

         if(a_stack.top==NULL)

         {

             top=NULL;

         }

         //else goes through all the links of the stack

         else

         {

             //create a temp variable which points to the top of the stack

             //and is used to trace the Stack from top to bottom         

             StackFramePtr temp=a_stack.top;

             //create a end variable which is used to point to the end of the stack

             StackFramePtr end;

             //create an object of StackFrame

             end=new StackFrame;

             //assign the data present at the top to the end pointer

             end->data=temp->data;

             //place the link of end to temp

             top=end;

             //get the link

             temp=temp->link;

             //proceed the process until all the values of the present

             //stack is copied into the new stack end;

             while(temp!=NULL)

             {

                 end->link=new StackFrame;

                 end=end->link;

                 end->data=temp->data;

                 temp=temp->link;

             }

             //place end of the link as NULL

             end->link=NULL;

         }

    }

Program code:

Program code:

//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 "stdafx.h"

#include <iostream>

#include <cstddef>

#include "stack.h"

using namespace std;

namespace csc161

{

    //Uses cstddef:

    Stack::Stack() : top(NULL)

    {

         //Body intentionally empty.

    }

    //copy constructor

    Stack::Stack(const Stack& a_stack)

    {

         //condition to check whether the stack is empty or not

         //if it is empty returns sets the top value to NULL

         if(a_stack.top==NULL)

         {

             top=NULL;

         }

         //else goes through all the links of the stack

         else

         {

             //create a temp variable which points to the top of the stack

             //and is used to trace the Stack from top to bottom         

             StackFramePtr temp=a_stack.top;

             //create a end variable which is used to point to the end of the stack

             StackFramePtr end;

             //create an object of StackFrame

             end=new StackFrame;

             //assign the data present at the top to the end pointer

             end->data=temp->data;

             //place the link of end to temp

             top=end;

             //get the link

             temp=temp->link;

             //proceed the process until all the values of the present

             //stack is copied into the new stack end;

             while(temp!=NULL)

             {

                 end->link=new StackFrame;

                 end=end->link;

                 end->data=temp->data;

                 temp=temp->link;

             }

             //place end of the link as NULL

             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;

    }

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

// PrintReverseOfString-Stack.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "stack.h"

//Program Using the Stack Class

//Program to demonstrate use of the Stack class.

#include <iostream>

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');

    system("pause");

    return 0;

}

Sample Output:

Enter a word: Hello C World
Written backward that is: dlroW C olleH
Again?(y/n): n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote