Implementing an Unordered List in CS 1520: Search, Append, and Remove Operations, Study notes of Data Structures and Algorithms

The implementation details of an unordered list adt using a singly-linked list in cs 1520. It covers the search, append, and remove operations, including their preconditions and method codes. The document also discusses the need for additional data attributes to aid the implementation of remove and index methods.

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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:_________________
L
ecture 8
- Page
1
Docsity.com
pf2

Partial preview of the text

Download Implementing an Unordered List in CS 1520: Search, Append, and Remove Operations and more Study notes Data Structures and Algorithms in PDF only on Docsity!

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 the item to remove from the Grocery list: “)

if myGroceryList.remove(itemToRemove)

Data Structures (CS 1520) Lecture 8 Name:_________________

Lecture 8Docsity.com - Page 1

f) The remove(item) and index(item) methods both need to look for the item. What is inefficient in this whole

process?

g) Modify the search(targetItem) method code in (a) to set additional data attributes to aid the implementation of

the remove(item) and index(item) methods.

h) Write the index(item) method including a check of its precondition(s).

def index(self, item):

i) Write the remove(item) method including a check of its precondition(s).

def remove(self, item):

j) Write the pop(position) and pop() method including a check of its precondition(s).

def pop(self, position = None):

Data Structures (CS 1520) Lecture 8 Name:_________________

Lecture 8Docsity.com - Page 2