











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document from the university of san francisco's department of computer science discusses abstract data types (adts) and lists as an example. An adt is defined by the operations that can be performed on it, and a list is an ordered collection of elements with defined operations such as making it empty, adding and removing elements, and getting its length. The document also covers iterators as a way to access elements in a list, and java interfaces for lists and iterators. The document also touches upon array and linked list implementations of lists.
Typology: Papers
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Department of Computer Science
University of San Francisco
Department of Computer Science โ University of San Francisco โ p.1/
An Abstract Data Type is a definition of a type based onthe operations that can be performed on it.
l is a List because we can insert, remove, clear, etc. Not because of how elements are stored. An ADT is an
interface
Data in an ADT cannot be manipulated directly โ onlythrough operations defined in the interface A data structure is an implementation of an ADT
Department of Computer Science โ University of San Francisco โ p.2/
Make list empty Add an element to the list
At the end At the current position Remove an element Get the length of the list Check if the list is empty Print list Get an iterator to traverse the list
Department of Computer Science โ University of San Francisco โ p.4/
Think of an iterator as a generic way of accessing theelements in an ADT. Provides a
logical
index into the ADT.
Separates out the functionality og accessing elementsfrom the ADT itself.Some operation on iterators:
Move iterator forward in the data structure Move iterator backward in the data structure Insert a new element at the current location Return the element at the current location
Department of Computer Science โ University of San Francisco โ p.5/
A Java
interface
is a set of methods.
Any class that implements an interface must implement allof these methods
Department of Computer Science โ University of San Francisco โ p.7/
Without iterators: public
interface
List
public
void
clear();
public
void
append(Object
item);
public
Object
remove();
public
int
length();
public
void
print();
public
boolean
isEmpty();
//
All
the
following
methods
require
a
user
to
know
about
the
โcurrโ
//
pointer.public
void
insert(Object
item);
public
void
next();
public
void
prev();
public
void
setPosition(int
pos);
public
void
setValue(Object
val);
public
Object
currValue();
Department of Computer Science โ University of San Francisco โ p.8/
The iterator is an example of
delegation
public
interface
ListIterator
public
void
first();
public
void
next();
public
void
previous();
public
boolean
inList();
public
Object
current();
public
void
setPosition(int
position);
public
void
insert(Object
elem);
public
Object
remove();
public
void
setCurrent(Object
value);
Department of Computer Science โ University of San Francisco โ p.10/
Data is stored in an array Iterator stores index of current location To add an element to the current position:
Shift other elements to the right To remove and element from the middle of the array:
Shift other elements to the left List has a maximum size (unless we use growable arrays)
Department of Computer Science โ University of San Francisco โ p.11/
Running Time for each operation: insert
append
remove
setFirst
next
previous
length
setPosition
currentValue
Department of Computer Science โ University of San Francisco โ p.13/
Data is stored in a linked list
Weโll use an auxiliary class called
Link
to hold
elements Maintain a pointer to first and last element in list Iterator maintains a pointer to the current element To find the ith element:
Start at the front of the list Skip past i elements
How do we insert an element before the current element? Howdo we remove the current element?
Department of Computer Science โ University of San Francisco โ p.14/
Data is stored in a linked list โ with a dummy first element Maintain a pointer to first (dummy) and last element in list Iterator maintains a pointer to the element
before
the
current element To find the ith element:
Start at the front of the list Skip past (i+1) elements
Department of Computer Science โ University of San Francisco โ p.16/
Running Time for each operation: insertappendremovesetFirstnextpreviouslengthsetPositioncurrentValue
Department of Computer Science โ University of San Francisco โ p.17/
Each element in the list has two pointers โ next andprevious
Can locate the previous element of any element in thelist in time
, instead of time
More space is required (two pointers for each element,instead of one) Does โcurrentโ still need to point to the element
before
the current element? Do we still need a โdummyโ element?
Department of Computer Science โ University of San Francisco โ p.19/