6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading...

Post on 30-Mar-2015

216 views 1 download

Tags:

transcript

04/10/23 23:37 Sequences 1

Lists and Sequences

04/10/23 23:37 Sequences 2

Outline and Reading

Singly linked listPosition ADT and List ADT (§5.2.1)Doubly linked list (§ 5.2.3)Sequence ADT (§5.3.1)Implementations of the sequence ADT (§5.3.3)Iterators (§5.5)

04/10/23 23:37 Sequences 3

Singly Linked ListA singly linked list is a concrete data structure consisting of a sequence of nodesEach node stores

element link to the next node

next

elem node

A B C D

04/10/23 23:37 Sequences 4

Stack with a Singly Linked List

We can implement a stack with a singly linked listThe top element is stored at the first node of the listThe space used is O(n) and each operation of the Stack ADT takes O(1) time

t

nodes

elements

04/10/23 23:37 Sequences 5

Queue with a Singly Linked List

We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node

The space used is O(n) and each operation of the Queue ADT takes O(1) time

f

r

nodes

elements

04/10/23 23:37 Sequences 6

Position ADTThe Position ADT models the notion of place within a data structure where a single object is storedA special null position refers to no object.Positions provide a unified view of diverse ways of storing data, such as

a cell of an array a node of a linked list

Member functions: Object& element(): returns the element stored

at this position bool isNull(): returns true if this is a null

position

04/10/23 23:37 Sequences 7

List ADT

The List ADT models a sequence of positions storing arbitrary objectsIt establishes a before/after relation between positionsGeneric methods:

size(), isEmpty()

Query methods: isFirst(p), isLast(p)

Accessor methods: first(), last() before(p), after(p)

Update methods: replaceElement(p,

o), swapElements(p, q)

insertBefore(p, o), insertAfter(p, o),

insertFirst(o), insertLast(o)

remove(p)

Example 5.4 (page 219)

04/10/23 23:37 Sequences 8

Operation Output S

insertFirst (8) P1 (8) (8)

insertAfter (P1 ,5 ) P2 (5) (8,5)

insertBefore (P2 ,3 ) P3 (3) (8,3,5)

insertFirst (9) p4 (9) (9,8,3,5)

before (p3 ) P1 (8) (9,8,3,5)

last () P2 (5) (9,8,3,5)

remove (p4) - (8,3,5)

swapElements (P1 ,P2)

- (5,3,8)

replaceElement (P3,7)

- (5,7,8)

insertAfter(first(),2) P5 (2) (5,2,7,8)

P1, P2, P3,..., Pi are variables representing positions

04/10/23 23:37 Sequences 9

Doubly Linked ListA doubly linked list provides a natural implementation of the List ADT

Nodes implement Position and store: element link to the previous node link to the next node

Special trailer and header nodes

prev next

elem

trailerheader nodes/positions

elements

node

04/10/23 23:37 Sequences 10

InsertionWe visualize operation insertAfter(p, X), which returns position q

A B X C

A B C

p

A B C

p

X

q

p q

04/10/23 23:37 Sequences 11

DeletionWe visualize remove(p), where p = last()

A B C D

p

A B C

D

p

A B C

04/10/23 23:37 Sequences 12

PerformanceIn the implementation of the List ADT by means of a doubly linked list The space used by a list with n

elements is O(n) The space used by each position of the

list is O(1) All the operations of the List ADT run in O(1) time

Operation element() of the Position ADT runs in O(1) time

04/10/23 23:37 Sequences 13

Sequence ADTThe Sequence ADT is the union of the Vector and List ADTsElements accessed by

Rank, or Position

Generic methods: size(), isEmpty()

Vector-based methods: elemAtRank(r),

replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r)

List-based methods: first(), last(),

before(p), after(p), replaceElement(p, o), swapElements(p, q), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p)

Bridge methods: atRank(r), rankOf(p)

04/10/23 23:37 Sequences 14

Applications of SequencesThe Sequence ADT is a basic, general-purpose, data structure for storing an ordered collection of elementsDirect applications: Generic replacement for stack, queue, vector,

or list small database (e.g., address book)

Indirect applications: Building block of more complex data structures

04/10/23 23:37 Sequences 15

Array-based Implementation

We use a circular array storing positions A position object stores:

Element Rank

Indices f and l keep track of first and last positions

0 1 2 3

positions

elements

S

lf

04/10/23 23:37 Sequences 16

Sequence ImplementationsOperation Array List

size, isEmpty 1 1

atRank, rankOf, elemAtRank 1 n

first, last, before, after 1 1

replaceElement, swapElements 1 1

replaceAtRank 1 n

insertAtRank, removeAtRank n n

insertFirst, insertLast 1 1

insertAfter, insertBefore n 1

remove n 1

04/10/23 23:37 Sequences 17

IteratorsAn iterator abstracts the process of scanning through a collection of elementsMethods of the ObjectIterator ADT:

boolean hasNext() object next() reset()

Extends the concept of position by adding a traversal capabilityMay be implemented with an array or singly linked list

An iterator is typically associated with an another data structureWe can augment the Stack, Queue, Vector, List and Sequence ADTs with method:

ObjectIterator elements()

Two notions of iterator: snapshot: freezes the

contents of the data structure at a given time

dynamic: follows changes to the data structure