Question
For this and the next problem, consider the following definition of a circular, doubly-linked list with a sentinel head, in which we have defined a
For this and the next problem, consider the following definition of a circular, doubly-linked list with a sentinel head, in which we have defined a set_cursor method, which sets the cursor attribute to a specified index in the linked list where subsequent operations can be performed.
class LinkedList:
def set_cursor(self, idx):
assert(idx < len(self))
self.cursor = self.head.next
for _ in range(idx):
self.cursor = self.cursor.next
def cursor_insert(self, val):
n = LinkedList.Node(val, prior=self.cursor.prior, next=self.cursor)
_________________________________________________
self.length += 1
def cursor_delete(self):
_________________________________________________
_________________________________________________
self.cursor = self.cursor.next
self.length -= 1
cursor_insert should insert the given value val at the current cursor position, and cursor_delete should delete the value at the current cursor position. In both cases, the cursor should refer to the same index as it did before the operation (except in the case of deletion of the last node, which will invalidate the cursor).
Which correctly completes the implementation of cursor_insert?
(a) self.cursor.prior.next = self.cursor.next.prior = n
(b) self.cursor = self.cursor.next = self.cursor.prior = n
(c) self.cursor = self.cursor.prior = self.cursor.next.prior = n
(d) self.cursor.next.prior = self.cursor.prior = self.cursor = n
(e) self.cursor.prior.next = self.cursor.prior = self.cursor = n
Which correctly completes the implementation of cursor_delete?
(a) self.cursor.prior = self.cursor.next
self.cursor.next = self.cursor.prior
(b) self.cursor = self.cursor.next
self.cursor.prior = self.cursor
(c) self.cursor.prior.next = self.cursor.next.prior
self.cursor.next.prior = self.cursor.prior.next
(d) self.cursor.prior.next = self.cursor.next
self.cursor.next.prior = self.cursor.prior
(e) self.cursor.next = self.cursor.next.prior
self.cursor.prior = self.cursor.prior.next
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started