Hi everyone around here, im new and im hopint to give and get help
The task is to create a linked list consisting of bunny's, the bunny is a class.
Im having problems in managing the list itself. I have doubts about the Delete() function in the list, is it correct?
When the bunnies reach 10 years, they should die, that means, that the first 5 bunnies should die all tohether, but they dont, 3 of them do, the other 2 die each the following 2 years. The 2 survivors dont age when the 3 die, but they should. [Bolded]
Objects are added in the back of the list, so that Head points to the very first element. The OutOfFood() function is doing nothing but harm, although it should work. Kills half of them, and then just keeps killing... If they are a lot of them, then it even prints, that nameless or completely all-less bunnies are killed.
Ive noticed, that sometimes, so bunnies are created without something (Color, Name etc.).
Sorry for the long post, the code seems a mess and i dont know where even to start cheking.
Thanks in advance!
You're passing in a Bunny*, but ignoring it and creating a new Bunny. You should just use the pointer you passed in. Also, you're setting next to head. That's a mistake too. Next in the last node should point to NULL.
From a design point of view, your list class should have a node class or struct that has the Bunny* as a member, and handles the next pointer. It's bad design to have the Bunny class contain the next pointer.
I would imagine there are lots more problems, but when writing code, you need to write small chunks, debug them and make sure they're working, then move on to the next piece. When you try to write it all at once, you end up with a huge mess. Get the list class properly designed, get the AddBunny function working properly, then move on and debug it piece by piece.
Thank you for your reply and advice Will tear this mess down peace by peace and learn to debug. I got the idea how to write the add function from a tutorial, guess it was wrong or i miss understood something and that propably means i might have some other mistakes..
Trying to figure out the steps needed to manage the head/tail/next/previous pointers for all the operations for a linked list can get a little mind boggling after a while.
My choice for solving problems like this is the draw the sequence of steps that are needed for each operation (Add, Delete, Insert), with lines showing where each pointer points to at each step. Otherwise it's easy to get yourself in a muddle.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
I got the idea how to write the add function from a tutorial,
That is the wrong approach. No tutorial, unless it is geared towards a definite aspect of an application, is going to write the program for you.
If you are to look at source code, then it should only be used to see how "programmer x" implemented a linked list. What "programmer x" did may not even look like or fit the requirements that you're looking for. What if that programmer implemented a template-based, STL-like linked list, complete with iterators, etc? Are you going to blindly copy the code or copy major portions of it? Of course not.
Googling or web searching for the exact code that fits what you're doing is a waste of time and energy, when that time could have been spent writing the program. It's better you learn what a linked list is, and based on that learning, create the program yourself.
You should have had a data structures textbook describing what a linked list is -- it's your job to then take the description of the linked list and create a program that manages a linked list.
@JohnW@Wessex
Thats what I am going to do, cant put everything together in my head.
@Paul McKenzie
Thanks for the post. I learned what a linked list is from tutorials, I went through 3 if i remmember corectly, but I didnt just plain copy the code, i tried to undesrstand and by what i learnt i made this mess. I know that copying gets you nowhere, i try to understand what and why i am writing something.
Reading just the description in not always enough, especially[did google check this word] if you dont have much experiance.
@GCDEF
Could you please explain this? I cant wrap my head around the idea..
"From a design point of view, your list class should have a node class or struct that has the Bunny* as a member, and handles the next pointer. It's bad design to have the Bunny class contain the next pointer."
@GCDEF
Could you please explain this? I cant wrap my head around the idea..
"From a design point of view, your list class should have a node class or struct that has the Bunny* as a member, and handles the next pointer. It's bad design to have the Bunny class contain the next pointer."
You need another class or struct to make up the elements of your list. For example
Code:
struct BunnyListNode
{
BunnyListNode* pNext;
BunnyListNode* pPrev;// if you want doubly linked
Bunny* pBunny;
};
That way you've decoupled the concepts of your Bunny class and the container that holds them. When you call AddBunny, you'd create a new BunnyListNode and set pBunny to point to the Bunny that node is containing, and set the BunnyListNode pointers instead of having your Bunny class responsible for its position in the list.
Well ok, thanks, understood the idea. When i create a new bunny, i create a struct inside, that has the information about the bunnies location and its neighbours. The bunny itself now is just data and has nothing to do with the list itself.
Could you try explaining why my method was inferior? Unless my previous sentence is the reason.
I don't really understand your previous sentence. You create a struct inside what?
Whenever possible you should decouple classes as much as possible. There's no reason Bunny should know anything about the list, or any other container that may hold them. When you're working with Bunny objects, you shouldn't have to be concerned with whether they're members of a list, and if you change the implementation of your list, you shouldn't have to change your Bunny class to accommodate it. While it's not always possible, good design dictates that you reduce inter-dependencies among classes as much as possible.
data would typically be a pointer to MyClass though.
Is the reason to decrease the size of the nodes? Most examples don't have data as a pointer, but then again, most examples use primitives for the data.
It's so that the list can store the actual object, not a copy of it, and again, it decouples the class from its container. If the list contains an actual object, that that object only exists as long as the list node exists, and that's not necessarily desirable behavior.
Dont have much time, but i have done some changes, created another class that handles the bunnies in the list. The sad parts is that nothing has changed :| If i create 5 bunnies and then delete with a simple cycle (Bold), all is whel, they are deleted, but if i make a cycle with aging (Bold), 2 of them are still not deleted.. Whats wrong with the cycle?
Bookmarks