Trouble with a list class

I've been working on a List class for a couple weeks and recently I've been having lots trouble with it. From what I can gather, there's something wrong with the List::operator =, and there's a little bug somewhere causing an if else statement in push_back to loop. The lab guy I asked believes it's somewhere in List or List_iterator. Anyone have any idea why this is happening?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  #ifndef LIST_H
#define LIST_H

// List.h - a doubly-linked list

#include <algorithm>
#include <iostream>

using namespace std;

// forward declaration of classes defined in this header
template <class T> class Link;
template <class T> class List_iterator;//prototype class declaration

template <class T> 
class List
{
public:
   typedef List_iterator<T> iterator;

   List();
   List(const List<T> & l);
   ~List();

   bool empty() const {return my_size==0;}//check if empty
   unsigned int size() const; 
   T & back() const {return last_link->value;}
   T & front() const {return first_link->value;}
   void push_front(const T & x);//put value in front
   void push_back(const T & x);//put value in back
   void pop_front();//delete value from front
   void pop_back();//delete value from back
   iterator begin() const {return iterator(first_link);}
   iterator end() const {return iterator();}
   void insert(iterator pos, const T & x);//insert x before iterator position
   void erase(iterator & pos) {erase(pos);}//position must be valid after erase() returns
   List<T> & operator=(const List<T> & l);

protected:
   Link<T> * first_link;
   Link<T> * last_link;
   unsigned int my_size;
};

template <class T>
List<T>::List()
{
        first_link = 0;
        last_link = 0;
        my_size = 0;
}

template <class T>
List<T>::List(const List & l)
{
    first_link = 0;
    last_link = 0;
    my_size = 0;
    for(Link<T>*current=l.first_link;current!= 0;current=current->next_link)
        push_back(current -> value);
}

template <class T>
List<T>::~List()
{
    while(last_link!=NULL)//while list is not empty
    {
        delete last_link;
    }
}

template <class T>
unsigned int List<T>::size() const
{
    /*int counter=0;
    for(Link<T>*ptr=first_link;ptr!=0; ptr=ptr->next_link)
    {
        counter++;
        cout<<"hi";
    }
    return counter;*/
    return my_size;
}

template <class T>
void List<T>::push_front(const T&x)//add new value to front
{
    Link<T>*new_link=new Link<T>(x);
    if(empty())//if empty
    {
        first_link=last_link=new_link;
    }
    else//if not empty
    {
        first_link->prev_link=new_link;//
        new_link->next_link=first_link;
        first_link=new_link;//the new value becomes the first
    }
    my_size++;
}

template <class T>
void List<T>::push_back(const T& x)//add new value to back
{
    Link<T>*new_link=new Link<T>(x);
    //last_link->prev_link=new_link;
    //new_link->next_link=last_link;
    if(first_link==0)//if first is 0/empty?
    {
        first_link=last_link=new_link;
    }
    else
    {
        new_link->prev_link=last_link;
        last_link->next_link=new_link;
        last_link=new_link;//new becomes the last
        cout<<"LOOP";//loop trap. THIS IS WHERE LOOPING HAPPENS. Displays LOOP over and over in console.
    }
    my_size++;
}

template <class T>
void List<T>::pop_front()//delete from front
{
    Link<T>*temp=first_link;
    first_link=first_link->next_link;
    if(first_link!=0)//if first is not 0
    {
        first_link->prev_link=0;
    }
    else
    {
        last_link=0;
    }
    delete temp;
    my_size--;
}

template <class T>
void List<T>::pop_back()
{
    Link<T>*temp=last_link;
    last_link=last_link->prev_link;
    if(last_link!=0)
    {
        last_link->prev_link=0;
    }
    else
    {
        last_link=0;
    }
    delete temp;
    my_size--;
}

template <class T> //insert x before pos
void List<T>::insert(iterator pos, const T & x)
{
    Link<T>*new_link=new Link<T>(x);
    if(new_link==0)
    {
        cout<<"hi ";
        exit(1);
    }
    my_size++;
    Link<T>*next=pos.current_link;
    Link<T>*prev=next->prev_link;
    
    new_link->next_link=next;
    next->prev_link=new_link;
    new_link->prev_link=prev;
    
    if(prev==0)
    {
        first_link=new_link;
    }
    else
    {
        prev->next_link=new_link;
    }
}

template <class T>
List<T>&List<T>::operator=(const List<T> & l)
{
    List_iterator<T> the_list;
    return l=the_list;
}


