Generics Linked List

Apr 27, 2012 at 6:34pm
Hi lads, nice to meet you all, I have a question regarding generics in C++. I come from a Java background and I have to learn C++ for a new job and I'm progressing through it fine, its not so bad really. Memory management and pointers used to scare me but now I couldn't imagine programming without them. I know how to generics in Java and am comfortable enough with them, I tried using templates but I got compiler errors galore with my C++ Linked List so I created this Linked List class and it works well enough, but only with integers. Could anyone point me in the right directions regarding making this be compatible with all primitive types. All input is greatly appreciated. Thanks in Advance.

Class file
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
#include "LinkedList.h"
#include <string>
#include <iostream>

using namespace std;

LinkedList::LinkedList()
{
    Node *head = new Node;
    Node *tail = new Node;

    prev_ = head;
    next_ = tail;
    head_ = head;
    tail_ = tail;

    head->next = next_;
    head->prev = 0;
    tail->prev = prev_;
    tail->next = 0;

    size_ = 0;
    start_ = 0;
}

LinkedList::~LinkedList()
{
    delete head_;
    Node *temp;
    for(int index = 0; index < size(); index++)
    {
        temp = startingPoint_->next;
        delete startingPoint_;
        startingPoint_ = temp;
    }
    delete tail_;
    startingPoint_ = 0;
    prev_ = 0;
    next_ = 0;
    head_ = 0;
    tail_ = 0;
}

void LinkedList::insertElement(int data)
{
    Node *curElement_  = new Node;
    curElement_->data = data;
    curElement_->prev = prev_;
    curElement_->next = next_;
    prev_->next = curElement_;

    prev_ = curElement_;
    if(size_ == 0)
    {
        startingPoint_ = curElement_;
        printStart_ = startingPoint_;
    }
    incrementSize();
}

void LinkedList::incrementSize()
{
    size_++;
}

int LinkedList::size()
{
    return size_;
}

void LinkedList::printAllElements()
{
    if(start_ < size())
    {
        cout << printStart_->data << endl;
        printStart_ = printStart_->next;
        start_++;
        printAllElements();
    }
    else
    {
        printStart_ = startingPoint_;
        start_ = 0;
    }
}

int LinkedList::getElementAt(int index)
{
    Node *getElement_ = startingPoint_;
    for(int iterator = 0; iterator < index; iterator++)
    {
        getElement_ = getElement_->next;
    }
    return getElement_->data;
}


header file
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
#pragma once

class LinkedList
{
public:
    LinkedList();
    ~LinkedList();
    void insertElement();
    typedef struct Node
    {
        Node *prev;
        int data;
        Node *next;
    } Node;
    void insertElement(int data);
    int getElementAt(int index);
    void printAllElements();
    int size();
private:
    void incrementSize();
    Node *startingPoint_;
    Node *printStart_;
    Node *cleanElement_;
    Node *prev_;
    Node *next_;
    Node *head_;
    Node *tail_;
    int size_;
    int start_;
};
Apr 27, 2012 at 6:50pm
Put your implementation in the header: see templates in c++ http://www.parashift.com/c++-faq-lite/templates.html

header file:

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
#ifndef LinkedList_h__
#define LinkedList_h__

template<typename T>
class LinkedList
{
public:
	LinkedList();
	~LinkedList();
	void insertElement();
	typedef struct Node
	{
		Node *prev;
		T data;
		Node *next;
	} Node;
	void insertElement(T data);
	T getElementAt(int index);
	void printAllElements();
	int size();
private:
	void incrementSize();
	Node *startingPoint_;
	Node *printStart_;
	Node *cleanElement_;
	Node *prev_;
	Node *next_;
	Node *head_;
	Node *tail_;
	int size_;
	int start_;
};

template<typename T>
LinkedList<T>::LinkedList()
{
	Node *head = new Node;
	Node *tail = new Node;

	prev_ = head;
	next_ = tail;
	head_ = head;
	tail_ = tail;

	head->next = next_;
	head->prev = 0;
	tail->prev = prev_;
	tail->next = 0;

	size_ = 0;
	start_ = 0;
}

template<typename T>
LinkedList<T>::~LinkedList()
{
	delete head_;
	Node *temp;
	for(int index = 0; index < size(); index++)
	{
		temp = startingPoint_->next;
		delete startingPoint_;
		startingPoint_ = temp;
	}
	delete tail_;
	startingPoint_ = 0;
	prev_ = 0;
	next_ = 0;
	head_ = 0;
	tail_ = 0;
}

template<typename T>
void LinkedList<T>::insertElement(T data)
{
	Node *curElement_  = new Node;
	curElement_->data = data;
	curElement_->prev = prev_;
	curElement_->next = next_;
	prev_->next = curElement_;

	prev_ = curElement_;
	if(size_ == 0)
	{
		startingPoint_ = curElement_;
		printStart_ = startingPoint_;
	}
	incrementSize();
}

template<typename T>
void LinkedList<T>::incrementSize()
{
	size_++;
}

template<typename T>
int LinkedList<T>::size()
{
	return size_;
}

template<typename T>
void LinkedList<T>::printAllElements()
{
	if(start_ < size())
	{
		cout << printStart_->data << endl;
		printStart_ = printStart_->next;
		start_++;
		printAllElements();
	}
	else
	{
		printStart_ = startingPoint_;
		start_ = 0;
	}
}

template<typename T>
T LinkedList<T>::getElementAt(int index)
{
	Node *getElement_ = startingPoint_;
	for(int iterator = 0; iterator < index; iterator++)
	{
		getElement_ = getElement_->next;
	}
	return getElement_->data;
}

#endif // LinkedList_h__ 
Last edited on Apr 27, 2012 at 6:50pm
Apr 27, 2012 at 7:02pm
This is my main

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "LinkedList.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
    LinkedList <int>list;
    list.insertElement(3);
    list.insertElement(7);
    cout << "The most recently insterted element is, " << list.getElementAt(1) << " ";
}


And I get these errors when I run that.

/tmp/ccuZ68zn.o: In function `main':
Main.cpp:(.text+0x11): undefined reference to `LinkedList<int>::LinkedList()'
Main.cpp:(.text+0x22): undefined reference to `LinkedList<int>::insertElement(int)'
Main.cpp:(.text+0x33): undefined reference to `LinkedList<int>::insertElement(int)'
Main.cpp:(.text+0x44): undefined reference to `LinkedList<int>::getElementAt(int)'
Main.cpp:(.text+0x78): undefined reference to `LinkedList<int>::~LinkedList()'
Main.cpp:(.text+0x93): undefined reference to `LinkedList<int>::~LinkedList()'
collect2: ld returned 1 exit status

Thanks again for your help, I tried it that way before and these were the errors.
Apr 27, 2012 at 7:07pm
My apologies, I put it in the class in the header and it worked. Why does it only work that way. Isn't it bad practice to do it that way?
Apr 27, 2012 at 8:18pm
Apr 27, 2012 at 8:29pm
Appreciated man :)
Topic archived. No new replies allowed.