Solved

Linked-List.

Posted on 2003-11-15
4
415 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 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

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

630 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