52
Abstract Data Types Abstract Data Types 1 Cpt S 223. School of EECS, WSU

Abstract Data TypesAbstract Data Types - The School of Electrical

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Abstract Data TypesAbstract Data Types - The School of Electrical

Abstract Data TypesAbstract Data Types

1111Cpt S 223. School of EECS, WSU

Page 2: Abstract Data TypesAbstract Data Types - The School of Electrical

Topics

Abstract Data Types (ADTs)

Some basic ADTs: Lists Stacks Queues

222Cpt S 223. School of EECS, WSU

Page 3: Abstract Data TypesAbstract Data Types - The School of Electrical

Primitive Data Type vs. Abstract Data Types

Primitive DT:

amm

er

ADT:

prog

ram

mer

prog

ra

Interface (API)

e.g., int, floatImplementation

(methods)

D t

33

Data

Cpt S 223. School of EECS, WSU

Page 4: Abstract Data TypesAbstract Data Types - The School of Electrical

Abstract Data Types (ADTs) ADT is a set of objects together with a set of

operations. “Abstract” in that implementation of operations not specified Abstract in that implementation of operations not specified

in ADT definition E.g., List Insert, delete, search, sort Insert, delete, search, sort

C++ classes are perfect for ADTs Can change ADT implementation details without

breaking code using ADT

444Cpt S 223. School of EECS, WSU

Page 5: Abstract Data TypesAbstract Data Types - The School of Electrical

Specifications of basic ADTsSpecifications of basic ADTs

List, Stack, Queue

Cpt S 223. School of EECS, WSU 5

Page 6: Abstract Data TypesAbstract Data Types - The School of Electrical

The List ADT

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

Each element A has a unique position k Each element Ak has a unique position k in the listElements can be arbitrarily complex Elements can be arbitrarily complex

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

printList()

66Cpt S 223. School of EECS, WSU

Page 7: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack ADT Stack = a list where insert

and remove take place only at the “top”at the “top”

Operations Push (insert) element on top of us ( se ) e e e o op o

stack Pop (remove) element from

top of stackp Top: return element at top of

stack

LIFO (Last In First Out)

7

LIFO (Last In First Out)

7Cpt S 223. School of EECS, WSU

Page 8: Abstract Data TypesAbstract Data Types - The School of Electrical

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 queueFIFO (First In First Out) FIFO (First In First Out)

5 7 2 6 3 2 8Enque hereDequeue here

8front back

q

8Cpt S 223. School of EECS, WSU

Page 9: Abstract Data TypesAbstract Data Types - The School of Electrical

I l t ti f b iImplementation for basic ADTsADTs

Cpt S 223. School of EECS, WSU 9

Page 10: Abstract Data TypesAbstract Data Types - The School of Electrical

List ADT using Arrays

A0 A1 A2 A3 …

