Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Python] Need help debugging. Instructions, my code, and sample cases are below!

ID: 3741281 • Letter: #

Question

[Python] Need help debugging. Instructions, my code, and sample cases are below! I'm having trouble with ordered_ll.tail.

def add(self, item):
#write your code here
temp = Node(item)
current = self.head
previous = None
stop = False
  
while current != None and not stop:
if current.getValue() > item:
stop = True
else:
previous = current
current = current.getNext()

if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
  
self.count+=1

Instructions: In class, we worked on the implementation of a linked list. That data structure keeps the elements of the linked list unsorted. Based on the LinkedList code, implement the data structure OrderedLinkedList with the following characteristics: . OrderedLinkedList) creates a new ordered list that is empty. It needs no parameters and returns nothing. Assume the items in the list are unique . add(item) adds a new Node with value-item to the list making sure that the ascending order . delete(item) removes the Node with value-item from the list. It needs the item and modifies . search(item) searches for the Node with value-item in the list. It needs the item and returns is preserved. It needs the item and returns nothing. [40 pts] the list. You can assume the Node is present in the list. [20 pts] a boolean value. [10 pts] . pop) removes and returns the last Node in the list. It needs nothing and returns an the value of the Node. [20 pts] .isEmpty0 tests to see whether the list is empty. It needs no parameters and returns a boolean value. [5 pts] size) returns the number of items in the list. It needs no parameters and returns an integer [5 pts] * NOTE: There is no partial credit for methods that do not work properly. Code will be tested calling all methods and comparing the final list EXAMPLE >>>ordered 11-OrderedLinkedList () >>> ordered 1 1 add ( 8 ) >>> ordered 11.add (7) >>>ordered 11.add (3) >>>ordered 11.add (-6) >>>ordered 11.add (58) >>>ordered 11.add (33) >>> ordered 11.add (1) >>>ordered 11.add (-88)

Explanation / Answer

The sample cases that you pasted looked okay. You should paste them as text and not as image.

In add funtion you are not updating the tail. For the first item your head and tail will be changed from None to something and also will be equal and then on it will depend if head/tail changes or not.

If new item smaller than head then only update head, if larger than tail then only update tail and if between head or tail then need not update.

So the change in add funtion code will be

def add(self, item):
#write your code here
temp = Node(item)
current = self.head
previous = None
stop = False
  
while current != None and not stop:
if current.getValue() > item:
stop = True
else:
previous = current
current = current.getNext()

if previous == None:

if stop == False: #Means 1st item, otherwise previous != None or stop != False
self.tail = temp

temp.setNext(self.head)
self.head = temp
else:

if current == None: # Means last element is updated, so make new tail

self.tail = temp

temp.setNext(current)
previous.setNext(temp)
  
self.count+=1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote