You are on page 1of 52

Abstract Data Types

Cpt S 223. School of EECS, WSU

Topics

Abstract Data Types (ADTs)

Some basic ADTs:


Lists
Stacks
Queues
Cpt S 223. School of EECS, WSU

Primitive Data Type vs.


Abstract Data Types
progra
ammer

programmer

Primitive DT:

ADT:

Interface (API)

Implementation
(methods)

e.g., int, float

D t
Data
Cpt S 223. School of EECS, WSU

Abstract Data Types (ADTs)

ADT is a set of objects together with a set of


operations.

Abstract
Abstract in that implementation of operations not specified
in ADT definition
E.g., List
Insert, delete, search, sort

C++ classes are perfect for ADTs


Can change ADT implementation details without
breaking code using ADT

Cpt S 223. School of EECS, WSU

Specifications of basic ADTs


List, Stack, Queue

Cpt S 223. School of EECS, WSU

The List ADT

List of size N: A0, A1, , AN-1


Each element Ak has a unique position k
in the list
Elements can be arbitrarily complex
Operations

insert(X,k), remove(k), find(X), findKth(k),


printList()
Cpt S 223. School of EECS, WSU

Stack ADT

Stack = a list where insert


and remove take place only
at the top
Operations

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

LIFO (Last In First Out)


Cpt S 223. School of EECS, WSU

Queue ADT

Queue = a list where insert takes place at the


back, but remove takes place at the front
Operations

Enqueue (insert) element at the back of the queue


Dequeue (remove and return) element from the
front of the queue
FIFO (First In First Out)
5

q
here
Dequeue
front

Enque here

back
Cpt S 223. School of EECS, WSU

IImplementation
l
t ti ffor b
basic
i
ADTs

Cpt S 223. School of EECS, WSU

List ADT using Arrays


A0

A1

A2

Operations

insert(X,k) : O(N)
remove(k) : O(N)
find(X) : O(N)
findKth(k)
( ) : O(1)
( )
printList() : O(N)

Cpt S 223. School of EECS, WSU

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

List ADT using Linked Lists

Elements not stored in contiguous


memory
Nodes in list consist of data element
and next pointer
node

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)

Cpt S 223. School of EECS, WSU

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

Also keep pointer to previous node

prev

next

Cpt S 223. School of EECS, WSU

14

Doubly-Linked List

Insert(X,A)

Remove(X)
( )

Problems with operations at ends of list

newA = new Node(A);


newA->prev = X->prev;
newA->next = X;
X->prev->next = newA;
X->prev = newA;
X->prev->next = X->next;
X->next->prev = X->prev;

Cpt S 223. School of EECS, WSU

15

Sentinel Nodes

Dummy head and tail nodes to avoid special cases at


ends of list
Doubly linked list with sentinel nodes
Doubly-linked

E t d
Empty
doubly-linked
bl li k d list
li t with
ith sentinel
ti l nodes
d

Cpt S 223. School of EECS, WSU

16

C++ Standard Template


Library (STL)

Implementation of common data


structures

List, stack, queue,


Generally called containers

WWW references for STL

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

Implementing Lists using STL

vector<Object>

Array-based implementation
fi dKth O(1)
findKth
insert and remove O(N)

list<Object>

Doubly-linked list with sentinel nodes


findKth O(N)
insert and remove O(1)

Unless change at end of vector

If position of change is known

Both
h require
i O(N)
O( ) for
f search
h
Cpt S 223. School of EECS, WSU

18

Container Methods

int size() const

void clear()

Return number of elements in container


Remove all elements from container

bool empty()

Return true is container has no elements,


otherwise returns false
Cpt S 223. School of EECS, WSU

19

Vector and List Methods

void push_back (const Object & x)

void pop_back ()

Remove object at end of list

const Object & back () const

Add x to end of list

Return object at end of list

const Object & front () const

Return object at front of list


Cpt S 223. School of EECS, WSU

20

List-only Methods

void push_front (const Object & x)

Add x to front of list

void pop_front ()

Remove object at front of list

Cpt S 223. School of EECS, WSU

21

Vector-only Methods

Object & operator[] (int idx)

Object & at (int idx)

Return object at index idx in vector with boundschecking

int capacity () const

Return object at index idx in vector with no


bounds-checking

Return internal capacity of vector

void reserve (int newCapacity)

Set new capacity for vector (avoid expansion)


Cpt S 223. School of EECS, WSU

22

Iterators

Represents position in container


Getting an iterator

iterator begin ()

Return appropriate iterator representing first


item in container

iterator end ()

Return appropriate iterator representing end


marker in container
Position after last item in container
Cpt S 223. School of EECS, WSU

23

Iterator Methods

itr++ and ++itr

*itr

Return reference to object stored at iterator itrs location

itr1 == itr2

Advance iterator itr to next location

Return true if itr1 and itr2 refer to same location;


otherwise return false

it 1 !
itr1
!= it
itr2
2

