Skip to main content
gamedev.net
🔒 Locked

Linked List

Started by dta1 Aug 5, 2002 at 6:39 PM 3 replies 250+ views
Original Post
dta1
dta1
Here's my Linked list classes:
    
class LL_Data
{
public:
	LL_Data() {}
	LL_Data(int val):myValue(val) {}
	int myValue;
};

class Node
{
public:
	Node() {}
	virtual ~Node() {}
	virtual LL_Data *GetData(USHORT num) = NULL;
};

class TailNode : public Node
{
public:
	TailNode() {}
	~TailNode() {}
	virtual LL_Data *GetData(USHORT num); // Returns error, could not find a node with that number.

};

class BodyNode : public Node
{
public:
	BodyNode(LL_Data *theData, Node *nextNode, USHORT num): myData(theData), myNext(nextNode), myNum(num) {}
	~BodyNode() { delete myData; delete myNext; }
	virtual LL_Data *GetData(USHORT num);
private:
	USHORT myNum;
	LL_Data *myData;
	Node *myNext;
};

class LinkedList
{
public:
	LinkedList():numNodes(0) { myHead = new TailNode; };
	~LinkedList() { delete myHead; };
	void InsertData(LL_Data *theData);
	LL_Data *GetData (USHORT num);
	const USHORT GetListSize() { return numNodes; }
private:
	Node *myHead;
	USHORT numNodes;
};
  
And there's two thing's I wonder about it. 1. How I may improve it to be faster (or easier to understand without speed change). 2. I'd like to use different data-types in the list. So the list can (almost) deal with whatever data-type it's given. I've tried to inherit the LL_Data (in the same way as the Node class is in my code) in my other classes but without success.
  
class LL_Data2 : public LL_Data
{
public:
	LL_Data2(int val):myOtherValue(val*10) {}
	int myOtherValue;
};
  
This gives me the error message: cannot convert from 'class LL_Data *' to 'class LL_Data2 *' (as expected) Help would be greatly appreciated. [edited by - dta1 on August 5, 2002 7:41:40 PM]
Erase, rewind and improve
Neosmyle
Neosmyle
Um...if you are just accessing the elements by a USHORT index, wouldn''t you be better off just using an array?
Zipster
Zipster
Template your linked list class to allow any data type. Like Neo said, you can speed it up by just making the whole thing an array if you want to access elements by index. That is unless, of course, you've carefully planned everything and decided a linked list would best suit your program's needs. If you only want to use the linked list for it's dynamic size quality, then it's not worth the O(n) lookup when you can get an O(1) with an array. You might not be using all the memory at once, and it may be a static size, but it's lightning fast compared to a linked list.

[edited by - Zipster on August 5, 2002 8:58:12 PM]
Ekas78
Ekas78
Linked lists are not fast. Lot''s o'' people mixup using pointers(which is "fast") and LL''s.

_______________________________________________
I don''t need pointers. I know where i''m going.
______Only dead fish go with the main-stream.
dta1
dta1
quote:
Original post by Zipster
Template your linked list class...


Thanks for the replies.
I think I''ll use arrays instead then, since I''m such a speed-freak
Erase, rewind and improve

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.