Class template for stack in C++

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  class Stack{
    
    
    private:
        struct node
        {
            int value;
            node *next;
            
            node(int value, node *next = NULL)
            {
                this->value = value;
                this->next = next;
            }
        };
        node *top;
        
        
    public:
        Stack()
        {
            top = NULL;
        }
        void push(int number)
        {
            top = new node(number, top);
        }
        
        int pop()
        {
            int number = top->value;
            node *temp = top;
            top = top->next;
            delete temp;
            
            return number;
        }
        
        bool isEmpty()
        {
            if(top == NULL)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };
    
    
    int main(int argc, const char * argv[]) {
        Stack s;
        s.push(5);
        s.push(7);
        s.push(8);
        
        return 0;
    }
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.
Topic archived. No new replies allowed.