Linked List question, (New to OOP/C++)

Discussion in 'C++' started by israphelr@googlemail.com, Mar 1, 2009.

  1. Guest

    Hi,

    I have written a linked list in the same way as I would for C, but
    since I am using C++ and not C, i'd like to transform the code into
    OOP code.

    I have a struct for a node, I am aware that I can use a class,
    however, since I am not used to self-referential nodes, I'm a bit
    confused, for e.g. I could write a displayNode member function, and
    loop through all the nodes from outside the node class, however when
    it came to deleting and adding nodes, I would also have to implement
    those from outside the node class yes? So i'd have a linked list class
    and a node class, and the linked list would contain the code for
    handling the nodes (objects).

    Some advice on this would be very much appreciated.

    Thanks.
     
    , Mar 1, 2009
    #1
    1. Advertising

  2. * :
    > Hi,
    >
    > I have written a linked list in the same way as I would for C, but
    > since I am using C++ and not C, i'd like to transform the code into
    > OOP code.
    >
    > I have a struct for a node, I am aware that I can use a class,
    > however, since I am not used to self-referential nodes, I'm a bit
    > confused, for e.g. I could write a displayNode member function, and
    > loop through all the nodes from outside the node class, however when
    > it came to deleting and adding nodes, I would also have to implement
    > those from outside the node class yes? So i'd have a linked list class
    > and a node class, and the linked list would contain the code for
    > handling the nodes (objects).
    >
    > Some advice on this would be very much appreciated.


    Use std::list instead of implementing your own, unless this is purely a learning
    exercise.

    A 'struct' is a 'class' and vice versa. The only difference for this usage is
    the default access. With 'struct' you have 'public' access as default, whereas
    with 'class' you have private access as default.

    You're right that for the problem of implementing a linked list there are
    logically two levels of abstraction, namely list and nodes. Ideally the node
    abstraction would be hidden, inaccessible to the code using a list. That's how
    it is with std::list.


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to [http://alfps.izfree.com/].
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, Mar 1, 2009
    #2
    1. Advertising

  3. Guest

    On Mar 1, 10:42 am, "Alf P. Steinbach" <> wrote:
    > * :
    >
    >
    >
    > > Hi,

    >
    > > I have written a linked list in the same way as I would for C, but
    > > since I am using C++ and not C, i'd like to transform the code into
    > > OOP code.

    >
    > > I have a struct for a node, I am aware that I can use a class,
    > > however, since I am not used to self-referential nodes, I'm a bit
    > > confused, for e.g. I could write a displayNode member function, and
    > > loop through all the nodes from outside the node class, however when
    > > it came to deleting and adding nodes, I would also have to implement
    > > those from outside the node class yes? So i'd have a linked list class
    > > and a node class, and the linked list would contain the code for
    > > handling the nodes (objects).

    >
    > > Some advice on this would be very much appreciated.

    >
    > Use std::list instead of implementing your own, unless this is purely a learning
    > exercise.
    >
    > A 'struct' is a 'class' and vice versa. The only difference for this usage is
    > the default access. With 'struct' you have 'public' access as default, whereas
    > with 'class' you have private access as default.
    >
    > You're right that for the problem of implementing a linked list there are
    > logically two levels of abstraction, namely list and nodes. Ideally the node
    > abstraction would be hidden, inaccessible to the code using a list. That's how
    > it is with std::list.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > Due to hosting requirements I need visits to [http://alfps.izfree.com/].
    > No ads, and there is some C++ stuff! :) Just going there is good. Linking
    > to it is even better! Thanks in advance!


    Thanks.
     
    , Mar 1, 2009
    #3
    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. joshd
    Replies:
    12
    Views:
    705
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    810
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page