Solved

Linked-List.

Posted on 2003-11-15
4
410 Views
Last Modified: 2013-12-14
To C++ experts,

    When we want to creat a linked-list, is it better to declare  the "ListNode" class and the "Itarator" class as members of the "List" class ? or better to make them separated classes with friend functions ?

    Similarly, should the "TreeNode" class and the "TreeIterator" class be members of the "Tree" class  ? or better to make them separated classes ?

Thanks.

meow.
0
Comment
Question by:meow00
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 30 total points
ID: 9754861
Hi meow00,

If the Iterator class is going to be used only to iterate the List class, I would create it as Inner classes of the List class, as it's existence is limitated only to that class.

I would definitely create the ListNode class as an Inner class of List, as like the case before it's existence is limitated to the List class (ListNodes will only be part of a List, they won't work with other classes).


Example with both classes as inner classes

public class List
{
    public class ListNode
    {
        // Code
    };
    public class Iterator
    {
        // Code
    };
}



Hope this helps
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9757343
meow00:

>     When we want to creat a linked-list, is it better to declare  the "ListNode"
> class and the "Itarator" class as members of the "List" class ? or better to
> make them separated classes with friend functions ?

You should study the Standard Template Library, and see how they do it in there.  If you want to be good at C++, it behooves you to learn the STL.  It is very handy.

Hope That Helps,
Dex*
0
 
LVL 1

Author Comment

by:meow00
ID: 9759171
Yes, but some STL books have Iterator class and ListNode class inside the List class. However, others prefer to make them different classes. I am sure both ways work, but I don't know which method is better in different situations ....... thanks .....
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 20 total points
ID: 9764926
Both classes should be derived internally to the list class.  That way, other classes only need to know about the list, and they automatically get access to the node classes (if needed), and to the iterator classes via the typedefs or definitions of the class.  For example:
    List::iterator itr = l.begin();

Anything knowing about List, automatically knows about List::iterator.  If they were separate, this would not be guarenteed.

Dex*
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now