CS 201 S24, Lecture 1b, Interfaces - ADTs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

CS 201, S24

Interfaces / Abstract Data Types

Queue Interface
• add(x): add the value x to the Queue
• remove(): remove the next (previously added) value, y, from the Queue and return y

Queueing Discipline Also known as Common names of


operations

FIFO add(x), remove()

LIFO Stack push(x), pop()

FIFO + LIFO Deque add_first(x), add_last(x),


remove_first(),
remove_last()

Priority-Based Priority Queue add(x),


remove()/delete_min()

List Interface
size() : return n, the length of the list
get(i) : return the value at index i
set(i, x) : set the value at position i equal to x
add(i, x) : add x at index i, shifting subsequent values one index forward
remove(i) : remove the value at index i, shifting back subsequent values one index

In-Class: Implement the first 3 Queue Interfaces using List Interface

Dictionary Interface
add(k,v) : assign the key, k, to value, v, and add the dictionary
remove(k) : remove the key, k (and its associated value) from the dictionary
find(k) : return the value of key k.

In-Class: Implement the Dictionary Interface using USet Interface


CS 201, S24

USet Interface: Unsorted Set (= is defined)


size() : return n, the number of elements in the set
add(x) : add x to the set provided no other element equals x in the set. Return
true if x was added in the set, and false
remove(x) : remove x from the set by finding an element y in the set such that x
equals y and remove y or nil if no such element exists.
Return y, or nil if no such element exists.
find(x) : Find an element y in the set such that y equals x. Return y, or nil if
no such element exists.

SSet Interface: Sorted Set (<, > and = are defined)


size() : return n, the number of elements in the set
add(x) : add x to the set provided no other element equals x in the set. Return
true if x was added in the set, and false
remove(x) : remove x from the set by finding an element y in the set such that x
equals y and remove y or nil if no such element exists.
Return y, or nil if no such element exists.
find(x) : locate x in the sorted set;
Find the smallest element y in the set such that y ≥ x. Return y or nil if no such
element exists.

Why might this find(x) operation be useful?

Assessment:
1. Suppose I tried to add an element to my USet and it returned false. What does
that mean?
2. Suppose I want to remove two elements at indices x and y from my List. I call
remove(x) and then remove(y) on my List. Have I removed the correct
elements?
3. Suppose I want to find an element, x, that is greater than all elements in my
SSet. What will find(x) return?
4. I want an interface where I can add and remove elements by value, and where
duplicates are allowed.
5. I want an interface where I can find the least element in a single operation.

Assessment: Do interfaces guarantee time complexity bounds?

You might also like