Results 1 to 2 of 2
Thread: question about linked lists
- 11-11-2007, 01:27 AM #1Member
- Join Date
- Nov 2007
- Rep Power
question about linked lists
I would like to start off saying hello, and I also want to apologize if this seems to be a remedial question... I am just drawing a blank, and have been yet to find a solid answer to my question... anyways, I am trying to learn as much as possible about java and I am currently looking into linked list structures in depth, specifically doubly linked lists. I am a little confused as to how the addBefore(position<P> p, Element E) mehtod works. This is the way it is written in my book. Basically, the method is supposed to insert a new node with the data of E before a node p. I am a little confused as to how to reference nodes in a doubly linked list when the node is being refered to by its data (Element). from what I understand, if you have the doubly linked list H<=>1<=>2<=>4<=>t and you call addBefore(4,3), the result should be H<=>1<=>2<=>3<=>4<=>t. This is fine for a sorted list, but what happens if you have multiple nodes with the same data in it? and is that possible? (ie. H<=>2<=>1<=>2<=>t thus when calling the addBefore(2,1) mehtod, 1 would be entered before the first 2, and not the second.)
I guess my basic question is how does the position() mehod work in a unsorted list, and can it work if there is multiple elements in the list with the same data? Any help will be much appreciated. thank you.
- 11-11-2007, 08:33 AM #2
I guess my basic question is how does the position() mehod work in a unsorted list, and can it work if there is multiple elements in the list with the same data?
Looks like you are inserting a value at an index (position) into the list. If this is so then all you care about is that you can find the position, ie, 0 <= position < lastElement. It shouldn't make any difference what the values on either side are or if there are multiple values.
If you were inserting a value after another value (vis-a-vis poisition), ie,
addBefore(Element E_in_list, Element E)
then you would likely insert it before the first instance of the value ("E_in_list") that you find in the list.
- By gapper in forum New To JavaReplies: 4Last Post: 01-20-2008, 09:01 AM
- By JavaBean in forum Java SoftwareReplies: 0Last Post: 08-10-2007, 04:39 PM
- By JavaNoob in forum New To JavaReplies: 2Last Post: 08-08-2007, 03:11 PM
- By oregon in forum New To JavaReplies: 2Last Post: 07-25-2007, 08:25 PM
- By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-11-2007, 03:32 PM