Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [source] Double-Linked List
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Catalyst




PostPosted: Wed Oct 08, 2003 8:56 pm   Post subject: [source] Double-Linked List

This a double linked list. Its the equivalent of std::list (speed-wise, std::list is about 8x faster in add elements, but they are equal in iteration speed)

To compile these ull need to turn off compilation and linking for list.cpp
or just rename it list.inl (that will turn those off automatically

note: thers a bug in the iterator this code would go thru the entire list

code:

List<float>::ListIterator i;
    for (i=test.Front();i!=test.Back()->nextNode;i++)
    {       
        hold=(*i);
    }



List.cpp
 Description:

Download
 Filename:  List.cpp
 Filesize:  10.3 KB
 Downloaded:  1142 Time(s)


List.h
 Description:

Download
 Filename:  List.h
 Filesize:  4.84 KB
 Downloaded:  822 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Feb 11, 2004 1:32 am   Post subject: (No subject)

A few things I would change:


  • Naming: change ListNode and ListIterator to simply Node and Iterator. List<Something>::ListIterator is redundant.
  • In your documentation you refer to those as subclasses. This term is understood by most to indicate inheritance. Terms like "inner class" or "nested class" are more appropriate.
  • In your comparison operators (line 155 in List.h), you pass in a List object. You've done two things wrong. First, the List class is generic, so you need to use List<T>. Second, you're passing them by value, rather than reference. This means a copy will be constructed and passed into the function each time. This is expensive. Using a reference solves that. That reference should be a constant reference also, like so:

    code:
    bool operator==(const List<T>& one);

  • Which brings me to... any member functions which can be called without changing the state of the object should have a "const" qualifier.
  • When dealing with templates, unless I'm out of the loop on a crazy new development, you can't separately compile your List.cpp file. Put the whole thing in one big header file. After all, you're not actually defining any classes or functions... just templates for some that will be created by the compiler as needed.
  • In the List class, you have:

    code:
    List<T>::ListNode *headNode,*tailNode;


    You're inside the List<T> class, so you can simply write this as:

    code:
    ListNode *headNode,*tailNode;



Can't find anything else at the moment, but I'll let you know if I do.
Catalyst




PostPosted: Wed Feb 11, 2004 6:59 am   Post subject: (No subject)

Thanks for the tips
They're very helpful Very Happy

But a few things


  • As for the naming scheme the inner-classes were originally outside the
    main class declaration (and this was part of a larger project) so i felt
    that i should have named them that. I guess i got lazy afterward when i
    moved them into the class

  • At the point this was written i had never heard of inherited classes being referred as subclasses, and i thought the term fit here

  • for the templates i didnt like the idea of having all the code in the
    header so i moved it to a .cpp and turned off complilation and linking
    on the file (saving as .inl would probably be better tho)

wtd




PostPosted: Wed Feb 11, 2004 5:23 pm   Post subject: (No subject)

Catalyst wrote:
As for the naming scheme the inner-classes were originally outside the main class declaration (and this was part of a larger project) so i felt that i should have named them that. I guess i got lazy afterward when i moved them into the class


Refactoring code sucks, doesn't it? Wink

Catalyst wrote:
At the point this was written i had never heard of inherited classes being referred as subclasses, and i thought the term fit here


Yeah, it's a fairly common way to refer to "child classes".

Speaking of inheritance, you might save yourself a lot of work by inheriting from the std::list class and simply adding the features you want.

code:
#include <list>

template <typename _t>
class my_list : public std::list<_t>
{
   // your features here
};



Catalyst wrote:
for the templates i didnt like the idea of having all the code in the header so i moved it to a .cpp and turned off complilation and linking on the file (saving as .inl would probably be better tho)


Like it or not, that's where it all belongs. Only things that can be compiled should go into a *.cpp file. Declarations of things (including templates) belong in a header file.
Catalyst




PostPosted: Wed Feb 11, 2004 5:57 pm   Post subject: (No subject)

i intentionally reinvented the wheel this time Laughing
I thought it would be a good experience making some basic container classes
wtd




PostPosted: Wed Feb 11, 2004 8:16 pm   Post subject: (No subject)

Catalyst wrote:
i intentionally reinvented the wheel this time Laughing
I thought it would be a good experience making some basic container classes


It is, but now learn how to work with the STL. Wink
Catalyst




PostPosted: Wed Feb 11, 2004 8:51 pm   Post subject: (No subject)

already do
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: