![]() |
In COmputer Science at the moment we are implementing Linked lists. Which was fine at first. Adding and deleting nodes from the head was simple enough, but now we must insert nodes in order, and delete specified nodes. THe teacher isn't giving us any help, but we are allowed to research it, so I figure asking here is fair game :D .
I don't have the code so far with me, as its at school only, but then, I'm asking you guys for it anyway. Any help would be much appreciated. [img]tongue.gif[/img] Expect more questions as the week progresses, we're having a whole week of this sort of stuff ;) |
Yeah right like I'm gonna give you the code (was 4 years ago I did it anyway, prob don't have it any more) [img]tongue.gif[/img]
It's simply, just traverse the list looking at each node's pointer to "next" until you find the place you need to insert (just use a while loop). For deleting you can just go through the list again with a while loop until you find the right thing. Simple. I'll answer more specific questions properly if I can. |
Well, I suppose I wasn't exactly expecting the code [img]tongue.gif[/img] .
WHat you have suggested, I think I've already tried. Here's what I have. The ListNode class creates objects with a String variable and another ListNode object within them. The first one is called head, and so this is the only one that I actually know the name of. To traverse the list, I created a new ListNode called positio, and set it equal to head. I then have a while loop to set position to position.getNextNode until I find what I am looking for. It is here that I encounter the problem. If I simply do the following: position = new ListNode(addData, position); Then position will not be a part of the list itself, and the main list will be unchanged. Will it not? Edit: Just noticed the word thanks doesn't appear in my post ;) Thanks, and thanks in advance. [ 03-14-2005, 06:41 PM: Message edited by: Callum ] |
Well, why can't you do:
ListNode temp = current.NextNode; current.NextNode = position; position.NextNode = temp; ? where current is the current node in the list just before the place you need to insert. You can do a similar thing to delete. |
Ahha! Thank you oh wonderful one! :D
|
All times are GMT -4. The time now is 12:57 PM. |
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved