Doubled Linked List with Template

Hi Friends. Have a question about the template.
The compiler report error about:
error: variable or field 'insert' declared void|
error: 'TYPE' was not declared in this scope|
error: template argument 1 is invalid|\
....

Please give me some hints about the syntax.
Before implementing, my program works well. But it becomes mess after adding the template to each header file.
I guess I violate the rules.

Thanks.
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
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include <iostream>
#include "Node.h"

using namespace std;
template<class TYPE>
class List
{
    public:
    List();
    ~List();
    unsigned int getNode();

    void pushFront(TYPE data);
    void pushBack(TYPE data);

    void popFront();
    void popBack();

    void insert(TYPE data);
    void remove(TYPE data, bool type);

    bool find(TYPE data);
    bool findRecursive (TYPE data);
    bool findRecursive(TYPE data, Node<TYPE>* ptr);

    friend ostream& operator<<(ostream& outStream, const List& object);

    void clear();
    void callclearRecursive();
    void clearRecursive(Node<TYPE>*& ptr);

    ///int getData();
    void sort();
    void swap(Node<TYPE>* ptr1, Node<TYPE>* ptr2);
    void print();

    private:
    unsigned int m_nodeCount;
    Node<TYPE>* m_head;
    Node<TYPE>* m_tail;

};
#endif // LIST_H_INCLUDED 

This is the second part. It is too long so I split to different post.

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "List.h"

template<class TYPE>
List<TYPE>::List()
{
    m_nodeCount=0;
    m_head=NULL;
    m_tail=NULL;
}

template<class TYPE>
List<TYPE>::~List()
{

}

template<class TYPE>
unsigned int List<TYPE>::getNode()
{
    return m_nodeCount;
}

template<class TYPE>
void List<TYPE>::pushFront(TYPE data)
{
    Node<TYPE> *newNode = new Node<TYPE>(data);
    if(m_head==NULL && m_tail ==NULL)
    {
        m_tail =m_head=newNode;
    }
    else
    {
        m_head->setPrevious(newNode);
        newNode->setNext(m_head);
        m_head=newNode;
    }
    m_nodeCount++;
}

template<class TYPE>
void List<TYPE>::pushBack(TYPE data)
{
    Node<TYPE>* newNode = new Node<TYPE>(data);
    if(m_head==NULL && m_tail == NULL)
    {
        m_tail = m_head = newNode;
    }
    else
    {
        m_tail->setNext(newNode);
        newNode->setPrevious(m_tail);
        m_tail = newNode;
        m_tail ->setNext(NULL);
    }
    m_nodeCount++;
}

template<class TYPE>
void List<TYPE>::popFront()
{
    if(m_head!=NULL)
    {
        Node<TYPE> *tmp= m_head;
        m_head = m_head->getNext();
        ///Only one Node<TYPE>, m_head = m_tail;
        if(m_tail==tmp)
        {
            m_tail=NULL;///set m_head, m_tail = NULL
        }
        else
        {
            m_head->setPrevious(NULL);
        }
        delete tmp;
        m_nodeCount--;
    }
}

template<class TYPE>
void List<TYPE>::popBack()
{
    if(m_tail!=NULL)
    {
        Node<TYPE>* tmp = m_tail;
        m_tail=m_tail->getPrevious();
        ///if there is only one Node<TYPE>. m_head = m_tail
        if(m_head==tmp)
        {
            m_head=NULL;
        }
        else if(m_head!=tmp)
        {
            m_tail->setNext(NULL);
        }
        delete tmp;
        m_nodeCount--;
    }
}