//Class Link
template <class T> 
class Link 
{
private:
   Link(const T & x): value(x), next_link(0), prev_link(0) {}
                
   T value;     
   Link<T> * next_link;
   Link<T> * prev_link;

   friend class List<T>;
   friend class List_iterator<T>;
};

template <class T> class List_iterator
{
public:
   typedef List_iterator<T> iterator;

   List_iterator(Link<T> * source_link): current_link(source_link) { }
   List_iterator(): current_link(0) { }
   List_iterator(List_iterator<T>*source_iterator):current_link(source_iterator.current_link){ }

   T & operator*();  // dereferencing operator
   iterator & operator=(const iterator & rhs);
   bool operator==(const iterator & rhs) const;
   bool operator!=(const iterator & rhs) const;
   iterator & operator++();  // pre-increment, ex. ++it
   iterator operator++(int); // post-increment, ex. it++
   iterator & operator--();  // pre-decrement
   iterator operator--(int); // post-decrement

protected:
   Link<T> * current_link;
   List<T> * the_list;

   friend class List<T>;
};

template <class T>
T & List_iterator<T>::operator*()
{
        return current_link -> value;
}

template <class T>
List_iterator<T> & List_iterator<T>::operator=(const iterator & rhs)
{
    the_list=rhs.the_list;
    current_link=rhs.current_link;
}

template<class T>
bool List_iterator<T>::operator==(const iterator & rhs) const
{
    return (this->current_link==rhs.current_link); 
}

template <class T>
bool List_iterator<T>::operator!=(const iterator & rhs) const
{
    return !( *this == rhs );
}

template <class T>
List_iterator<T> & List_iterator<T>::operator++() // pre-increment
{
        current_link = current_link -> next_link;
        return *this;
}

template <class T>
List_iterator<T> List_iterator<T>::operator++(int)
{
    List_iterator<T> clone(current_link);
    current_link=current_link->next_link;
    return clone;
}

template <class T>
List_iterator<T>&List_iterator<T>::operator--()
{
    current_link=current_link->prev_link;
    return *this;
}

template <class T>
List_iterator<T> List_iterator<T>::operator--(int)
{
    List_iterator<T> clone(the_list, current_link);
    current_link=current_link->prev_link;
    return clone;
}

#endif 


And for the sake of reference, here's the .cpp file I'm using to test.

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
61
62
63
64
65
66
67
#include <iostream>
#include <cassert>
#include "List.h"

using namespace std;

int main()
{
   List<int> l;

   assert(l.size() == 0);
   assert(l.empty());

   l.push_front(44);         // list = 44
   assert(!l.empty());
   assert(l.front() == 44);
   assert(l.back() == 44);

   l.push_front(33);         // list = 33, 44
   assert(l.size() == 2);
   assert(l.front() == 33);
   assert(l.back() == 44);

   l.push_front(22);         // list = 22, 33, 44
   List<int>::iterator it = l.begin();
   l.insert(it, 11);         // list = 11, 22, 33, 44
   it = l.begin();
   assert(l.front() == 11);
   assert(*it == 11);
   assert(*++it == 22);
   assert(*++it == 33);
   assert(*++it == 44);

   it = l.begin();
   ++it;
   ++it;
   ++it;
   l.insert(it, 38);         // list = 11, 22, 33, 38, 44
   List<int>::iterator it2 = l.begin();
   assert(*it2 == 11);
   assert(*++it2 == 22);
   assert(*++it2 == 33);
   assert(*++it2 == 38);
   assert(*++it2 == 44);

   l.pop_front();            // list = 22, 33, 38, 44
   it2 = l.begin();
   assert(*it2 == 22); 
   assert(*++it2 == 33);     
   assert(*++it2 == 38);
   assert(*++it2 == 44);
   
   l.pop_back();             //list = 22, 33, 38
   List<int> copy = l;       //copy = 22, 33, 38
   assert(copy.size() == 3);
   List<int>::iterator it3 = copy.begin();
   assert(*it3 == 22);
   assert(*++it3 == 33);
        
   copy.erase(it3);         //copy = 22, 38
   assert(copy.size() == 2); 
   it3 = copy.begin(); 
   assert(*it3 == 22);
   assert(*++it3 == 38);
   
   cout << "SUCCESS\n";
}  
Anybody? This is seriously stumping me.
Topic archived. No new replies allowed.