pointers and linked list

Discussion in 'C++' started by Pats, May 20, 2004.

  1. Pats

    Pats Guest

    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!!!:)
     
    Pats, May 20, 2004
    #1
    1. Advertising

  2. Pats

    Allan Bruce Guest

    "Pats" <> wrote in message
    news:...
    > 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
     
    Allan Bruce, May 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    514
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    503
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    548
    Barry Schwarz
    Jul 3, 2006
  4. Sean
    Replies:
    2
    Views:
    661
    loufoque
    Sep 24, 2006
  5. joshd
    Replies:
    12
    Views:
    705
    John Carson
    Oct 2, 2006
Loading...

Share This Page