Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linked List question, (New to OOP/C++)

Reply
Thread Tools

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

 
 
israphelr@googlemail.com
Guest
Posts: n/a
 
      03-01-2009
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-01-2009
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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!
 
Reply With Quote
 
 
 
 
israphelr@googlemail.com
Guest
Posts: n/a
 
      03-01-2009
On Mar 1, 10:42 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed):
>
>
>
> > 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.
 
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
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
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