nested loop use example

W

Willie

I am trying to get an inner class IntNode to encapsulate the nodes of
the linked list with the outer class having having a simple integer
value for data storage. So far I just can't see how this becomes
useful as with my current implementation it is just a pain to declare
everything. There must be a better way or at least there must be a
great pay off in some situations to go through this information as to
either would be of great help thanks.


William
 
T

Thomas Matthews

Willie said:
I am trying to get an inner class IntNode to encapsulate the nodes of
the linked list with the outer class having having a simple integer
value for data storage. So far I just can't see how this becomes
useful as with my current implementation it is just a pain to declare
everything. There must be a better way or at least there must be a
great pay off in some situations to go through this information as to
either would be of great help thanks.


William

In a simple linked list, there is no need to have separate classes
for data and for links. Templates are very useful when you don't
know the data type or for having an algorithm or class work with
any data type.

template<class Data_Type>
struct Node
{
Data_Type data;
Node * next;
};

If you want to get complicated:
template<class Data_Type>
struct Double_Link_Node
: public Node<Data_Type>
{
Node<Data_Type> * previous;
};

Although for a doubly linked list, I would just put both link
fields in the same structure.

If this is not for homework, try the STL std::list<> container.
It is already written and tested.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top