Computer Science Canada I need help with my linked list program. |
Author: | RGB255 [ Thu Nov 16, 2006 8:15 pm ] | ||
Post subject: | I need help with my linked list program. | ||
We started linked lists today at school and I sort of get how the adding code works to add to the list but I don't know how to make code that will "remove" items from the list. Any help on this would be awsome. Thanks.
|
Author: | wtd [ Fri Nov 17, 2006 12:11 am ] |
Post subject: | |
With all due respect, if you're using an array, then you don't have a linked list. |
Author: | RGB255 [ Fri Nov 17, 2006 8:11 am ] |
Post subject: | |
So how would I go about making a proper linked list? |
Author: | rdrake [ Fri Nov 17, 2006 9:17 am ] |
Post subject: | |
If you look at the [WIP] C Whirlwind then you will see an example of a linked list in C. Not sure if that helps, though. |
Author: | wtd [ Fri Nov 17, 2006 2:19 pm ] |
Post subject: | |
A linked list is formed from nodes. Each node contains a value of some type, and a link to another node. In a language like C or C++, this will often take the form of a pointer to another node. If the pointer is NULL, then the current node is assumed to be the end of the list. Other languages may have different ways to represent this boolean logic. |
Author: | RGB255 [ Wed Nov 22, 2006 8:55 pm ] |
Post subject: | |
Okay the code may not be a true linked list but can anyone help me with the code that will allow me to replace an item in the list and then allow me to keep adding to the end of the list? |
Author: | wtd [ Thu Nov 23, 2006 12:48 am ] |
Post subject: | |
I hate to harp on this incessantly, but style matters Indenting the contents of your subroutines would make it much easier to trace what is going on. ![]() Of course, more of an MVC approach would help too, with the code to manage a "linked list" kept separate from the code for the UI. The problem with adding to the end of the list is that your solution is based on an array. The array is of fixed size, and thus cannot grow in the same way a true linked list can. |
Author: | RGB255 [ Thu Nov 23, 2006 1:24 pm ] |
Post subject: | |
Well this is just supposed to be an example to show us the concept so I didn't bother to make it in a dynamic array. The problem is the teacher left us to figure out the code that will allow us to replace an item in the list. The problem I get is that when I try to replace an item everything gets out of order and doesn't work. |
Author: | Silent Avenger [ Thu Nov 30, 2006 6:34 pm ] |
Post subject: | |
Well linked lists are complicated things so I won't just give you the answer to your problem but I'll give you hints and in this way you'll learn how the linked list works. First of all if if you look how your initial links are set up you'll see a pattern which is that each empty spot points to the next empty spot. So you learn from this that you'll also need to change the link of the spot you're trying to remove. Also if you try running your program a few times you'll notice that if you remove a spot it doesn't really change the order of the list so you'll also have to change the link of the item that pointed to your removed item. With this you should be able to figure out the code that you need to remove an Item. |
Author: | RGB255 [ Thu Nov 30, 2006 6:55 pm ] |
Post subject: | |
Okay thanks for the tips. I'll try them out. |
Author: | Silent Avenger [ Thu Nov 30, 2006 7:15 pm ] |
Post subject: | |
One other thing to tell you the code that you're going to come up with isn't going to remove the first item so you'll have to do some thinking. |
Author: | RGB255 [ Thu Nov 30, 2006 7:30 pm ] |
Post subject: | |
Thanks it worked! |
Author: | Silent Avenger [ Thu Nov 30, 2006 7:39 pm ] |
Post subject: | |
You're welcome. See now that you know how it works it makes it a lot easier to figure out the code to remove an item. |