template<class TYPE>
void List<TYPE>::insert(TPYE data)
{
    Node<TYPE>* newNode = new Node<TYPE>(data);
    if(m_head==NULL && m_tail ==NULL)
    {
        m_head = m_tail = newNode;
    }
    else if(m_head->getData() > data)
    {
        newNode->setNext(m_head);
        m_head->setPrevious(newNode);
        m_head = newNode;
    }
    else if(m_tail ->getData()<=data)
    {
        m_tail->setNext(newNode);
        newNode->setPrevious(m_tail);
        m_tail = newNode;
    }
    else
    {
        Node<TYPE>* ptr = m_head;
        ///string junk;
        while(ptr->getNext()!=NULL && ptr->getNext()->getData()<data)
        {
            ///cout << "Seeking " << data << ", -now at Node<TYPE> " << ptr->getData() << endl;
            ///cout << "Seeking " << data << ", is larger " << ptr->getNext()->getData() << endl;
            ///getline(cin, junk);
            ptr = ptr->getNext();
            ///cout << "Seeking " << data << ", +now at Node<TYPE> " << ptr->getData() << endl;
            ///getline(cin, junk);
        }
        ///cout << "Seeking " << data << ", now at Node<TYPE> " << ptr->getData() << endl;
        ptr->getNext()->setPrevious(newNode);
        newNode->setNext(ptr->getNext());
        ptr->setNext(newNode);
        newNode->setPrevious(ptr);
    }
}
template<class TYPE>
void List<TYPE>::sort()
{
    if(m_head!=NULL)
    {
        bool sort =false;
        for(int i=0; i<m_nodeCount && sort!=true; i++)
        {
            Node<TYPE>* ptr=m_tail;
            for(int j=m_nodeCount-1; j>i; j--)
            {
                if(ptr->getData()<ptr->getPrevious()->getData())
                {
                    sort = false;
                    swap(ptr, ptr->getPrevious());
                }
                ptr = ptr->getPrevious();
            }
        }
    }
}
template<class TYPE>
void List<TYPE>::swap(Node<TYPE> *ptr1, Node<TYPE> *ptr2)
{
    int tmp;
    tmp = ptr1->getData();
    ptr1->setData(ptr2->getData());
    ptr2->setData(tmp);
}
template<class TYPE>
void List<TYPE>::remove(int data, bool type)
{
    if(m_head!=NULL && m_tail !=NULL)
    {
        Node<TYPE> *ptr = m_head;
        if(type==true)
        {
            while(ptr!=NULL)
            {
                cout << "Seeking " << data << ", now at Node " << ptr->getData() << endl;
                string junk;
                getline(cin, junk);

                if(ptr->getData()==data)
                {
                    if(ptr ==m_head)
                    {
                        ptr = ptr->getNext();
                        popFront();
                    }
                    else if(ptr == m_tail)
                    {
                        ptr = ptr->getNext();
                        popBack();
                    }
                    else
                    {
                        Node<TYPE> *previous = ptr->getPrevious();
                        Node<TYPE> *next = ptr->getNext();
                        previous->setNext(next);
                        next->setPrevious(previous);
                        delete ptr;
                        m_Node<TYPE>Count--;
                        ptr = next;
                    }
                }
                else
                {
                    ptr = ptr->getNext();
                }
            }
        }
        else if(type ==false)
        {
            while(ptr!=NULL)
            {
                cout << "Seeking " << data << ", now at Node " << ptr->getData() << endl;
                string junk;
                getline(cin, junk);
                if(ptr->getData()==data)
                {
                    break;
                }
                ptr = ptr->getNext();
            }
            if(ptr!=NULL)
            {
                if(ptr ==m_head)
                {
                    popFront();
                }
                else if(ptr ==m_head)
                {
                    popBack();
                }
                else
                {
                    Node<TYPE> *previous = ptr->getPrevious();
                    Node<TYPE> *next = ptr->getNext();
                    previous->setNext(next);
                    next->setPrevious(previous);
                    delete ptr;
                    m_Node<TYPE>Count--;
                }
            }
        }
    }
}
template<class TYPE>
bool List<TYPE>::find(int data)
{
    if(m_head==NULL && m_tail ==NULL)
    {
        return false;
    }
    Node<TYPE>* ptr = m_head;
    while(ptr!=NULL && ptr->getData()!=data)
    {
        ptr = ptr->getNext();
    }
    if(ptr==NULL)
    {
        return false;
    }
    else
    {
        return true;
    }
}
template<class TYPE>
bool List<TYPE>::findRecursive (TYPE data)
{
    return findRecursive(data, m_head);
}

