Sunteți pe pagina 1din 3

Sequences 4/1/2003 8:57 AM

Outline and Reading


Singly linked list
Lists and Sequences Position 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)

4/1/2003 8:57 AM Sequences 1 4/1/2003 8:57 AM Sequences 2

Singly Linked List Stack with a Singly Linked List


A singly linked list is a We can implement a stack with a singly linked list
concrete data structure next
The top element is stored at the first node of the list
consisting of a sequence
of nodes The space used is O(n) and each operation of the
Each node stores Stack ADT takes O(1) time
„ element elem node
nodes
„ link to the next node
t ∅

A B C D elements
4/1/2003 8:57 AM Sequences 3 4/1/2003 8:57 AM Sequences 4

Queue with a Singly Linked List Position ADT


We can implement a queue with a singly linked list The Position ADT models the notion of place
„ The front element is stored at the first node within a data structure where a single object
„ The rear element is stored at the last node is stored
The space used is O(n) and each operation of the A special null position refers to no object.
Queue ADT takes O(1) time r Positions provide a unified view of diverse
ways of storing data, such as
nodes
„ a cell of an array
f „ 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
elements
4/1/2003 8:57 AM Sequences 5 4/1/2003 8:57 AM Sequences 6

1
Sequences 4/1/2003 8:57 AM

List ADT Doubly Linked List


A doubly linked list provides a natural prev next
The List ADT models a Accessor methods: implementation of the List ADT
sequence of positions „ first(), last() Nodes implement Position and store:
storing arbitrary objects „ before(p), after(p) „ element
„ link to the previous node elem node
It establishes a Update methods: „ link to the next node
before/after relation „ replaceElement(p, o), Special trailer and header nodes
between positions swapElements(p, q)
insertBefore(p, o), header nodes/positions trailer
Generic methods: „

insertAfter(p, o),
„ size(), isEmpty()
„ insertFirst(o),
Query methods: insertLast(o)
„ isFirst(p), isLast(p) „ remove(p)
elements
4/1/2003 8:57 AM Sequences 7 4/1/2003 8:57 AM Sequences 8

Insertion Deletion
We visualize operation insertAfter(p, X), which returns position q We visualize remove(p), where p = last()
p p

A B C A B C D
p
A B C p
A B q C

X D
p q

A B X C A B C
4/1/2003 8:57 AM Sequences 9 4/1/2003 8:57 AM Sequences 10

Performance Sequence ADT


In the implementation of the List ADT The Sequence ADT is the List-based methods:
union of the Vector and
by means of a doubly linked list List ADTs
„ first(), last(),
before(p), after(p),
„ The space used by a list with n elements is Elements accessed by replaceElement(p, o),
O(n) „ Rank, or swapElements(p, q),
Position insertBefore(p, o),
„ The space used by each position of the list „
insertAfter(p, o),
is O(1) Generic methods: insertFirst(o),
„ size(), isEmpty() insertLast(o),
„ All the operations of the List ADT run in Vector-based methods: remove(p)
O(1) time „ elemAtRank(r), Bridge methods:
„ Operation element() of the replaceAtRank(r, o),
atRank(r), rankOf(p)
insertAtRank(r, o), „
Position ADT runs in O(1) time removeAtRank(r)

4/1/2003 8:57 AM Sequences 11 4/1/2003 8:57 AM Sequences 12

2
Sequences 4/1/2003 8:57 AM

Applications of Sequences Array-based Implementation


We use a elements
The Sequence ADT is a basic, general-
circular array
purpose, data structure for storing an ordered storing
collection of elements positions
Direct applications: A position
object stores:
„ Generic replacement for stack, queue, vector, or
„ Element 0 1 2 3
list „ Rank
„ small database (e.g., address book) Indices f and l positions

Indirect applications: keep track of


first and last
„ Building block of more complex data structures positions S
f l
4/1/2003 8:57 AM Sequences 13 4/1/2003 8:57 AM Sequences 14

Sequence Implementations Iterators


Operation Array List An iterator abstracts the An iterator is typically
size, isEmpty 1 1 process of scanning through associated with an another
a collection of elements data structure
atRank, rankOf, elemAtRank 1 n Methods of the ObjectIterator We can augment the Stack,
first, last, before, after 1 1 ADT: Queue, Vector, List and
boolean hasNext() Sequence ADTs with method:
replaceElement, swapElements
„
1 1
„ object next() „ ObjectIterator elements()
replaceAtRank 1 n „ reset() Two notions of iterator:
insertAtRank, removeAtRank n n Extends the concept of „ snapshot: freezes the
position by adding a traversal contents of the data
insertFirst, insertLast 1 1 capability structure at a given time
insertAfter, insertBefore n 1 dynamic: follows changes to
May be implemented with an „
the data structure
array or singly linked list
remove n 1
4/1/2003 8:57 AM Sequences 15 4/1/2003 8:57 AM Sequences 16

S-ar putea să vă placă și