![Page 1: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/1.jpg)
Lists, Stacks, and Queues
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 1 / 50
![Page 2: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/2.jpg)
Abstract Data Types (ADT)
Data typeI a set of objects + a set of operationsI Example: integer
F set of integer numbersF operations: +, -, x, /
Can this be generalized? e.g. procedures generalize the notion ofan operatorAbstract data type
I what can be stored in the ADTI what operations can be done on/by the ADT
EncapsulationI Operations on ADT can only be done by calling appropriate
functionsI no mention of how the set of operations is implementedI ADT→ C++: classI method→ C++: member function
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 2 / 50
![Page 3: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/3.jpg)
Abstract Data Types (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 3 / 50
![Page 4: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/4.jpg)
Object-Oriented Design
A technique for developing a program in which the solution isexpressed in terms of objects – self- contained entities composed ofdata and operations on that data.
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 4 / 50
![Page 5: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/5.jpg)
More about OOD
Languages supporting OOD include: C++, Java, Smalltalk, Eiffel,and Object-Pascal.A class is a programmer-defined data type and objects arevariables of that type.In C++, cin is an object of a data type (class) named istream, andcout is an object of a class ostream. Header files iostream.h andfstream.h contain definitions of stream classes.
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 5 / 50
![Page 6: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/6.jpg)
Client Code Using DateType
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 6 / 50
![Page 7: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/7.jpg)
2 Separate Files Generally Used for Class Type
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 7 / 50
![Page 8: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/8.jpg)
DateType Class Instance Diagrams
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 8 / 50
![Page 9: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/9.jpg)
Implementation of DateType member functions
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 9 / 50
![Page 10: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/10.jpg)
Implementation of DateType member functions(Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 10 / 50
![Page 11: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/11.jpg)
The List ADT
A sequence of zero or more elements:
A1,A2,A3, · · · ,AN
N: length of the listA1: first elementAN: last elementIf N = 0, then empty list
I Ai precedes Ai+1I Ai follows Ai−1
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 11 / 50
![Page 12: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/12.jpg)
List Operations
printList: print the listmakeEmpty: create an empty listfind: locate the position of an object in a list
I list: 34,12, 52, 16, 12I find(52)→ 3
insert: insert an object to a listI insert(x,3)→ 34, 12, 52, x, 16, 12
remove: delete an element from the listI remove(52)→ 34, 12, x, 16, 12
findKth: retrieve the element at a certain position
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 12 / 50
![Page 13: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/13.jpg)
Implementation of an ADT
Choose a data structure to represent the ADT. E.g. arrays, records,etc.Each operation associated with the ADT is implemented by one ormore subroutinesTwo standard implementations for the list ADT
I Array-basedI Linked list
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 13 / 50
![Page 14: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/14.jpg)
Array Implementation
Requires an estimate of the maximum size of the list – waste spaceprintList and find: linearfindKth: constantinsert and delete: slow
I e.g. insert at position 0 (making a new element) requires firstpushing the entire array down one spot to make room
I e.g. delete at position 0 requires shifting all the elements in the listup one
I On average, half of the lists needs to be moved for either operation
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 14 / 50
![Page 15: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/15.jpg)
Pointer Implementation (Linked List)
The list is not stored contiguouslyA series of structures that are not necessarily adjacentEach node contains the element and a pointer to a structurecontaining its successor; the last cells next link points to NULLCompared to the array implementation,
I√
the pointer implementation uses only as much space as is neededfor the elements currently on the list
I × but requires space for the pointers in each cell
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 15 / 50
![Page 16: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/16.jpg)
Linked Lists
A linked list is a series of connected nodesEach node contains at least
I A piece of data (any type)I Pointer to the next node in the list
Head: pointer to the first nodeThe last node points to NULL
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 16 / 50
![Page 17: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/17.jpg)
A Simple Linked List Class
Declare Node class for the nodes
Declare List, which containsI head: a pointer to the first node in the list.I Since the list is empty initially, head is set to NULL Operations on
List
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 17 / 50
![Page 18: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/18.jpg)
Inserting a New Node
Node* InsertNode(int index, double x)I Insert a node with data equal to x after the index’th elements.
(i.e., when index = 0, insert the node as the first element; whenindex = 1, insert the node after the first element, and so on)
I If the insertion is successful, return the inserted node. Otherwise,return NULL.(If index is < 0 or > length of the list, the insertion will fail.)
StepsI Locate index’th elementI Allocate memory for the new nodeI Point the new node to its successorI Point the new nodes predecessor to the new node
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 18 / 50
![Page 19: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/19.jpg)
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 19 / 50
![Page 20: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/20.jpg)
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 20 / 50
![Page 21: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/21.jpg)
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 21 / 50
![Page 22: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/22.jpg)
Inserting a New Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 22 / 50
![Page 23: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/23.jpg)
Deleting a Node
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 23 / 50
![Page 24: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/24.jpg)
Deleting a Node (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 24 / 50
![Page 25: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/25.jpg)
Destroying the list
∼List(void)I Use the destructor to release all the memory used by the list.I Step through the list and delete each node one by one.
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 25 / 50
![Page 26: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/26.jpg)
Variations of Linked Lists
Circular linked listsI The last node points to the first node of the listI How do we know when we have finished traversing the list? (Tip:
check if the pointer of the current node is equal to the head.)
Doubly linked listsI Each node points to not only successor but the predecessorI There are two NULL: at the first and last nodes in the listI Advantage: given a node, it is easy to visit its predecessor.
Convenient to traverse lists backwards
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 26 / 50
![Page 27: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/27.jpg)
Doubly-Linked List
insert(X, A): insert node A before X
remove(X)
Problems with operations at ends of list
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 27 / 50
![Page 28: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/28.jpg)
Sentinel Nodes
Dummy head and tail nodes to avoid special cases at ends of listDoubly-linked list with sentinel nodes
Empty doubly-linked list with sentinel nodes
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 28 / 50
![Page 29: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/29.jpg)
Link Inversion
Forward:· · · A → B → C → D · · ·⇑ ↑
prev pres
· · · ← A B → C → D · · ·⇑ ↑
· · · ← A ← B C → D · · ·⇑ ↑
Backward ...
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 29 / 50
![Page 30: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/30.jpg)
Exclusive-Or Doubly Linked List
An XOR linked list compresses the same information into oneaddress field by storing the bitwise XOR (here denoted by
⊕) of
the address for previous and the address for next in one fieldAddr · · · A B C D E · · ·Link ↔ A
⊕C ↔ B
⊕D ↔ C
⊕E
More formally:link(B) = addr(A)
⊕addr(C), link(C) = addr(B)
⊕addr(D), ...
Note: X⊕
X = 0 X⊕
0 = X X⊕
Y = Y⊕
X(X⊕
Y)⊕
Z = X⊕
(Y⊕
Z)
Move forward:at C, take addr(B), XOR it with C’s link value (i.e., B
⊕D), yields
addr(D), and you can continue traversing the list.Move backward: Similar
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 30 / 50
![Page 31: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/31.jpg)
C++ Standard Template Library (STL)
Implementation of common data structuresI Available in C++ library, known as Standard Template Library
(STL)I List, stack, queue,I Generally these data structures are called containers or collections
WWW resourcesI www.sgi.com/tech/stlI www.cppreference.com/cppstl.html
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 31 / 50
![Page 32: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/32.jpg)
Lists Using STL
Two popular implementation of the List ADTI The vector provides a growable array implementation of the List
ADTF Advantage: it is indexable in constant timeF Disadvantage: insertion and deletion are computationally expensive
I The list provides a doubly linked list implementation of the ListADT
F Advantage: insertion and deletion are cheap provided that theposition of the changes are known
F Disadvantage: list is not easily indexable
Vector and list are class templatesI Can be instantiated with different type of items
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50
![Page 33: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/33.jpg)
Iterators
Insert and remove from the middle of the listI Require the notion of a positionI In STL a position is represented by a nested type, iteratorI Example: list <string>:: iterator; vector<int>:: iterator
Iterator: Represents position in the containerThree Issues:
I How one gets an Iterator?I What operations the iterators themselves can perform?I Which LIST ADT methods require iterators as parameters?
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 33 / 50
![Page 34: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/34.jpg)
Example: The Polynomial ADT
An ADT for single-variable polynomials
f (x) =N∑
i=0
aixi
Array implementation
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 34 / 50
![Page 35: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/35.jpg)
The Polynomial ADT
Multiplying two polynomials
P1(x) = 10x1000 + 5x14 + 1
P2(x) = 3x1990 − 2x1492 + 11x + 5
Implementation using a singly linked list
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 35 / 50
![Page 36: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/36.jpg)
Stack ADT
Stack is a list where insert and remove take place only at the topOperations: Constant time
I Push - inserts element on top of stackI Pop - removes element from top of stackI Top - returns element at top of stackI Empty, size
LIFO (Last In First Out)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 36 / 50
![Page 37: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/37.jpg)
Stack Implementation
Singly Linked ListI Push - inserts the element at the front of the listI Pop - deletes the element at the front of the listI Top - returns the element at the front of the list
ArrayI Back, push back, pop back implementation from vectorI theArray and topOfStack which is -1 for an empty stackI Push - increment topOfStack and then set theArray[topOfStack] = xI Pop - return the value to theArray[topOfStack] and then decrement
topOfStack
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 37 / 50
![Page 38: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/38.jpg)
Stack Implementation (cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 38 / 50
![Page 39: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/39.jpg)
Applications of Stack
Balancing symbolsI Compiler checks for program syntax errorsI Every right brace, bracket, and parenthesis must correspond to its
left counterpartI The sequence [()] is legal, but [(]) is wrong
Postfix ExpressionsInfix to Postfix ConversionFunction calls
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 39 / 50
![Page 40: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/40.jpg)
Balancing Symbols
Balancing symbols: ((()())(()))
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 40 / 50
![Page 41: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/41.jpg)
Evaluation of Postfix Expressions
Infix expression: ((1 * 2) + 3) + (4 * 5)Postfix expression: 1 2 * 3 + 4 5 * +
I Unambiguous (no need for parenthesis)I Infix needs parenthesis or else implicit precedence specification to
avoid ambiguityE.g. ”a + b * c” can be ”(a + b) * c” or ”a + (b * c)”
I Postfix expression evaluation uses stackI E.g. Evaluate 1 2 * 3 + 4 5 * +
Rule of postfix expression evaluationI When a number/operand is seen push it onto the stackI When a operator is seen, the operator is applied to the two
numbers (symbols) that are popped from the stack, andI Result is pushed onto the stack
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 41 / 50
![Page 42: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/42.jpg)
Infix to Postfix Conversion
When an operand is read, place it onto the outputOperators are not immediately output, so save them somewhereelse which is stackIf a left parenthesis is encountered, stack itStart with an initially empty stackIf we see a right parenthesis, pop the stack, writing symbols untilwe encounter a left parenthesis which is popped but not outputIf we see any other symbols, then we pop the entries from thestack until we find an entry of lower priority. One exception is wenever remove a ”(” from the stack except when processing a ”)”Finally, if we read the end of input, pop the stack until it is empty,writing symbols onto the output
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 42 / 50
![Page 43: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/43.jpg)
Infix to Postfix Conversion (Cont’d)
Infix: a + b * c + ( d * e + f ) * gPostfix: a b c * + d e * f + g * +
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 43 / 50
![Page 44: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/44.jpg)
Applications of Stack: Function Calls
Programming languages use stack to keep track of function callsWhen a function call occurs
I Push CPU registers and program counter on to stack (activationrecord or stack frame)
I Upon return, restore registers and program counter from top stackframe and pop
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 44 / 50
![Page 45: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/45.jpg)
Queue ADT
Queue is a list where insert takes place at the back, but removetakes place at the frontOperations
I Enqueue - inserts element at the back of queueI Dequeue - removes and returns element from the front of the queue
FIFO (First In First Out)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 45 / 50
![Page 46: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/46.jpg)
Queue Implementation of Array
When an item is enqueued, make the rear index move forward.When an item is dequeued, the front index moves by one elementtowards the back of the queue (thus removing the front item, sono copying to neighboring elements is needed).(front) XXXXOOOOO (rear)
OXXXXOOOO (after 1 dequeue, and 1 enqueue)OOXXXXXOO after another dequeue, and 2 enqueues)OOOOXXXXX (after 2 more dequeues, and 2 enqueues)
The problem here is that the rear index cannot move beyond thelast element in the array.Using a circular array
I OOOOO7963→ 4OOOO7963 (after Enqueue(4))
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 46 / 50
![Page 47: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/47.jpg)
Empty or Full?
Empty queue:I back = front - 1
Full queue?I the same!I Reason: n values to represent n + 1 states
SolutionsI Use a boolean variable to say explicitly whether the queue is empty
or notI Make the array of size n+1 and only allow n elements to be storedI Use a counter of the number of elements in the queue
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 47 / 50
![Page 48: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/48.jpg)
Queue Implementation based on Linked List
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 48 / 50
![Page 49: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/49.jpg)
Queue Implementation based on Linked List (Cont’d)
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 49 / 50
![Page 50: Lists, Stacks, and Queues - 國立臺灣大學ccf.ee.ntu.edu.tw/~yen/courses/ds17/chapter-3.pdf(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 32 / 50 Iterators](https://reader035.vdocuments.mx/reader035/viewer/2022070808/5f0690837e708231d4189dc7/html5/thumbnails/50.jpg)
Applications of Queue
Job scheduling, e.g., Jobs submitted to a line printerCustomer service calls are generally placed on a queue when alloperators are busyPriority queuesGraph traversalsQueuing theory
(Lists, Stacks, and Queues ) Data Structures and Programming Spring 2017 50 / 50