bool List<TYPE>::findRecursive(TYPE data, Node<TYPE>* target)
{
    if(target==NULL)
    {
        return false;
    }
    else if(target->getData()==data)
    {
        return true;
    }
    else
    {
        return findRecursive(data, target->getNext());
    }
}
template<class TYPE>
ostream& operator<<(ostream& outStream, const List<TYPE>& object)
{
    for(Node<TYPE>* ptr = object.m_head; ptr ; ptr= ptr->getNext())
    {
        outStream<<ptr->getData();
    }
    outStream<<endl;
    return outStream;
}
template<class TYPE>
void List<TYPE>::clear()
{
    while(m_head!=NULL)
    {
        Node<TYPE>* tmp =m_head;
        m_head = m_head->getNext();
        delete tmp;
    }
    m_tail = NULL;
}
template<class TYPE>
void List<TYPE>::callclearRecursive()
{
    clearRecursive(m_head);
    m_head=NULL;
    m_tail=NULL;
}
template<class TYPE>
void List<TYPE>::clearRecursive(Node<TYPE> *&ptr)
{
    if(ptr!=NULL)
    {
        clearRecursive(ptr->getNext());
        delete ptr;
        ptr = NULL;
    }
}
template<class TYPE>
void List<TYPE>::print()
{
    Node<TYPE>* ptr=m_head;
    int i=1;
    cout<<"Double Linked-List<TYPE>:";
    while(ptr!=NULL)
    {
        cout<<"["<<i<<"]"<<ptr->getData()<<"->";
        ptr=ptr->getNext();
        i++;
    }
    cout<<endl;
}

The third part:
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
#ifndef Node_H_INCLUDED
#define Node_H_INCLUDED

#include <iostream>

template<class TYPE>
class Node
{
public:
    Node();
    Node(TYPE data);
    ~Node();

    void setData(TYPE data);
    TYPE getData();

    void setNext(Node<TYPE>* next);
    Node<TYPE>* getNext()const;///const can help complier to tell different function overloaded.
    Node<TYPE>*& getNext();

    void setPrevious(Node<TYPE>* previous);
    Node<TYPE>* getPrevious()const ;
    Node<TYPE>*& getPrevious();

private:
    TYPE m_data;
    Node<TYPE>* m_next;
    Node<TYPE>* m_previous;
};

#endif // Node_H_INCLUDED 

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
#include "Node.h"
template<class TYPE>
Node<TYPE>::Node()
{
    m_data = 0;
    m_next = NULL;
    m_previous= NULL;
}
template<class TYPE>
Node<TYPE>::Node(TYPE data)
{
    m_data = data;
    m_next = NULL;
    m_previous= NULL;
}
template<class TYPE>
Node<TYPE>::~Node()
{

}
template<class TYPE>
void Node<TYPE>::setData(TYPE data)
{
    m_data = data;
}
template<class TYPE>
TYPE Node<TYPE>::getData()
{
    return m_data;
}
template<class TYPE>
void Node<TYPE>::setNext(Node<TYPE>* next)
{
    m_next =next;
}
template<class TYPE>
Node<TYPE>* Node<TYPE>::getNext()const///const can help complier to tell different function overloaded.
{
    return m_next;
}
template<class TYPE>
Node<TYPE>*& Node<TYPE>::getNext()
{
    return m_next;
}
template<class TYPE>
void Node<TYPE>::setPrevious(Node<TYPE>* previous)
{
    m_previous = previous;
}
template<class TYPE>
Node<TYPE>* Node<TYPE>::getPrevious()const
{
    return m_previous;
}
template<class TYPE>
Node<TYPE>*& Node<TYPE>::getPrevious()
{
    return m_previous;
}
Topic archived. No new replies allowed.