Solved

Linked-List.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 169
Issues with C++ Class 19 100
Copy output image from TWindowsMediaPlayer 6 48
Need some help with mailto 16 20
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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…

789 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