Simple linked list with classes

I need to implement a Dynamic Array class that is represented using simple linked list.

I have problems picturing this.

Should I go with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<typename Element>
struct NODE{
	Element e;
	struct NODE *next;
}NODE;

template<typename Element>
class LinkedList
{
private:
	NODE *element;
public:
// etc
};


This feels wrong.

I want to do it as general as possible so I don't want to start with some designing errors that I will see after a week when it will be too late and I'll need to redo a huge part of my project.

I know that every node will be a list entry and that it will point to the next list entry, but I need some help designing this (a quick example will be just enough to kick things off).

EDIT:

1
2
3
4
5
6
7
8
9
template<typename Element>
class LinkedList
{
public:
// constructor, destructor etc
private:
    LinkedList *next;
    Element e;
};


Is this the one?
Last edited on
The first example was better. A list is a collection of nodes, and should therefore be separate objects. I would start this project like so:
1
2
3
4
5
6
7
8
9
10
template <typename T>
class LinkedList
{
  private:
  struct Node
  {
    T data;
    Node* next;
  } *head;
};


What this does is makes it so the end user has no idea about a Node. It also allows you to store things like size as member variables of the LinkedList




Thanks. In the meantime I tried to implement a linked list and an iterator like this:

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
template<typename Element>
class Nod;

typedef Nod *PNod;

template<typename Element>
class Nod
{
private:
	Element e;
	PNod next;
public:
	friend class SingleLinkedList;

	// constructor
	template<typename Element>
	Nod(Element newE, PNod nextE = 0)
	{
		this->e = newE;
		this->next = next;
	}

	template<typename Element>
	Element getElement()
	{
		return e;
	}

	PNod getNext()
	{
		return next;
	}
};

/*
 * iterator
 */
class Iterator {
public:
	friend class SingleLinkedList;

	void prim();

	void next()
	{
		curent = curent->getNext();
	}

	int hasNext()
	{
		return curent != 0;
	}

	template<typename Element>
	Element elem()
	{
		return curent->getElement();
	}

private:
	Iterator(PNod &first)
	{
		curent = first;
	}

	PNod curent;
};

/*
 *
 */
class SingleLinkedList {
public:
	friend class Iterator;

	SingleLinkedList() : prim(0)
	{
	}

	~SingleLinkedList();

	Iterator* iterator()
	{
		Iterator *i = new Iterator(prim);
		return i;
	}

	/**
	 * Add an element to the list
	 * e - element
	 */
	template<typename Element>
	void add(Element e);

private:
	PNod prim;

	/*
	 * Return the first element in the iteration
	 */
	PNod first()
	{
		return prim;
	}

	/*
	 * Get the next element
	 */
	PNod next(PNod p)
	{
		return p->getNext();
	}
};

SingleLinkedList::~SingleLinkedList()
{
	while (prim != 0) {
		PNod p = prim;
		prim = prim->next;
		delete p;
	}
}

template<typename Element>
void SingleLinkedList::add(Element e)
{
	if (!prim)
		prim = new Nod(e);
	else {
		PNod p = prim;
		for (; p->next; p = p->next)
			;
		PNod q = new Nod(e);
		p->next = q;
	}
}


But I think I lost something(s) on the road when it comes to templates and/or friend classes because I get all kinds of errors: from "field next could not be resolved" in SingleLinkedList::~SingleLinkedList() to "shadow template parm 'class Element' " on template<typename Element> class Nod.

I'll try to implement the above with an Iterator friend class while waiting for some light on the mess I made here.
Topic archived. No new replies allowed.