   [C++] Linked lists. Am I doing it right?
  Hey,I've spent the last few hours trying to implement a linked list to manage the inventory in my game and I think I've suceeded. It works fine, but I keep wondering whether I'm doing it right and if there's a better, more efficient way to implement the linked list.Here's my code:struct SItemList{CItem *item;int amount;SItemList *next;SItemList *previous;};class CInventory{private:SItemList *head;SItemList *tail;public:CInventory();SItemList *GetItem(int count); // returns a SItemList which is located "count" items away from the head.void AddItem(CItem *item, int amount);void RemoveItem(CItem *item, int amount);};The player class then gets an instance of the inventory class to sort its items, though it does add some long calls like player.inventory.GetItem(itemnum)->item->GetName();Anything that could be done to improve this system?   
  A faster and more efficient way to write a linked list is to use std::list, found in the list standard header provided by your compiler.However, given the aspect of what you're trying to do, I might argue that a dynamic array would be a better choice (instead of a linked list), for instance as std::vector or boost::ptr_vector, or perhaps an associative container such as std::map if you had a simple order relationship over your items.   ;  Well you did properly implement a doubly linked list, but judging by the only function you have to read data from your list "SItemList *GetItem(int count);" you really want a list that supports random access of its members. Perhaps an std::vector of SItemList objects (minus the next,previous for the LL) would fit your problem a little better.;  EDIT: I'm too slow.   ;  You'll have to wait for a C++ programmer to come around and tell you the syntax, but the biggest issue I see is that you've made a linked list that's specific to your inventory. It's better to make a generic linked list class that can hold any kind of object. Your inventory would be an instance of LinkedList<CItem>, meaning a linked list that holds objects of type CItem. This way you don't need to reimplement a linked list every time you want to use one for a different kind of object.As you become a more experienced programmer, I would suggest not writing your own LinkedList class at all and instead just using libraries.However, its a good idea to write your own data structure classes at first so you get an understanding of how they work.   ;  So it seems it'd have been better if I used std vectors or lists, as they seem very easy to use and provides the functionality that I need. I'll read more about them and try doing this with vectors a bit later, meanwhile, Hombert brought up a good point. I can't really do much with my items since the list consists of pointers CItem instances, while I have a ton of classes derived from the CItem that i'll use in the list (weapons, armor, potions and whatnot), since the only thing the CItem class has is a name and a cost variable, I can't access any members of the derived classes from the inventory. Any advice on solving that? I didn't really understand what you meant, Hombert, as it looks exactly like what I have now.   ;Quote:I can't really do much with my items since the list consists of pointers CItem instances, while I have a ton of classes derived from the CItem that i'll use in the list (weapons, armor, potions and whatnot), since the only thing the CItem class has is a name and a cost variable, I can't access any members of the derived classes from the inventory. Any advice on solving that?You're wrong. A list that contains pointers to a base class (preferrably std::list<CItem*>) can contain instances of any class that inherits from that base class. That is the point of polymorphism and inheritance.Example (may contain typos):#include <list>#include <iostream>using namespace std;class Foo{ public: virtual void print() { cout << "Foo" << endl; };};class Bar : public Foo{ public: virtual void print() { cout << "Bar" << endl; };};int main(){ Foo* foo = new Foo; Bar* bar = new Bar; list<Foo*> exampleList; exampleList.push_back(foo); exampleList.push_back((Foo*) bar);  list<Foo*>::iterator it; for(it = exampleList.begin(); it != exampleList.end(); ++it) {  (*it)->print(); }}The above code will print:FooBarTo use functions available in Bar but not in Foo, you need to do an unsafe cast. That is generally not recommended; the need to do so is indicative of bad design.   ;  Is that only true when using std lists? Because I've tried accesing a potion item, that has an integer called effect through my linked list of items (the same as in my OP) and it said that there's no such variable in "CItem".   ;Quote:Original post by LeigherHey,I've spent the last few hours trying to implement a linked list to manage the inventory in my game and I think I've suceeded. It works fine, but I keep wondering whether I'm doing it right and if there's a better, more efficient way to implement the linked list.Here's my code:*** Source Snippet Removed ***The player class then gets an instance of the inventory class to sort its items, though it does add some long calls like player.inventory.GetItem(itemnum)->item->GetName();Anything that could be done to improve this system?Yes, you are doing it correctly. You could finish it by creating Stack and Queue classes which inherit this one, and have the methods push and pop. queue push/pops the tail, stack push/pops the head.yes, you are better off using std::list.And yes, you will do well to get the hang of linked lists anyway, if you plan to program as a career, because you won't always have the benefit of STL type stuff. There isn't much point, other than for learning purposes, of re-writing stuff like this where std::list can do it for you. There are, of course, times where you cannot use std::anything for reasons of compatibility, or using a different language. In these situations, learning this stuff now and touching up on it every now and then will benefit you.;Original post by Hnefi...exampleList.push_back((Foo*) bar);...Slightly of topic, just to point out a little detail. The code above works but still it is preferable to write "exampleList.push_back(bar);" without the C-style cast. With the cast it contains a seed of a serious error. Without the cast the compiler is able to figure out the right cast if it is possible. If it isn't possible, it'll produce an error. Consider if Bar isn't derived from Foo.With the cast, even if the Bar isn't derived from Foo, the code will compile. Casting an object of one class to some other will produce undefined results and very likely will crash and burn your program.Best regards!