Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointers and linked list

Reply
Thread Tools

pointers and linked list

 
 
Pats
Guest
Posts: n/a
 
      05-20-2004
Hello everyone I'm new here...

Just needed help on pointers and linked list how to implement
these...? What are the guidelines... and a sample program using
these.. Thanks!!!
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      05-20-2004

"Pats" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hello everyone I'm new here...
>
> Just needed help on pointers and linked list how to implement
> these...? What are the guidelines... and a sample program using
> these.. Thanks!!!


For a basic linked list you need to specify a basic node class. This should
have a pointer to some data, and methods for removing/inserting a new node
(at the head or tail only for a single-linked list or at any point for a
double-linked list).

To store the data, either use a template, or my preferred solution is to
specify an abstract class with no members which must be inherited from to
store data in the list. The data would be stored in the node as a pointer
to this abstract class.

here is a basic interface (untested)

class DataItem
{
public:
DataItem(){}
virtual ~DataItem(){}
};

class DoubleListNode
{
protected:
ListNode(DataItem *xiData);
virtual ~ListNode();

DataItem *mData;
DoubleListNode *mNextNode;
DoubleListNode *mPrevNode;
};

class DoubleList
{
public:
DoubleList();
virtual ~DoubleList();

DataItem *Insert(DataItem *xiNewItem);
DataItem *Remove(DataItem *xiNewItem);

// any other interface required

protected:
unsigned long mNumNodes;
DoubleListNode *mHead;
DoubleListNode *mTail;
};



I would also add some form of iterator to the list, but I`ll leave something
for you to do!
To make this List store integers, make a new DataItem like this:

class IntDataItem : public DataItem
{
IntDataItem(int xiInt) {mInt = xiInt;}
virtual ~IntDataItem();

int GetInt() {return mInt;}

private:
int mInt;
};


Hope this helps
Allan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Pointers, linked list, array of pointers Sean C++ 2 09-24-2006 01:35 PM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments