Data Structures and Algorithms: Lists and Iterators, Papers of Data Structures and Algorithms

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-emz
koofers-user-emz ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Lists
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science โ€” University of San Francisco โ€“ p.1/19
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Data Structures and Algorithms: Lists and Iterators and more Papers Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

Lists Chris Brooks

Department of Computer Science

University of San Francisco

Department of Computer Science โ€” University of San Francisco โ€“ p.1/

Abstract Data Types

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/

List ADT Operations

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/

Iterators

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/

Java Interfaces

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/

Java List Interface

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/

Java List Iterator Interface

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/

Array Implementation

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/

Array Implementation



Running Time for each operation: insert



append



remove



setFirst



next



previous



length



setPosition



currentValue



Department of Computer Science โ€” University of San Francisco โ€“ p.13/

Linked-List Implementation

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/

Linked-List Implementation

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/

Linked List Implementation



Running Time for each operation: insertappendremovesetFirstnextpreviouslengthsetPositioncurrentValue

Department of Computer Science โ€” University of San Francisco โ€“ p.17/

Doubly-Linked Lists

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/