Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data structure lecture note, Lecture notes of Data Structures and Algorithms

This is about data structure lecture note

Typology: Lecture notes

2023/2024

Uploaded on 05/16/2024

yujin-lee-3
yujin-lee-3 🇺🇸

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'
UnorderedList 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 UnorderedList:
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
a
ppend
(self
,
i
tem
):
d) Why do you suppose I added a
_tail
attribute?
Data Structures (CS 1520) Lecture 8 Name:_________________
L
ecture 8
- Page
1
pf2

Partial preview of the text

Download Data structure lecture note and more Lecture 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'

UnorderedList 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 UnorderedList: 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?

Data Structures (CS 1520) Lecture 8 Name:_________________

Lecture 8 - Page 1

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)

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):

Data Structures (CS 1520) Lecture 8 Name:_________________

Lecture 8 - Page 2