Professional Documents
Culture Documents
Topics
programmer
Primitive DT:
ADT:
Interface (API)
Implementation
(methods)
D t
Data
Cpt S 223. School of EECS, WSU
Abstract
Abstract in that implementation of operations not specified
in ADT definition
E.g., List
Insert, delete, search, sort
Stack ADT
Push
us ((insert)
se ) e
element
e e o
on top
op o
of
stack
Pop (remove) element from
top
p of stack
Top: return element at top of
stack
Queue ADT
q
here
Dequeue
front
Enque here
back
Cpt S 223. School of EECS, WSU
IImplementation
l
t ti ffor b
basic
i
ADTs
A1
A2
Operations
insert(X,k) : O(N)
remove(k) : O(N)
find(X) : O(N)
findKth(k)
( ) : O(1)
( )
printList() : O(N)
A3
Read as order N
(
(means
that
th t runtime
ti
is
i
proportional to N)
Read as order 1
((means that runtime is a
constant i.e., not
dependent on N)
10
data
next
pointer
Cpt S 223. School of EECS, WSU
null
11
Linked Lists
Operations
Insert(X A) O(1)
Insert(X,A)
Remove(A) O(1)
Cpt S 223. School of EECS, WSU
12
Linked Lists
Operations
find(X) O(N)
findKth(k) O(N)
printList() O(N)
13
Doubly-Linked List
Singly-linked list
insert(X,A)
insert(X
A) and remove(X) require pointer
to node just before X
Doubly-linked
Doubly
linked list
prev
next
14
Doubly-Linked List
Insert(X,A)
Remove(X)
( )
15
Sentinel Nodes
E t d
Empty
doubly-linked
bl li k d list
li t with
ith sentinel
ti l nodes
d
16
www.sgi.com/tech/stl/
www
sgi com/tech/stl/
http://www.cplusplus.com/reference/stl/
www.cppreference.com/cppstl.html
f
/
tl ht l
Cpt S 223. School of EECS, WSU
17
vector<Object>
Array-based implementation
fi dKth O(1)
findKth
insert and remove O(N)
list<Object>
Both
h require
i O(N)
O( ) for
f search
h
Cpt S 223. School of EECS, WSU
18
Container Methods
void clear()
bool empty()
19
void pop_back ()
20
List-only Methods
void pop_front ()
21
Vector-only Methods
22
Iterators
iterator begin ()
iterator end ()
23
Iterator Methods
*itr
itr1 == itr2
it 1 !
itr1
!= it
itr2
2
24
Example: printList
template <typename Container>
void printList (const Container & lst)
{
for (typename Container::const_iterator itr = lst.begin();
itr != lst.end();
++itr)
{
cout << *itr << endl;
}
}
25
Constant Iterators
iterator begin ()
const_iterator begin
g
() const
iterator end ()
const
co
st_iterator
te ato e
end
d () co
const
st
Appropriate version above returned based on
whether container is const
If const_iterator used, then *itr
pp
on left-hand side of
cannot appear
assignment (e.g., *itr=0 )
Cpt S 223. School of EECS, WSU
26
Better printList
27
Container Operations
Requiring Iterators
Remove all items beginning at position start, up to, but not including end
28
Implementation of Vector
constructor
copy constructor
destructor
operator=
29
Implementation of Vector
Automatic
resize
30
Implementation of Vector
Iterators (implemented
using simple pointers)
Iterator methods
31
Implementation of List
Iterators implemented
using nested class
32
Implementation of List
33
Implementation of List
Empty list
34
Implementation of List
35
Implementation of List
Note:
there is
no const
here
36
Implementation of List
37
Implementation of List
38
Implementation of List
39
Stack ADT
Push
us ((insert)
se ) e
element
e e o
on top
op o
of
stack
Pop (remove) element from
top
p of stack
Top: return element at top of
stack
40
Stack Implementation
Linked List
Vector
41
Stack Implementation
Linked List
Vector
42
Stack Implementation
Linked List
Running
g Times ?
Vector
43
Methods
#include <stack>
Push, pop,
Push
pop top
Empty, size
stack<int> s;
for (int i = 0; i < 5; i++ )
{
s.push(i);
}
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
44
Stack Applications
45
Stack Applications
Postfix expressions
Class PostFixCalculator
{
Means ((1 * 2) + 3) + (4 * 5)
public:
HP calculators
...
void Multiply ()
Unambiguous (no need for
{
paranthesis)
int i1 = s.top();
p()
Infix needs paranthesis or
s.pop();
else implicit precedence
int i2 = s.top();
specification to avoid
ambiguity
s.pop();
s push (i1 * i2);
s.push
E.g.,
E g try a+(b*c) and
(a+b)*c
}
private:
stack<int> s;
Postfix evaluation uses stack
}
12*3+45*+
46
Stack Applications
Function calls
Programming
g
g languages
g g use stacks to keep
p
track of function calls
When a function call occurs
47
Queue ADT
q
here
Dequeue
front
Enque here
back
Cpt S 223. School of EECS, WSU
48
Queue Implementation
Li k d Li
Linked
Listt
template <typename Object>
class queue
{
public:
How would the runtime
queue () {}
change if vector is used
void enqueue (Object & x)
in implementation?
{ q.push_back (x); }
Object & dequeue ()
{
Object & x = q.front
q front ();
q.pop_front ();
return x;
}
g time ?
Running
private:
list<Object> q;
}
Cpt S 223. School of EECS, WSU
49
Methods
#include <queue>
queue<int> q;
for (int i = 0; i < 5; i++ )
{
q.push(i);
}
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
50
Queue Applications
Job scheduling
Graph traversals
Queuing theory
51
Summary
Linked list
Stack
Queue
52