0% found this document useful (0 votes)
145 views2 pages

Chap3 HW

If the lines that reset the tail field to null when deleting the last node are removed from the removeFirst method of the SinglyLinkedList class, the class would still work but yield incorrect results. Specifically, it would not be able to reliably detect an empty list since the tail field would still reference the previous last node even if the list is empty. This can lead to inconsistencies in subsequent operations. To find the middle node of a doubly linked list, traverse the list with two pointers moving at different speeds - one at twice the speed of the other. The slower pointer will reach the middle when the faster pointer reaches the end. This takes O(n) time. Swapping two nodes in a singly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views2 pages

Chap3 HW

If the lines that reset the tail field to null when deleting the last node are removed from the removeFirst method of the SinglyLinkedList class, the class would still work but yield incorrect results. Specifically, it would not be able to reliably detect an empty list since the tail field would still reference the previous last node even if the list is empty. This can lead to inconsistencies in subsequent operations. To find the middle node of a doubly linked list, traverse the list with two pointers moving at different speeds - one at twice the speed of the other. The slower pointer will reach the middle when the faster pointer reaches the end. This takes O(n) time. Swapping two nodes in a singly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Samantha Sinnerine

CSCI-313
Chap 3 HW

R-3.5 - The removeFirst method of the SinglyLinkedList class includes a special case to
reset the tail field to null when deleting the last node of a list (see lines 51 and 52
of Code Fragment 3.15). What are the consequences if we were to remove those
two lines from the code? Explain why the class would or would not work with
such a modification.

If the lines that reset the tail field to null when deleting the last node are removed from the
removeFirst method of the SinglyLinkedList class, we would not be able to detect an empty list.
By resetting tail to null when deleting the last node, it becomes a reliable indicator of an empty
list. Without those lines, the tail field will still reference the previous last node, even though the
list is empty. The class will work but yield incorrect results. For example, when appending a new
node to an empty list, the tail will still reference the previous last node. This can lead to
inconsistencies and confusion in subsequent operations that rely on the correct tail reference.

R-3.8 - Describe a method for finding the middle node of a doubly linked list with header
and trailer sentinels by “link hopping,” and without relying on explicit knowledge
of the size of the list. In the case of an even number of nodes, report the node
slightly left of center as the “middle.” What is the running time of this method?

To find the middle node of a doubly linked list, we can traverse the list with two helper pointers:
one moving at twice the speed of the other. The faster pointer will reach the end of the list twice
as quickly ([Link]) as the slow one ([Link]), thus the slow pointer will reach the
middle of the doubly linked list when the fast pointer has reached the end. The running time of
this would be O(n).

C-3.27 - Describe in detail how to swap two nodes x and y (and not just their contents) in
a singly linked list L given references only to x and y. Repeat this exercise for the
case when L is a doubly linked list. Which algorithm takes more time?

To swap two nodes, x and y, in a singly linked list L, given only references to x and y, start by
finding the previous nodes of x and y in the list while traversing through it. Once we have the
previous nodes, we update the next references accordingly. If x is the head, we update the head to
point to y. Otherwise, we update the next reference of the previous node of x to point to y.
Similarly, if y is the head, we update the head to point to x. Otherwise, we update the next
reference of the previous node of y to point to x. Finally, we swap the next references of x and y
to adjust the links.

In a doubly linked list, obtain the previous and next nodes of both x and y and update the
previous and next references of the nodes surrounding x and y to redirect them to their new
positions. If x has a previous node, we update its next reference to y, otherwise, we update the
head of the list to point to y. Similarly, if x has a next node, we update its previous reference to y.
We perform similar steps for node y, updating the references of its previous and next nodes
accordingly. Finally, we swap the previous and next references of nodes x and y to complete the
swapping process.

The algorithm for swapping nodes in a doubly linked list takes more time compared to the
algorithm for swapping nodes in a singly linked list.

You might also like