Return true if itr1 and itr2 refer to different locations;


otherwise return false
Cpt S 223. School of EECS, WSU

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;
}
}

Cpt S 223. School of EECS, WSU

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

Cpt S 223. School of EECS, WSU

27

Container Operations
Requiring Iterators

iterator insert (iterator pos, const Object & x)

Return iterator representing position of inserted item


O(1) for lists, O(N) for vectors

iterator erase (iterator pos)

Remove object whose position is given by iterator pos


Return iterator representing position off item ffollowing
ll
pos
This operation invalidates pos

O(1) for lists, O(N) for vectors

Add x into list, prior to position given by iterator pos

iterator erase (iterator start,


start iterator end)

Remove all items beginning at position start, up to, but not including end

Cpt S 223. School of EECS, WSU

28

Implementation of Vector
constructor
copy constructor
destructor
operator=

Cpt S 223. School of EECS, WSU

29

Implementation of Vector

Automatic
resize

Cpt S 223. School of EECS, WSU

30

Implementation of Vector
Iterators (implemented
using simple pointers)

Iterator methods

Cpt S 223. School of EECS, WSU

31

Implementation of List

Iterators implemented
using nested class

Cpt S 223. School of EECS, WSU

32

Implementation of List

Cpt S 223. School of EECS, WSU

33

Implementation of List

Empty list

Cpt S 223. School of EECS, WSU

34

Implementation of List

Allows inheriting classes to


access these.

Gives List class access to


constructor.

Cpt S 223. School of EECS, WSU

35

Implementation of List

Allows inheriting classes to


access these.

Note:
there is
no const
here

Gives List class access to


constructor.

Cpt S 223. School of EECS, WSU

36

Implementation of List

Cpt S 223. School of EECS, WSU

37

Implementation of List

Cpt S 223. School of EECS, WSU

38

Implementation of List

Cpt S 223. School of EECS, WSU

39

Stack ADT

Stack is a list where insert


and remove take place only
at the top
Operations

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

LIFO (Last In First Out)


Cpt S 223. School of EECS, WSU

40

Stack Implementation
Linked List

Vector

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ ?s.push_front (x); }
void pop ()
{ ?s.pop_front (); }
Object & top ()
{ ?s.front (); }
private:
list<Object> s;
}

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ s.push_back
(x); }
?
void pop ()
?
{ s.pop_back
(); }
Object & top ()
?
{ s.back
(); }
private:
vector<Object> s;
}

Cpt S 223. School of EECS, WSU

41

Stack Implementation
Linked List

Vector

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ ?s.push_front (x); }
void pop ()
{ ?s.pop_front (); }
Object & top ()
{ ?s.front (); }
private:
list<Object> s;
}

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ s.push_back (x); }
void pop ()
{ s.pop_back (); }
Object & top ()
{ s.back (); }
private:
vector<Object> s;
}

Cpt S 223. School of EECS, WSU

42

Stack Implementation
Linked List

Running
g Times ?

Vector

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ s.push_front (x); }
void pop ()
{ s.pop_front (); }
Object & top ()
{ s.front (); }
private:
list<Object> s;
}

template <typename Object>


class stack
{
public:
stack () {}
void push (Object & x)
{ s.push_back (x); }
void pop ()
{ s.pop_back (); }
Object & top ()
{ s.back (); }
private:
vector<Object> s;
}

Cpt S 223. School of EECS, WSU

43

C++ STL Stack Class

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();
}

Cpt S 223. School of EECS, WSU

44

Stack Applications

Balancing symbols: ((()())(()))


stack<char> s;
;
while not end of file
{
read character c
if c = (
then s.push(c)
if c = )
then if s.empty()
then error
else s.pop()
}
if (! s.empty())
s empty())
then error
else okay

How does this work?

Cpt S 223. School of EECS, WSU

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*+

Cpt S 223. School of EECS, WSU

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

Push CPU registers and program counter on to


stack (activation record or stack frame)
Upon return, restore registers and
d program
counter from top stack frame and pop

Cpt S 223. School of EECS, WSU

47

Queue ADT

Queue is a list where insert takes place at the


back, but remove takes place at the front
Operations

Enqueue (insert) element at the back of the queue


Dequeue (remove and return) element from the
front of the queue
FIFO (First In First Out)
5

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

C++ STL Queue Class

Methods

#include <queue>

Push (at back)


Pop (from front)
Back front
Back,
Empty, size

queue<int> q;
for (int i = 0; i < 5; i++ )
{
q.push(i);
}
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}

Cpt S 223. School of EECS, WSU

50

Queue Applications

Job scheduling
Graph traversals
Queuing theory

Cpt S 223. School of EECS, WSU

51

Summary

Abstract Data Types (ADTs)

Linked list
Stack
Queue

C++ Standard Template Library (STL)


Numerous applications
Building blocks for more complex data
structures
Cpt S 223. School of EECS, WSU

52

You might also like