Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linked-List.

Posted on 2003-11-15
4
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 120 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 80 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

719 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