
1. The textbook’s unordered list ADT uses a singly-linked list implementation. I added the
_size
and
_tail
attributes:
data next data next data next data next
_head
_tail
_size 4
'w' 'a' 'y' 'c'
UnorderList Object
a) The
search(targetItem)
method searches for
targetItem
in the list. It returns
True
if
targetItem
is in the list;
otherwise it returns
False
. Complete the
search(targetItem)
method code:
class UnsortedList:
def search(self, targetItem):
b) The textbook’s unordered list ADT does not allow duplicate items, so operations
add(item), append(item),
and insert(pos, item)
would have what precondition?
c) Complete the
append(item)
method including a check of it’s precondition(s)?
def append(self, item):
d) Why do you suppose I added a
_tail
attribute?
e) The textbook’s
remove(item)
and
index(item)
operations “Assume the item is present in the list.” Thus, they
would have a precondition like “Item is in the list.” When writing a program using an UnorderedList object (say
myGroceryList = UnorderedList()
), how would the programmer check if the precondition is satisfied?
itemToRemove = input(“Enter t he item to remove from the Gr ocery list: “)
if
myGroceryList.remove(itemToRemove)
Data Structures (CS 1520) Lecture 8 Name:_________________