abstract data types - school of electrical engineering...
TRANSCRIPT
![Page 1: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/1.jpg)
1111
Abstract Data Types
CptS 223 – Advanced Data Structures
Larry HolderSchool of Electrical Engineering and Computer Science
Washington State University
![Page 2: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/2.jpg)
222
Purpose
Abstract Data Types (ADTs)ListsStacksQueues
![Page 3: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/3.jpg)
33
Abstract Data Types (ADTs)ADT is a set of objects together with a set of operations.Abstract in that implementation of operations not specified in ADT definitionE.g., List
Insert, delete, search, sortC++ class perfect for ADTsCan change ADT implementation details without breaking code using ADT
![Page 4: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/4.jpg)
44
The List ADT
List of size N: A0, A1, …, AN-1
Each element Ak has a unique position k in the listElements can be arbitrarily complexOperations
insert(X,k), remove(k), find(X), findKth(k), printList()
![Page 5: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/5.jpg)
55
Lists Using Arrays
Simple array vs. Vector classEstimating maximum size
Operationsinsert(X,k) – O(N)remove(k) – O(N)find(X) – O(N)findKth(k) – O(1)printList() – O(N)
![Page 6: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/6.jpg)
66
Linked Lists
Elements not stored in contiguous memoryNodes in list consist of data element and next pointer
node
data next nullpointer
![Page 7: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/7.jpg)
77
Linked Lists
OperationsInsert(X,A) – O(1)
Remove(A) – O(1)
![Page 8: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/8.jpg)
88
Linked Lists
Operationsfind(X) – O(N)findKth(k) – O(N)printList() – O(N)
![Page 9: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/9.jpg)
99
Doubly-Linked List
Singly-linked listinsert(X,A) and remove(X) require pointer to node just before X
Doubly-linked listAlso keep pointer to previous node
prev next
![Page 10: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/10.jpg)
1010
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;
![Page 11: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/11.jpg)
1111
Sentinel NodesDummy head and tail nodes to avoid special cases at ends of listDoubly-linked list with sentinel nodes
Empty doubly-linked list with sentinel nodes
![Page 12: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/12.jpg)
12
C++ Standard Template Library (STL)
Implementation of common data structures
List, stack, queue, …Generally called containers
WWW resourceswww.sgi.com/tech/stl/www.cppreference.com/cppstl.html
12
![Page 13: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/13.jpg)
13
Lists using STLvector<Object>
Array-based implementationfindKth – O(1)insert and remove – O(N)
Unless change at end of vector
list<Object>Doubly-linked list with sentinel nodesfindKth – O(N)insert and remove – O(1)
If position of change is known
Both require O(N) for search
13
![Page 14: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/14.jpg)
14
Container Methods
int size() constReturn number of elements in container
void clear()Remove all elements from container
bool empty() constReturn true if container has no elements, otherwise return false
14
![Page 15: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/15.jpg)
15
Vector and List Methodsvoid push_back (const Object & x)
Add x to end of listvoid pop_back ()
Remove object at end of listconst Object & back () const
Return object at end of listconst Object & front () const
Return object at front of list
15
![Page 16: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/16.jpg)
16
List-only Methodsvoid push_front (const Object & x)
Add x to front of listvoid pop_front ()
Remove object at front of list
16
![Page 17: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/17.jpg)
17
Vector-only MethodsObject & 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 () constReturn internal capacity of vector
void reserve (int newCapacity)Set new capacity for vector (avoid expansion)
17
![Page 18: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/18.jpg)
18
Iterators
Represents position in containerGetting an iteratoriterator begin ()
Return appropriate iterator representing first item in container
iterator end ()Return appropriate iterator representing end marker in containerPosition after last item in container
18
![Page 19: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/19.jpg)
19
Iterator Methodsitr++ and ++itr
Advance iterator itr to next location
*itrReturn reference to object stored at iterator itr’s location
itr1 == itr2Return true if itr1 and itr2 refer to same location; otherwise return false
itr1 != itr2Return true if itr1 and itr2 refer to different locations; otherwise return false
19
![Page 20: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/20.jpg)
20
Example: printList
20
template <typename Container>void printList (const Container & lst){
for (typename Container::const_iterator itr = lst.begin();itr != lst.end();++itr)
{cout << *itr << endl;
}}
![Page 21: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/21.jpg)
21
Constant Iteratorsiterator begin ()const_iterator begin () constiterator end ()const_iterator end () const
Appropriate version above returned based on whether container is constIf const_iterator used, then *itr cannot appear on left-hand side of assignment (e.g., *itr=0 )
21
![Page 22: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/22.jpg)
22
Better printList
22
![Page 23: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/23.jpg)
23
Container Operations Requiring Iterators
iterator insert (iterator pos, const Object & x)Add x into list, prior to position given by iterator posReturn iterator representing position of inserted itemO(1) for lists, O(N) for vectors
iterator erase (iterator pos)Remove object whose position is given by iterator posReturn iterator representing position of item following posThis operation invalidates posO(1) for lists, O(N) for vectors
iterator erase (iterator start, iterator end)Remove all items beginning at position start, up to, but not including end
23
![Page 24: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/24.jpg)
24
Implementation of Vector
24
copy constructor
destructor
operator=
![Page 25: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/25.jpg)
25
Implementation of Vector
25
Automaticresize
![Page 26: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/26.jpg)
26
Implementation of Vector
26
Iterators
![Page 27: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/27.jpg)
27
Implementation of List
27
Nested class
![Page 28: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/28.jpg)
28
Implementation of List
28
![Page 29: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/29.jpg)
29
Implementation of List
29
Empty list
![Page 30: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/30.jpg)
30
Implementation of List
30
Allows inheriting classes to access these.
Gives List class access to constructor.
![Page 31: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/31.jpg)
31
Implementation of List
31
![Page 32: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/32.jpg)
32
Implementation of List
32
![Page 33: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/33.jpg)
33
Implementation of List
33
![Page 34: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/34.jpg)
34
Implementation of List
34
![Page 35: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/35.jpg)
35
Stack ADTStack is a list where insert and remove take place only at the “top”Operations
Push (insert) element on top of stackPop (remove) element from top of stackTop: return element at top of stack
LIFO (Last In First Out)
![Page 36: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/36.jpg)
36
Stack Implementation
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;
}
Linked List Vector
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;
}
Running Times ?
![Page 37: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/37.jpg)
37
C++ STL Stack Class
MethodsPush, pop, topEmpty, size
#include <stack>
stack<int> s;
for (int i = 0; i < 5; i++ ){
s.push(i);}while (!s.empty()){
cout << s.top() << endl;s.pop();
}
![Page 38: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/38.jpg)
38
Stack Applications
Balancing symbols: ((()())(()))stack<char> s;while not end of file{read character cif c = ‘(’then s.push(c)if c = ‘)’then if s.empty()
then errorelse s.pop()
}if (! s.empty())then errorelse okay
![Page 39: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/39.jpg)
39
Stack ApplicationsPostfix expressions
((1 * 2) + 3) + (4 * 5) = 1 2 * 3 + 4 5 * +HP calculators
Infix to postfix conversionOutput operandsPush operators and left parenthesesWhen right parenthesis, output operators up to left parenthesis
Class PostFixCalculator{public:...void Multiply (){int i1 = s.top();s.pop();int i2 = s.top();s.pop();s.push (i1 * i2);
}private:stack<int> s;
}
![Page 40: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/40.jpg)
40
Stack Applications
Function callsProgramming languages use stacks to keep track of function callsWhen a function call occurs
Push CPU registers and program counter on to stack (“activation record” or “stack frame”)Upon return, restore registers and program counter from top stack frame and pop
![Page 41: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/41.jpg)
41
Queue ADT
Queue is a list where insert takes place at the back, but remove takes place at the frontOperations
Enqueue (insert) element at the back of the queueDequeue (remove and return) element from the front of the queueFIFO (First In First Out)
front back
5 7 2 6 3 2 8Enque hereDequeue here
![Page 42: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/42.jpg)
42
Queue Implementation
template <typename Object>class queue{public:queue () {}void enqueue (Object & x){ q.push_back (x); }
Object & dequeue (){Object & x = q.front ();q.pop_front ();return x;
}private:list<Object> q;
}
Linked List Vector (?)
Running time ?
![Page 43: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/43.jpg)
43
C++ STL Queue Class
MethodsPush (at back)Pop (from front)Back, frontEmpty, size
#include <queue>
queue<int> q;
for (int i = 0; i < 5; i++ ){
q.push(i);}while (!q.empty()){
cout << q.front() << endl;q.pop();
}
![Page 44: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/44.jpg)
44
Queue Applications
Job schedulingPriority queuesGraph traversalsQueuing theory
44
![Page 45: Abstract Data Types - School of Electrical Engineering ...holder/courses/CptS223/spr09/slides/adt.pdf · Abstract Data Types. CptS 223 ... to node just before X Doubly-linked list](https://reader031.vdocuments.us/reader031/viewer/2022022522/5b3243777f8b9a2c0b8c6a06/html5/thumbnails/45.jpg)
45
Summary
Abstract Data Types (ADTs)Linked listStackQueue
C++ Standard Template Library (STL)Numerous applicationsBuilding blocks for more complex data structures