Operations • Read as “order N” ( th t ti i

insert(X,k) : O(N) remove(k) : O(N)

(means that runtime is proportional to N)

find(X) : O(N) findKth(k) : O(1)

• Read as “order 1” (means that runtime is a

1010

( ) ( ) printList() : O(N)

(constant – i.e., not dependent on N)

Cpt S 223. School of EECS, WSU

Page 11: Abstract Data TypesAbstract Data Types - The School of Electrical

List ADT using Linked Lists

Elements not stored in contiguous memorymemory

Nodes in list consist of data element and next pointerand next pointer

node

1111

data next nullpointer

Cpt S 223. School of EECS, WSU

Page 12: Abstract Data TypesAbstract Data Types - The School of Electrical

Linked Lists

Operations Insert(X A) – O(1) Insert(X,A) O(1)

Remove(A) – O(1)

1212Cpt S 223. School of EECS, WSU

Page 13: Abstract Data TypesAbstract Data Types - The School of Electrical

Linked Lists

Operations find(X) – O(N) find(X) O(N) findKth(k) – O(N) printList() – O(N) printList() O(N)

1313Cpt S 223. School of EECS, WSU

Page 14: Abstract Data TypesAbstract Data Types - The School of Electrical

Doubly-Linked List

Singly-linked list insert(X A) and remove(X) require pointer insert(X,A) and remove(X) require pointer

to node just before X

Doubly-linked list Doubly linked list Also keep pointer to previous node

1414

prev nextCpt S 223. School of EECS, WSU

Page 15: Abstract Data TypesAbstract Data Types - The School of Electrical

Doubly-Linked List

Insert(X,A) newA = new Node(A);newA->prev = X->prev;newA->next = X;

( )

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

Remove(X) X->prev->next = X->next;X->next->prev = X->prev;

Problems with operations at ends of list

1515Cpt S 223. School of EECS, WSU

Page 16: Abstract Data TypesAbstract Data Types - The School of Electrical

Sentinel Nodes Dummy head and tail nodes to avoid special cases at

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

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

1616Cpt S 223. School of EECS, WSU

Page 17: Abstract Data TypesAbstract Data Types - The School of Electrical

C++ Standard Template Library (STL)

Implementation of common data structuresstructures List, stack, queue, … Generally called containers Generally called containers

WWW references for STLwww sgi com/tech/stl/ www.sgi.com/tech/stl/

http://www.cplusplus.com/reference/stl/f / tl ht l

17

www.cppreference.com/cppstl.html17Cpt S 223. School of EECS, WSU

Page 18: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementing Lists using STL vector<Object>

Array-based implementationfi dKth O(1) findKth – O(1)

insert and remove – O(N) Unless change at end of vector

list<Object> Doubly-linked list with sentinel nodes findKth – O(N) findKth O(N) insert and remove – O(1)

If position of change is known

h i O( ) f h

18

Both require O(N) for search

18Cpt S 223. School of EECS, WSU

Page 19: Abstract Data TypesAbstract Data Types - The School of Electrical

Container Methods

int size() const Return number of elements in container Return number of elements in container

void clear()Remove all elements from container Remove all elements from container

bool empty() Return true is container has no elements,

otherwise returns false

1919Cpt S 223. School of EECS, WSU

Page 20: Abstract Data TypesAbstract Data Types - The School of Electrical

Vector and List Methods void push_back (const Object & x)

Add x to end of list void pop_back ()

Remove object at end of list const Object & back () const

Return object at end of list const Object & front () const

Return object at front of list

2020Cpt S 223. School of EECS, WSU

Page 21: Abstract Data TypesAbstract Data Types - The School of Electrical

List-only Methods void push_front (const Object & x)

Add x to front of list void pop_front ()

Remove object at front of list

2121Cpt S 223. School of EECS, WSU

Page 22: Abstract Data TypesAbstract Data Types - The School of Electrical

Vector-only Methods Object & operator[] (int idx)

Return object at index idx in vector with no bounds-checking

Object & at (int idx) Return object at index idx in vector with bounds-

checking int capacity () const int capacity () const

Return internal capacity of vector void reserve (int newCapacity)

22

void reserve (int newCapacity) Set new capacity for vector (avoid expansion)

22Cpt S 223. School of EECS, WSU

Page 23: Abstract Data TypesAbstract Data Types - The School of Electrical

Iterators

Represents position in container Getting an iterator Getting an iterator

iterator begin ()Return appropriate iterator representing first Return appropriate iterator representing first item in container

iterator end ()() Return appropriate iterator representing end

marker in container

23

Position after last item in container23Cpt S 223. School of EECS, WSU

Page 24: Abstract Data TypesAbstract Data Types - The School of Electrical

Iterator Methods itr++ and ++itr

Advance iterator itr to next location

*itr Return reference to object stored at iterator itr’s location

itr1 == itr2 itr1 itr2 Return true if itr1 and itr2 refer to same location;

otherwise return falseit 1 ! it 2 itr1 != itr2 Return true if itr1 and itr2 refer to different locations;

otherwise return false

2424Cpt S 223. School of EECS, WSU

Page 25: Abstract Data TypesAbstract Data Types - The School of Electrical

Example: printList

template <typename Container>void printList (const Container & lst)void printList (const Container & lst){

for (typename Container::const_iterator itr = lst.begin();itr != lst.end();++itr)

{cout << *itr << endl;

}}}

2525Cpt S 223. School of EECS, WSU

Page 26: Abstract Data TypesAbstract Data Types - The School of Electrical

Constant Iterators iterator begin () const iterator begin () const_ g iterator end () const iterator end () constco st_ te ato e d () co st

Appropriate version above returned based on whether container is const

If const_iterator used, then *itr cannot appear on left-hand side of

26

ppassignment (e.g., *itr=0 )

26Cpt S 223. School of EECS, WSU

Page 27: Abstract Data TypesAbstract Data Types - The School of Electrical

Better printList

2727Cpt S 223. School of EECS, WSU

Page 28: Abstract Data TypesAbstract Data Types - The School of Electrical

Container Operations Requiring Iterators iterator insert (iterator pos, const Object & x)

Add x into list, prior to position given by iterator pos 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

f f ll Return iterator representing position of item following pos This operation invalidates pos O(1) for lists, O(N) for vectors

iterator erase (iterator start iterator end) iterator erase (iterator start, iterator end) Remove all items beginning at position start, up to, but not including end

2828Cpt S 223. School of EECS, WSU

Page 29: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of Vector

constructor

copy constructor

destructor

operator=

2929Cpt S 223. School of EECS, WSU

Page 30: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of Vector

Automaticresize

3030Cpt S 223. School of EECS, WSU

Page 31: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of VectorIterators (implemented

using simple pointers)

Iterator methods

3131Cpt S 223. School of EECS, WSU

Page 32: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

3232

Iterators implemented using nested class

Cpt S 223. School of EECS, WSU

Page 33: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

3333Cpt S 223. School of EECS, WSU

Page 34: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

Empty listEmpty list

3434Cpt S 223. School of EECS, WSU

Page 35: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

Allows inheriting classes to access these.

Gives List class access to constructor.

3535Cpt S 223. School of EECS, WSU

Page 36: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of ListAllows inheriting classes to access these.

Gives List class access to

Note: there is no const Gives List class access to

constructor.no const

here

3636Cpt S 223. School of EECS, WSU

Page 37: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

3737Cpt S 223. School of EECS, WSU

Page 38: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

3838Cpt S 223. School of EECS, WSU

Page 39: Abstract Data TypesAbstract Data Types - The School of Electrical

Implementation of List

3939Cpt S 223. School of EECS, WSU

Page 40: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack ADT Stack is a list where insert

and remove take place only at the “top”at the “top”

Operations Push (insert) element on top of us ( se ) e e e o op o

stack Pop (remove) element from

top of stackp Top: return element at top of

stack

LIFO (Last In First Out)

40

LIFO (Last In First Out)

Cpt S 223. School of EECS, WSU

Page 41: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack Implementation

template <typename Object>class stack

Linked List Vector

template <typename Object>class stack

{public:stack () {}void push (Object & x)

{public:stack () {}void push (Object & x)void push (Object & x)

{ s.push_front (x); }void pop (){ s.pop_front (); }

void push (Object & x){ s.push_back (x); }

void pop (){ s.pop_back (); }

?

? ?

?

Object & top (){ s.front (); }

private:list<Object> s;

Object & top (){ s.back (); }

private:vector<Object> s;

? ?

41

list<Object> s;}

vector<Object> s;}

Cpt S 223. School of EECS, WSU

Page 42: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack Implementation

template <typename Object>class stack

Linked List Vector

template <typename Object>class stack

{public:stack () {}void push (Object & x)

{public:stack () {}void push (Object & x)void push (Object & x)

{ s.push_front (x); }void pop (){ s.pop_front (); }

void push (Object & x){ s.push_back (x); }

void pop (){ s.pop_back (); }

?

?Object & top (){ s.front (); }

private:list<Object> s;

Object & top (){ s.back (); }

private:vector<Object> s;

?

42

list<Object> s;}

vector<Object> s;}

42Cpt S 223. School of EECS, WSU

Page 43: Abstract Data TypesAbstract Data Types - The School of Electrical

Running Times ?

Stack Implementationg

template <typename Object>class stack

Linked List Vector

template <typename Object>class stack

{public:stack () {}void push (Object & x)

{public:stack () {}void push (Object & x)void push (Object & x)

{ s.push_front (x); }void pop (){ s.pop_front (); }

void push (Object & x){ s.push_back (x); }

void pop (){ s.pop_back (); }

Object & top (){ s.front (); }

private:list<Object> s;

Object & top (){ s.back (); }

private:vector<Object> s;

43

list<Object> s;}

vector<Object> s;}

43Cpt S 223. School of EECS, WSU

Page 44: Abstract Data TypesAbstract Data Types - The School of Electrical

C++ STL Stack Class

Methods Push pop top

#include <stack>

stack<int> s; 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();

}

