Peer-reviewed code snippets that anyone can edit
A wiki for useful code snippets
Template Linked List With Iterators
r1 r2
54
54
    // note that these definitions illistrate why the iterator template uses three parameters.
55
55
    friend class LinkedListIterator<T, T&, T*>;
56
56
    friend class LinkedListIterator<T, const T&, const T*>; //Here we want the reference and pointers to be const, but not the data!
57
57
   
58
    //define some friendly names for veririous versions of the iterator class
58
    //define some friendly names for various versions of the iterator class
59
59
    typedef LinkedListIterator<T, T&, T*> iterator;
60
60
    typedef LinkedListIterator<T, const T&, const T*> const_iterator;
61
61
   
62
62
    //Allow access to the the template type..
~
~
 
78
78
    public:
79
79
    //Default constructor... set root to null!
80
80
    LinkedList() : root(NULL), lastNode(NULL) { }
81
81
   
82
    //constructor to initilize with a given number of nodes using value_type's default constructor.
82
    //constructor to initialize with a given number of nodes using value_type's default constructor.
83
83
    LinkedList(size_t size) : root(NULL), lastNode(NULL) {
84
84
        for(size_t i=0; i < size; ++i) {
85
85
            push_top(value_type());
86
86
        }
87
87
    }
88
88
   
89
    //constructor to initilize with a given number of nodes with a default value.
89
    //constructor to initialize with a given number of nodes with a default value.
90
90
    //   value_type must have a copy constructor... is this constructor necessary?
91
91
    LinkedList(size_t size, value_type defValue) : root(NULL), lastNode(NULL) {
92
92
        for(size_t i=0; i < size; ++i) {
93
93
            push_top(value_type(defValue));
Tags added:
Tags removed: