ProgrammingLinked Lists QuestionPosted:

Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
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
  • E3 2017
Status: Online
Joined: Dec 25, 20097 Year Member
Posts: 2,305
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
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-
  • Shoutbox Hero
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
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
  • Wizard
Status: Online
Joined: Dec 25, 20097 Year Member
Posts: 2,305
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
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-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
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
  • E3 2017
Status: Online
Joined: Dec 25, 20097 Year Member
Posts: 2,305
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
Gavin- wroteAlso 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, 20134 Year Member
Posts: 4,199
Reputation Power: 765
tortuga wrote
Gavin- wroteAlso 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-
  • Ninja
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
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
  • E3 2017
Status: Online
Joined: Dec 25, 20097 Year Member
Posts: 2,305
Reputation Power: 514
Motto: shouts out to dah for gifting me gold
Gavin- wroteFor 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-
  • Shoutbox Hero
Status: Offline
Joined: Nov 02, 20134 Year Member
Posts: 4,199
Reputation Power: 765
tortuga wrote
Gavin- wroteFor 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.
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS