Page 1 of 1

a) In an unordered list, new items are added to the head. What is the content of the list after running the code below?

Posted: Mon May 09, 2022 5:53 am
by answerhappygod
a) In an unordered list, new items are added to the head. What
is the content of the list after running the code below? The items
should be listed in the order counting from the head.
my_list = UnorderedList()
for num in [17, 7, 23, 45, 4, 3]:
my_list.add(num)
my_list.remove(3)
my_list.add(32)
b) search_loop is a function to search a singly linked
list. Its expected results are illustrated below. Write a function
that implement the same effect in a recursive algorithm.
def search_loop(list_head, item):
cur = list_head
found = False
while cur != None and not found:
if cur.getData()
== item:
found
= True
else:
cur
= cur.getNext()
return found
my_list = UnorderedList()
for num in [5, 7, 3, 9, 1]:
my_list.add(num)
search_loop(my_list.head, 3) # returns True
search_loop(my_list.head, 2) # returns False