If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: A task involving linked lists and a headache

As to what's wrong with your loop, each time you delete a node, your list gets smaller. Your loop variable i doesn't account for that. You really shouldn't be forcing array constructs onto a list anyway.

Re: A task involving linked lists and a headache

Originally Posted by MustSeeMelons

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?

If your goal is to delete all of the objects, then why not just keep deleting the head object until there are no objects in the list left to delete?

Code:

while (there are bunnies in my list)
{
delete the head bunny;
}

In other words, get rid of the for loop that uses "i". In any event, it's totally wrong anyway. If your list has 6 bunnies, and they are numbered 1,2,3,4,5, and 6, when you delete bunny "1", then bunny 2 now becomes bunny 1, bunny 3 becomes bunny 2, etc... Your for loop implies that when one is deleted, the other bunnies identifying number doesn't decrease. Your loop counter is going up, while the number of bunnies in the list is going down -- see anything wrong with that?

This is wrong not only in the way you wrote the loop, but the way you implemented the interface to the linked list. You're not supposed to index a linked list like an array, in other words, there is no Bunny[1], Bunny[2], etc.. You can only "point to" or get to a certain item in the list by traversing the list and finding the bunny that you want. So right there, you are not using a linked list in your assignment as a linked list. What you did was write a linked list, and reality using it as if it's an array.

Secondly, even if you were to use indexing into the list as if it's an array, you're mixing up zero-based and 1-based indexing. Why does Delete use 1 based indexing , but GetBunnyInList uses 0-based indexing? Look at your code -- when you call GetBunnyInList, you immediately subtract 1? The user of your linked list doesn't know whether bunnies start at 0 or 1, given the inconsistency of the implementation. Not only that, your internal code gets confusing with the "Pos-1", "Pos+1", etc. You're practically begging for a memory overwrite or some sort of memory corruption to occur with that type of coding. Be consistent and start at 0, where every C++ programmer expects a container to start indexing.

int Add(Bunny* NewBunny)
{
// create a bunny
BunnyInList *InList= new BunnyInList;
InList->Next = NULL;
if (Head == NULL)
{
// this is the head bunny
Head = InList;
return ++size;
}
// there are bunnies already existing, so add this
// new one to the end of the list
BunnyInList* Current=Head;
while(Current->Next)
Current=Current->Next;
Current->Next=InList;
return ++size;
}

This is much simpler. If the list is empty, then the new item becomes the head, else you traverse the list and add the new item at the end.

Re: A task involving linked lists and a headache

Head is still NULL at that point, why is it wrong? Should i just use InList=NULL then?
I tried debugging - it was overwhelming and hard to understand, still searsching for a good debugging tutorial.

My goal is to make a function, which deletes bunnies when they hit age 10. I was afraid that the list was getting smaller when deleting, thanks for making that clear. Understood, start always with 0, i had some sort of reason why i wrote 1, ill fix that.

Ok, now i have to ask this, how should i treat i linked list? Use a counter in loops and go through elements by using Next? The reason im trying to use the list like an array is probably because im used to solid structures, the list seems like it could be scattered all over the memory.

Is there something wrong with using Pos-1 & Pos +1?
Thank you wery much.

Re: A task involving linked lists and a headache

Head is still NULL at that point, why is it wrong? Should i just use InList=NULL then?

Just use the implementation of Add() that I had. Your version of Add() complicates everything by making two allocations when one is all that's required.

I tried debugging - it was overwhelming and hard to understand, still searsching for a good debugging tutorial.

This is what happens when you copy code from somewhere, and you don't fully understand the code yourself.

If you wrote the program from scratch, then debugging the program becomes easy because you wrote the program, so you must have had something in mind when you wrote it. In other words, you had a plan, you had this plan written out on paper, and now you implemented it. So now the program doesn't work -- where does the program deviate from the plan you had written down? Then this becomes straightforward as to how to proceed from there.

The reason im trying to use the list like an array is probably because im used to solid structures, the list seems like it could be scattered all over the memory.

That's right, and for good reason why the memory is "scattered". When you remove an item from a linked list, all you're doing is manipulating a couple of pointers. If it were a (dynamic) array, you have to remove the item, and then "squeeze" all of the items together in the array to cover up the hole that is created with the item that has been removed. That takes more time to execute.

Assume you had a linked list of 10,000 items, and you want to delete the 5,000th entry in the list. You have a pointer to the 5,000th entry, so all you need to do is manipulate the pointers to entry 4,999 and 5,001 to remove entry 5000 (and then of course you deallocate the memory for entry 5000). For an array, to delete entry 5,000 requires you to move all the entries starting from 5,001 to cover up the hole created when 5,000 is removed, one by one. That takes way more time, and especially if the entries are expensive to copy.

In other words, the links in a linked list can be anywhere in memory -- the aspect that keeps them in order is that "Next" pointer link (hence the term "linked list"). For an array, the items must be consecutive in memory. It is the consecutiveness of an array that keeps an array in order (you know where any item is in an array, since each item must follow the previous or next item in memory). Therefore a link in a linked list can be easily removed, since it just needs a couple of pointer updates to remove the item from the list -- it doesn't matter where the link is in memory, as long as the Next pointer is doing the right thing. For an array, to keep the items consecutive, you have to fill the hole created when you removed an item, and that requires copying all elements after the removed item "up by 1".

Is there something wrong with using Pos-1 & Pos +1?

There is nothing "wrong", it's just confusing and makes the code harder to maintain. If you had stuck to 0-based indexing, then things become easier.

Re: A task involving linked lists and a headache

Head is still NULL at that point, why is it wrong? Should i just use InList=NULL then?
I tried debugging - it was overwhelming and hard to understand, still searsching for a good debugging tutorial.

My goal is to make a function, which deletes bunnies when they hit age 10. I was afraid that the list was getting smaller when deleting, thanks for making that clear. Understood, start always with 0, i had some sort of reason why i wrote 1, ill fix that.

Ok, now i have to ask this, how should i treat i linked list? Use a counter in loops and go through elements by using Next? The reason im trying to use the list like an array is probably because im used to solid structures, the list seems like it could be scattered all over the memory.

Is there something wrong with using Pos-1 & Pos +1?
Thank you wery much.

You're right, but as you can see from my mistake, setting next = head and assuming head == NULL makes the code harder to read. If you want it to be NULL, set it to NULL.

Typically you'll iterate a list using a while loop, checking to see if the current node's next pointer != NULL, although some implementations allow for a for loop also.

Re: A task involving linked lists and a headache

Im happy to inform, that this is a happy post - i corrected my mess and everything is working as it should^^ (Maybe there is a small glitch somewhere, but i havent found it yet). Went through my code and found two really dumb mistakes: GetBunnyInList couldnt return a NULL, so changes were made. And the worst mistake, when deleting a node in the middle i had written that the previous nodes next is the next nodes next, that caused all the deletion problems, dont know what i was thinking when i wrote that line..
Want to thank GCDEF and esspecially Paul McKenzie for there great posts
I still have some work to do, like add some more features like:

★ Modify the program to run in real time, with each turn lasting 2 seconds, and a one second pause between each announement.

★★ Allow the user to hit the 'k' key to initiate a mass rabit cull! which causes half of all the rabits to be killed (randomly chosen).

