summaryrefslogtreecommitdiff
path: root/chapter7/stack2.template
blob: e02d827cdba309b8c10c119674c743f057ff855a (plain) (blame)
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
// FILE: stack2.template
// TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack2.h for documentation)
// This file is included in the header file, and not compiled separately.
// INVARIANT for the stack class:
//   1. The items in the stack are stored in a linked list, with the top of the
//      stack stored at the head node, down to the bottom of the stack at the
//      final node.
//   2. The member variable top_ptr is the head pointer of the linked list.

#include <cassert>  // Provides assert
#include "node2.h"  // Node template class

namespace main_savitch_7B
{
    template <class Item>
    stack<Item>::stack(const stack<Item>& source) 
    // Library facilities used: node2.h
    {
        main_savitch_6B::node<Item> *tail_ptr; // Needed for argument of list_copy

        list_copy(source.top_ptr, top_ptr, tail_ptr);
    }

    template <class Item>
    void stack<Item>::push(const Item& entry) 
    // Library facilities used: node2.h
    {
        list_head_insert(top_ptr, entry);
    }

    template <class Item>
    void stack<Item>::pop( )
    // Library facilities used: cassert, node2.h
    {
        assert(!empty( ));
        list_head_remove(top_ptr);
    }
    
    template <class Item>
    void stack<Item>::operator =(const stack<Item>& source) 
    // Library facilities used: node2.h
    {
        main_savitch_6B::node<Item> *tail_ptr; // Needed for argument of list_copy

        if (this == &source) // Handle self-assignment
            return;

        list_clear(top_ptr);
        list_copy(source.top_ptr, top_ptr, tail_ptr);
    }

    template <class Item>
    Item stack<Item>::top( ) const 
    // Library facilities used: cassert
    {
        assert(!empty( ));
        return top_ptr->data( );
    }
}