44Cpt S 223. School of EECS, WSU

Page 45: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack Applications

Balancing symbols: ((()())(()))stack<char> s; How does this work?;while not end of file{read character cif c = ‘(’

How does this work?

if c = ‘(’then s.push(c)if c = ‘)’then if s.empty()

then errorelse s.pop()

}if (! s empty())

45

if (! s.empty())then errorelse okay

Cpt S 223. School of EECS, WSU

Page 46: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack Applications Postfix expressions Postfix expressions

1 2 * 3 + 4 5 * + Means ((1 * 2) + 3) + (4 * 5)

HP calculators

Class PostFixCalculator{public:

HP calculators Unambiguous (no need for

paranthesis) Infix needs paranthesis or

...void Multiply (){int i1 = s.top();

Infix needs paranthesis or else implicit precedence specification to avoid ambiguityE g try a+(b*c) and

p()s.pop();int i2 = s.top();s.pop();s push (i1 * i2); E.g., try a+(b*c) and

(a+b)*c

Postfix evaluation uses stack

s.push (i1 * i2);}

private:stack<int> s;

46

Postfix evaluation uses stack}

Cpt S 223. School of EECS, WSU

Page 47: Abstract Data TypesAbstract Data Types - The School of Electrical

Stack Applications

Function calls Programming languages use stacks to keep g g g g p

track of function calls When a function call occurs

Push CPU registers and program counter on to stack (“activation record” or “stack frame”)

d Upon return, restore registers and program counter from top stack frame and pop

47Cpt S 223. School of EECS, WSU

Page 48: Abstract Data TypesAbstract Data Types - The School of Electrical

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 queueFIFO (First In First Out) FIFO (First In First Out)

5 7 2 6 3 2 8Enque hereDequeue here

48front back

q

Cpt S 223. School of EECS, WSU

Page 49: Abstract Data TypesAbstract Data Types - The School of Electrical

Queue ImplementationLi k d Li t

template <typename Object>class queue{

Linked List

{public:queue () {}void enqueue (Object & x)

How would the runtime change if vector is used

{ q.push_back (x); }Object & dequeue (){Object & x = q front ();

in implementation?

Object & x q.front ();q.pop_front ();return x;

} Running time ?

49

private:list<Object> q;

}

g

Cpt S 223. School of EECS, WSU

Page 50: Abstract Data TypesAbstract Data Types - The School of Electrical

C++ STL Queue Class

Methods Push (at back)

#include <queue>

queue<int> q; Push (at back) Pop (from front) Back front

queue<int> q;

for (int i = 0; i < 5; i++ ){

Back, front Empty, size

q.push(i);}while (!q.empty()){{

cout << q.front() << endl;q.pop();

}

50Cpt S 223. School of EECS, WSU

Page 51: Abstract Data TypesAbstract Data Types - The School of Electrical

Queue Applications

Job scheduling Graph traversals Graph traversals Queuing theory

5151Cpt S 223. School of EECS, WSU

Page 52: Abstract Data TypesAbstract Data Types - The School of Electrical

Summary

Abstract Data Types (ADTs) Linked list Stack Queue

C++ Standard Template Library (STL) Numerous applicationsNumerous applications Building blocks for more complex data

structures

52

structures

Cpt S 223. School of EECS, WSU