Modify the program so that radioactive mutant vampire bunnies only convert bunnies that end a turn on an adjacent square.
Modify the program so that new babies are born in an empty random adjacent square next to the mother bunny. (if no empty square exits then the baby bunny isn't born)

★★★★★ Modify the program so that it saves each turn to a file and can play back at accelearted speed all subsequent turns.

If ill have any more headackes or finish the program with no problems, ill definetly post here.
Thanks yet again and heres the code, which works.

Re: A task involving linked lists and a headache

Well thank you. Im not racist, but i do like to make my method names a bit more interesting, otherwise its just too plain and booring, without any character. Spices thing up.

I didn't say you were racist, I said the method name was
But seriously, you should consider how the person marking this assignment might see this. They won't be looking for 'interesting' method names, but would prefer that they are clear and descriptive of what the method does.

Re: A task involving linked lists and a headache

Your use of Random makes little sense. You're doing weird things like saying Random(10) to determine sex, when it seems like Random(2) would make more sense. Same with color.

You'd be better off storing attributes as ordinal values rather than string literals. Then you could do things like
sex = Random(2);
without all those if statements. If you use enums or const ints rather than string literals, the compiler will catch it if you make a spelling mistake, whereas it won't with string literals.

What's the difference between size and BunnyCount in your list?

Why are you writing things like
Name=Names[rand()%(14-10)+10
Why not just write 4 if you mean 4?

If you maintained a pointer to the tail of your list, you wouldn't have to iterate the list every time you add a bunny.

There are other issues but if you're so inclined, you could start with them.

Re: A task involving linked lists and a headache

Um, the Bunny doesnt have the next pointer, BunnyInList does, the Bunny it self has no idea that its in a class.
As for the Random function, well, youre right, dont know how i didnt think of that before, thanks.
The BunnyCount is just a ticker on how many bunnies have lived in total, its not decremented, as for now, 2547 is the biggest farm ive had

Why are you writing things like
Name=Names[rand()%(14-10)+10
Why not just write 4 if you mean 4?

Because the Names array contains all gender & mutant names, so i need a random number between 10 and 14 for specific names, i might just make 3 different arrays if ill want to add more names.
The code runs great, so for now ill keep it as it is and will take your advice the next time ill be making something simiral.

Noone is marking this, its just for myself, the code is just for me. I would like the code to be a bit spiced, so that i can think odd things about people based on there code

A question about programming itself, ill be graduating next year and i am willing to learn usefull programming related stuff at home. For now im doing C++ and JAVA, what else could be usefull? To try to land a job or practise position?

Last question, related to the real-time program execution.

"Modify the program to run in real time, with each turn lasting 2 seconds, and a one second pause between each announement."

How to understand the bolded part? I have to restrict the time given for the turn or make a pause? Ive made a function which takes care of the pause.

Re: A task involving linked lists and a headache

Noone is marking this, its just for myself, the code is just for me.

So why didn't you just use the std::list class?

The code runs great, so for now ill keep it as it is

The issue is not that it "runs great" (and that isn't even conclusive). The issue is that you don't want to write code in the way you wrote it. It is not the way a junior or senior professional programmer would write the code. For example, your BunnyList lacks a proper copy constructor and assignment operator. I can crash or totally make your class useless with just a 2 or 3 line main() program. Another example is that you don't check what happens if the color is exactly 24.

Another example is this:

Code:

GetBunny(z)->~Bunny();

This is not how you use destructors. By doing this, your object is erroneously destroyed twice, once with that ill-advised line above, and another when the object goes out of scope.

Honestly, don't keep this program. It has bugs, bad coding, etc.

A question about programming itself, ill be graduating next year and i am willing to learn usefull programming related stuff at home.

Learn how to use the C++ library. There already is a std::list class that you could/should have used. Unless you know how to use the standard library, you really can't call yourself a C++ programmer to any great extent.

To be honest, this isn't even my best attempt. I didn't address the random number issues that GCDEF mentioned, but at the very least, you can start to look at this code and understand how it does what it does.

Re: A task involving linked lists and a headache

Look at your Bunny class again. The most recent one still has a next*.

Still, why write (14 - 10) instead of just 4?

Forgot to delete that pointer for the bunny, anyway, it wasnt used.
Because, rand()%4 would give me 0-4, but those positions contain Male names, i need rand()%(14 - 10)+10 gives me 10-14, which are the names i need.

So why didn't you just use the std::list class?

I had C++ just for 1 semester, one in two weeks, we didnt have classes what so ever, mostly just OOP principles etc.,
So I took the hard road. The upside is that i feel that i have learned something.

Learn how to use the C++ library. There already is a std::list class that you could/should have used. Unless you know how to use the standard library, you really can't call yourself a C++ programmer to any great extent.

Im not saying i am, i am willing to be. Ok then, will look in to your code and start learning STL.I got 8 months to make myself usefull, wish me luck

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.