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