You are viewing our Forum Archives. To view or take place in current topics click here.
Linked Lists Question
Posted:

Linked Lists QuestionPosted:

Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
So im studying linked lists currently this is the question,,

The removeFirst() removes the first node from a list :

public void removeFirst() {
1 if (first == null)
2 throw new NoSuchElementException();
3 first = first.next;
}


so it first checks to see if the list is empty if so throw the error otherwise advance the cursors postion by one.

now here is the question it says if this code was added to the end what would it do,,


if (first == null)
last = null;




*note the linked list has a queue implemented into it so thats why there is last.

What i could get from it if the list is empty set last to null. Problem is im not sure what last is .
#2. Posted:
tortuga
  • Blind Luck
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
One of the reasons you might have a pointer to the last node in the list is if you're implementing a doubly-linked list. Whatever the case is though, adding the two lines to the end of the above `removeFirst()` function won't change the behavior of the function at all, since it'll throw an exception before it even gets to the second null check. I'm not really sure why we'd even put that check into that function in the first place. Is there some background to the question we're missing?
#3. Posted:
Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Private Link last; -- "which is a reference to the last node in the linked list".

That is all that given it says that that code i mentioned above must be added underneath it


What you are saying is probably right though. I think its a singly linked list though not double.
#4. Posted:
tortuga
  • Fairy Master
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
If it's a singly-linked list, you wouldn't even need to keep track of the last node though. Whichever node whose `next` pointer is pointing to nothing is the last node in your list.
#5. Posted:
Gavin-
  • Fairy Master
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Also if i was to return the node that was removed how would i do that ? the code above just advances the position to the next node.
#6. Posted:
tortuga
  • Fairy Master
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Gavin- wrote Also if i was to return the node that was removed how would i do that ? the code above just advances the position to the next node.

A common pattern in destructive functions that return what was destroyed is to store a local reference to the thing that will be destroyed, before actually destroying/modifying it.

Hopefully that helps without actually giving the answer away!
#7. Posted:
Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
tortuga wrote
Gavin- wrote Also if i was to return the node that was removed how would i do that ? the code above just advances the position to the next node.

A common pattern in destructive functions that return what was destroyed is to store a local reference to the thing that will be destroyed, before actually destroying/modifying it.

Hopefully that helps without actually giving the answer away!


Thank you will look into it

EDIT : Think i got it from a bit of research before you advance the cursors position you create and object equaled to the data of that node then return it at the end.
#8. Posted:
Gavin-
  • TTG Fanatic
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
For the merging of 2 arrays into a third array if im using strings does it go by length or by letter.



Example 1 :

Array 1 : Jack , Joe , Kay , Bill

Array 2 : Ann ,Jill , Bob

Array 3 : Ann , Jack ?

Comparsions :

Jack - Ann = Ann;
Jack - Jill : Jack ( does it go by the length of the string or do i just check the second letter so in this instance jack would go into the array next as its second letter is "a" and Jill has "i".)
#9. Posted:
tortuga
  • Fairy Master
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Status: Offline
Joined: Dec 25, 200914Year Member
Posts: 2,314
Reputation Power: 1686
Gavin- wrote For the merging of 2 arrays into a third array if im using strings does it go by length or by letter.



Example 1 :

Array 1 : Jack , Joe , Kay , Bill

Array 2 : Ann ,Jill , Bob

Array 3 : Ann , Jack ?

Comparsions :

Jack - Ann = Ann;
Jack - Jill : Jack ( does it go by the length of the string or do i just check the second letter so in this instance jack would go into the array next as its second letter is "a" and Jill has "i".)

This doesn't seem like the same question man lol
#10. Posted:
Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
tortuga wrote
Gavin- wrote For the merging of 2 arrays into a third array if im using strings does it go by length or by letter.



Example 1 :

Array 1 : Jack , Joe , Kay , Bill

Array 2 : Ann ,Jill , Bob

Array 3 : Ann , Jack ?

Comparsions :

Jack - Ann = Ann;
Jack - Jill : Jack ( does it go by the length of the string or do i just check the second letter so in this instance jack would go into the array next as its second letter is "a" and Jill has "i".)

This doesn't seem like the same question man lol



I know lmao just didnt want to make a new topic.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.