It's most usual to have a separate List class and a separate Node class. Node is usually very simple. List holds a pointer to the first Node and implements the various list operations (add, remove, find and so on).
It's most usual to have a separate List class and a separate Node class. Node is usually very simple. List holds a pointer to the first Node and implements the various list operations (add, remove, find and so on).
Something like the following class List { public: List() { first = new Node(); } void insert(int val); void remove(int val); // ... and so on ~List() { // ... clean up } private: struct Node { int val; Node* next; Node(int val_ = 0, Node* next_ = 0) : val(val_), next(next_) {} }; Node* first; }; Note that you can place Node outside List if you want to, but this usually doesn't make much sense.
You can definitely template List (and Node through it) to have the value of an arbitrary type. Why else would you need nodes of different classes? – Eli Bendersky Dec 22 '09 at 17:05 My above example was just an abbreviation of a Tetris clone I have written.
I have two classes, Shape and Text. Both of them use linked lists so I can add/remove blocks and text dynamically. Could I template "List" to control the linked lists for both classes with add/remove/insert functionality?
– Ash Dec 22 '09 at 17:15 @Ash: implement a generic templated List class that can hold any object (i.e. Shapes or Text). Then, one instance will be List and another List.
Naturally you can also use STL's ready made 'list', which is already generic – Eli Bendersky Dec 22 '09 at 17:19 Maybe leave a comment in the dtor about deleting the new'd memory? – luke Dec 22 '09 at 18:25.
The latter is definitely preferable. By making the first node pointer a static class member, you are basically saying that there will only be a single linked list in your whole program. The second example lets you create several lists.
The first example has the definite drawback of only being able to create a single linked list in your entire program, so I wouldn't do that. The second works fine, but doesn't shield the user of the class from how the linked list works. It would be better to add a second class, for example named LinkedList, that stores the 'first' pointer and performs list management.
Even better, you could use std::list instead.
In which case, the static option is a non-starter.
You definitely don't want the "first" to be a static. This implies there's only one linked list in your entire program. Static means that every Node in every linked list in your entire program has the same beginning.
That being said you want your Node to have the fewest responsibilities-- It make sense for it to store its value and be able to get to the next Node. It adds complexity to add the job of1 maintaining the "first" pointer. For example what happens when you insert a new element at the beginning?
Then you'd have to update everyone's "first" pointer. Given the two choices above I'd chose the second choice. I would furthermore add a third choice.
Add a "linked list" wrapper that gave you easy access to "first", "last" and allow you to easily insert into and iterate through the list. Something like: class LinkedList { Node* First; Node* Last; public: Node* GetFirst() {return First;} Node* GetLast() {return Last;} // insert after "where" void Insert(Node* where, Node* newNode); ... }.
Not uselessly limiting your code to a single list instance is one very good argument for code variant 2. However, just from superficially looking at the two examples, the sheer number of lines of code also gives a good indication that variant 2 has merits over variant 1 by being significantly shorter.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.