Member 8370427 wrote:
what you said in last two lines.can you explain it more
See
Wikipedia[
^]for a graphic example of a linked list.
In code, this would look like:
template<typename t>
class LinkedListNode {
public:
LinkedListNode() : m_pNext(NULL) { }
LinkedListNode(T tData) : m_pNext(NULL), m_tData(tData) { }
void SetNext(LinkedListNode *node) { m_pNext = next; }
LinkedListNode *GetNext() { return m_pNext; }
void SetData(T &tData) { m_tData = tData; };
T &GetData() { return m_tData; };
private:
LinkedListNode *m_pNext;
T m_tData;
};
This is a very basic single linked list. This example is for a linked list of integers:
LinkedListNode<int> head = new LinkedListNode<int>(3);
LinkedListNode<int> next = new LinkedListNode<int>(1);
head->SetNext(next);
next->SetNext(new LinkedListNode<int>(2));
LinkedListNode<int> node = head;
while (node != NULL) {
printf("%d\n", node->GetData());
node = node->GetNext();
}
This gives the linked list 3->1->2 (each number would be printed on a separate line in the output)
You would add more functionality for things like add to end, which would loop through until it finds the node which has a
GetNext()
of
NULL
, and then set that next to the new node.
The point here is that every part of the linked list is of the same class,
LinkedListNode
.
If you still need help understanding linked lists,
google[
^] is your friend.