I'm trying to implement a function to copy one instance of a stack into another instance of a stack.

The result should be two identical stacks. It only outputs blank lines. I

Implementation

#include <cassert>
#include <iostream>
#include <string>

#include "linkedStack.h"

using namespace std;

void linkedStack::initialize()
{
    destroyStack();
} //end initialize

void linkedStack::push(int newNumber)
{
    node *newNode;                    //pointer to create the new node

    newNode = new node;                //creats a new node
    assert( newNode != NULL);
    
    newNode->number = newNumber;    //new integer stored in newNode
    newNode->link = stackTop;        //isert newNode before stackTop
    stackTop = newNode;                //set stackTop to point to the top node
}

void linkedStack::pop()
{
    node *temp;

    if(stackTop != NULL)            //if stack is not empty
    {
        temp = stackTop;            //set temp to stackTop
        stackTop = stackTop->link;    //advance stackTop to the next node
        delete temp;                //delete temp node
    }
    else
        cerr << "Cannot remove from an empty stack." << endl;
} //end pop

void linkedStack::printStack()
{
    node *current;
    node *temp;

    temp = stackTop;
    while(stackTop != NULL)
    {
        cout << stackTop->number << " ";    //print letter on top of stack
        
        current = stackTop->link;    //set current to stackTops link
        stackTop = current;            //set stackTop to current

    }
    
    stackTop = temp;
    cout << endl << endl;
}    

void linkedStack::destroyStack()
{
    node *temp;                //pointer to delete a node

    while (stackTop != NULL)
    {
        temp = stackTop;

        stackTop = stackTop->link;

        delete temp;        // delet temp pointer
    }
} // end destroy

void linkedStack::displayMenu()
{
    cout << "***************************************" << endl << endl;
    cout << "1 Add an integer to the stack" << endl << endl;
    cout << "2 Delete the top integer on the stack" << endl << endl;
    cout << "3 Print the stack" << endl << endl;
    cout << "4 Copy the first stack into a second stack" << endl << endl;
    cout << "5 Print both stacks" << endl << endl;
    cout << "6 Destroy both stacks" << endl << endl << "9 to EXIT";
}

linkedStack::linkedStack()
{
    stackTop = NULL;
} //end constructor

linkedStack::linkedStack(linkedStack &otherStack)
{
    stackTop = NULL;
    copyStack(otherStack);
} // end constructor

linkedStack::~linkedStack()
{
    destroyStack();
} //end destructor

void linkedStack::copyStack(linkedStack& otherStack)
{
    node *newNode;        //pointer to create a node
    node *current;        //pointer to travel stack

    
        current = otherStack.stackTop;    //current points to the list to be copied

        assert(stackTop != NULL);

        stackTop->number = current->number;    //copy the letter
        stackTop->link = NULL;

        current = current->link;            //make current point to the next node

        while(current != NULL)
        {
            newNode = new node;

            assert(newNode != NULL);

            newNode->number = current->number; //copy the letter
            newNode->link = NULL;               //set the link of newNode to NULL

            stackTop->link = newNode;           //attach newNode after stackTop
            stackTop = newNode;                   //make the the top the last node

            current = current->link;           //make current point to next node

        } //end while
} //end copyStack

main()

#include <iostream>
#include "linkedStack.h"

using namespace std;

int main()
{
    linkedStack stack1, stack2;
    int choice;

    stack1.displayMenu();
    cout << endl << endl << "Please enter a choice: ";
    cin >> choice;
    cout << endl;

    while (choice != 9)
    {
        switch(choice)
        {
        case 1:    cout << "Enter an integer to add to the stack: ";
                cin >> choice;
                stack1.push(choice);
                
                break;
        case 2: stack1.pop();
                break;
        case 3: stack1.printStack();
                break;
        case 4: stack1.copyStack(stack2);
                break;
        case 5: stack1.printStack();
                stack2.printStack();
                break;
        case 6: stack1.destroyStack();
                stack2.destroyStack();
                break;
        default: cout << "Bad Selection!";
        } //end switch

        stack1.displayMenu();
        cout << endl << endl << "Enter your choice: ";
        cin >> choice;
        cout << endl;
    }// end while

};

HEADER

#ifndef H_linkedStack
#define H_linkedStack

#include <iostream>

using namespace std;

struct node
{
    int number;
    node *link;
};

class linkedStack
{
public:

    void initialize();
        //Function to initialize the stack to empty.
        //Postcondition: stackTop = NULL
    bool isEmpty();
        //Function to see if stack is empty.
        //Postcondition: Returns true if the stack is empty; otherwise, returns false.

    void push(int );
        //Funtion to add an integer to the stack.
        //Precondition: The stack exists and is not full.
        //Postcondition: The new integer is added to the stack.

    void pop();
        //Function to remove the top element of the stack.
        //Precondition: The stack exists and is not empty.
        //Postcondition: The stack is changed and the top element 
        //               is removed from the stack.

    void printStack();
        //Function to print the stack to the output device.
        //Postcondition: If the stack is empty, the program displays a proper
        //                 message, otherwise, the entire stack is printed.

    void destroyStack();
        //Function to remove all the elements of the stack,       
        //leaving the stack in an empty state.
         //Postcondition: stackTop = NULL
        
    void copyStack(linkedStack& otherStack); 
        //Function to make a copy of otherStack.
         //Postcondition: A copy of otherStack is created and
         //               assigned to this stack.

    void displayMenu();
        //Function to display a menu

    linkedStack(); 
        //default constructor
          //Postcondition: stackTop = NULL
    linkedStack(linkedStack& otherStack); 
        //copy constructor
    ~linkedStack();
        //destructor
        //All the elements of the stack are removed from the stack.

private:
    node *stackTop; //pointer to the stack

};

#endif

1. Inside copyStack() this line.. current = otherStack.stackTop; ..would ensure a crash as it would make current point to NULL.

2. Usually it's better to re-use the code. So in this case you can implement the copy like this:

void linkedStack::copyStack(linkedStack& otherStack)
{
    node* currNode = stackTop ;
    int* stack_contents = new int[size()] ;

    for( int i = 0; i < size(); i++, currNode = currNode->link )
    {
        assert( currNode != NULL ) ;
        stack_contents[i] = currNode->number ;
    }

    for( int i = size() - 1; i >= 0; i-- )
        otherStack.push( stack_contents[i] ) ;

    delete stack_contents ;
} //end copyStack

For this to work you have to keep track of the size of the stack. Just add a member size_t _size ; and initialize in c'tor, increment in push() and decrement in pop(). Also add this function size_t size() { return _size() ; } Good thing abt this is you don't have to re-write the way push() works inside copyStack().

The first iterator isn't getting assigned the correct number. instead of size - 1, it is being assigned to -842150451. Then obviously that number is being copied to the new stack.

void linkedStack::copyStack(linkedStack& otherStack)
{
    node* currNode = stackTop;
    int* stackContents = new int[stackSize()];
    otherStack.initialize();

    for( int i = size - 1; i < 0; i--)
    {
        assert( currNode != NULL );
        stackContents[i] = currNode->number;
        currNode = currNode->link;
     }

     for( int j = size -1; j >= 0; j--)
        otherStack.push( stackContents[j] );

} //end copyStack

Edited 3 Years Ago by pyTony: fixed formatting

This article has been dead for over six months. Start a new discussion instead.