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

I

israphelr

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.
 
A

Alf P. Steinbach

* (e-mail address 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
 
I

israphelr

* (e-mail address removed):







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

Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top