Here is the original requirements:
Write a class template for a linked-list implementation of a stack. The stack class must have the push, pop and isEmpty functions included, but with four additional functions: de- structor, copy constructor, overloaded assignment operator, and deepCopy. The private section shall only have one data member: a pointer called top. Be sure to have the pointer of the last node (at the bottom of the stack) set to NULL at all times. (This condition was- n’t necessary in the linked-list implementation of the queue, because the back pointer signified the last node—but it is generally necessary in linked lists). At the top of the class implementation file, include iostream (important because NULL is defined here) and use the std namespace.
Because my prof always don't talk much in the class, now I'm kind of confused with this project. I've made the push pop and isEmpty, although I'm not sure is it correct. But I've no idea how to make those four additional functions. Please give me some help; Here is the code I've done so far.
Your code looks good so far. The only comment i have is that you can simplify is Empty():
1 2 3
bool isEmpty() {
return (top == NULL);
}
The destructor should delete all nodes in the list.
The copy constructor, assignment operator and deepCopy() all do basically the same thing. Should deepCopy create a new list that contains a deep copy: Stack * Stack::deepCopy(); // make a deep copy of the current list
Or should the target list be an argument:
void Stack::deepCopy(Stack & target); // make target a deep copy of "this"
Or the other way around: void Stack::deepCopy(Stack &source); // make "this" a deep copy of source
In any case, don't forget that the assignment is actually to create a template:
Write a class template for a linked-list implementation of a stack.
I suggest that you continue writing the code for a stack of int's. Once it's working, modify the code to be a template.