Hi. I've been working with linked lists for a bit, but I've been having some trouble with them. My programming book does a good job in helping you make a linked list, but it does a bad job of showing you how to use them.

What I am trying to do is make an unordered linked list of classes, where you can access the function of each individual class but I'm just completely befuddled at this point. I can make the linked list store classes, but I have no clue how I would be able to "get" the classes inside the list and use their functions.

If someone could show me an example of how to use a linked of classes correctly It would be much appreciated.

Recommended Answers

All 3 Replies

What do you mean by an unordered linked list of classes? I don't understand that bit. I think you mean objects? To "get" objects from a linked list, I suppose you mean, how to access the data at a particular node?

What you usually do, is create a separate iterator class, which will have functions like start() forward() or similar. start will create a new iterator which points to the start of your list, forward will move that pointer forward by 1 etc.. Then, to access the data at each of those nodes, you dereference the pointer.

The only way to add classes to a linked list is to create objects/instances of those classes and add them to a list.

If you want multiple classes in the same list, you'll need to use inheritance and create child classes of a parent so they can be considered a type the same as the parent. In that case, you'll also need to know what type each one is in order to properly cast the object to get to any non-inherited functionality.

Sorry I was a little busy recently so I wasn't able to reply.

>>What do you mean by an unordered linked list of classes?
When I mean an unordered linked list of classes. I mean a linked list that stores a "singular" type of class which does not need to be sorted while/after it is inserted. My project that I am trying to do is make a building, which has a linked list floors, obviously I do not change floor #62 to floor #1 unless there is a catastrophic earthquake.(Which is unfortunately not part of my project. :()

The problem is, I just can't figure out how once I add classes to the list, how to retrieve them. I want to be able to get the first node of the list, then go through the list until I get to the desired class that I want to achieve, but so far, all I seem to be able to do is access temporary memory that resets after the next run, defeating the entire purpose of my program.

Right now though, I just decided to ditch my regular linked list and go to a doubly liked list.(a linked list with a pointer to the next and previous node). There's not much difference, It can just be run without the help of another header file. It's not my source code, but it's only for practicing purposes. I could make my own linked list, but I would basically be copying from the book anyways, except with a bunch of errors along the way and I don't have enough time for that right now.

Could someone tell me how to use this linked list though? I put a text file down below. I promise it's not for homework, I know how annoying is for someone giving you a txt file and say "do this" can be. I'm just trying to know how to use linked lists right.

Ironically, I know how to use linked stacks and queues but a regular linked list has me stumped...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.