Chapter 5 ADTs Stack and Queue Fall 2013

  • Slides: 59
Download presentation
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS 2200 1

Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS 2200 1

Outline • Stack – Array-based Implementation – Linked Implementation • Queue – Array-based Implementation

Outline • Stack – Array-based Implementation – Linked Implementation • Queue – Array-based Implementation – Linked Implementation • Comparison Fall 2013 Yanjun Li CS 2200 2

Stacks of Coins and Bills Fall 2013 Yanjun Li CS 2200 3

Stacks of Coins and Bills Fall 2013 Yanjun Li CS 2200 3

Stacks of Boxes and Books TOP OF THE STACK Fall 2013 TOP OF THE

Stacks of Boxes and Books TOP OF THE STACK Fall 2013 TOP OF THE STACK Yanjun Li CS 2200 4

Stacks • Logical level • What do these composite objects all have in common

Stacks • Logical level • What do these composite objects all have in common ? Fall 2013 Yanjun Li CS 2200 5

Stacks • Stack • An abstract data type in which elements are added and

Stacks • Stack • An abstract data type in which elements are added and removed from only one end. • A “last in, first out” (LIFO) structure. Fall 2013 Yanjun Li CS 2200 6

Stacks • Logical level • What operations would be appropriate for a stack? Fall

Stacks • Logical level • What operations would be appropriate for a stack? Fall 2013 Yanjun Li CS 2200 7

Stacks • Transformers – Push – Pop change state • Observers – Is. Empty

Stacks • Transformers – Push – Pop change state • Observers – Is. Empty observe state – Is. Full – Top What about an iterator? Fall 2013 Yanjun Li CS 2200 8

Stacks • Application Level • For what type of problems would stacks be useful?

Stacks • Application Level • For what type of problems would stacks be useful? • LIFO: A stack is great for reversing data. – Operating system function calls – Finding palindromes – Expression evaluation & syntax parsing Fall 2013 Yanjun Li CS 2200 9

Stack Applications • Operating system function calls void Draw. Square(int x, int y, int

Stack Applications • Operating system function calls void Draw. Square(int x, int y, int edge) { Draw. Line(x, y, edge, HORIZONTAL); Draw. Line(x, y, edge, VERTICAL); Draw. Line(x+edge, y, edge, VERTICAL); Draw. Line(x, y+edge, HORIZONTAL); } int main () { Draw. Square(1, 2, 3); return 0; } Fall 2013 Yanjun Li CS 2200 10

Stack Applications • Finding palindromes (Lab!) Fall 2013 Yanjun Li CS 2200 11

Stack Applications • Finding palindromes (Lab!) Fall 2013 Yanjun Li CS 2200 11

Stack Applications • Help Converting Decimal to Binary (pseudocode) 1) Read (number) 2) Loop

Stack Applications • Help Converting Decimal to Binary (pseudocode) 1) Read (number) 2) Loop (number > 0) 1) digit = number modulo 2 (number%2) 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan • Problem: The binary numbers are printed backwards. – 19 becomes 11001 instead of 10011 • Solution: push each binary number onto the stack and pop the digit out of the stack and print it at the end. Fall 2013 Yanjun Li CS 2200 12

Stacks class Stack. Type { public: Stack. Type(); ~Stack. Type(); bool Is. Empty() const;

Stacks class Stack. Type { public: Stack. Type(); ~Stack. Type(); bool Is. Empty() const; bool Is. Full() const; void Push(Item. Type item); void Pop( ); Item. Type Top() const; Fall 2013 Yanjun Li CS 2200 13

Stacks • Implementation Level • Array-based Implementation • Linked Structure Fall 2013 Yanjun Li

Stacks • Implementation Level • Array-based Implementation • Linked Structure Fall 2013 Yanjun Li CS 2200 14

Array-Based Implementation private: int top; Item. Type items[MAX_ITEM]; }; [0] [1] [2] …. [M.

Array-Based Implementation private: int top; Item. Type items[MAX_ITEM]; }; [0] [1] [2] …. [M. . ] stack. items. top Fall 2013 Yanjun Li CS 2200 15

Array-Based Implementation Give a series of operations that could produce this situation Fall 2013

Array-Based Implementation Give a series of operations that could produce this situation Fall 2013 Yanjun Li CS 2200 16

Array-Based Implementation To what do we initialize top ? 0 or -1 Do we

Array-Based Implementation To what do we initialize top ? 0 or -1 Do we increment or store first? 0: store and increment -1: increment and store Which is better? (what does top represent? ) Stack. Type: : Stack. Type() { top = -1; } Fall 2013 Yanjun Li CS 2200 17

Array-Based Implementation • Before we code, we must consider error conditions • Stack overflow

Array-Based Implementation • Before we code, we must consider error conditions • Stack overflow – The condition that results from trying to push an element on to a full stack • Exception class : Full. Stack • Stack underflow – The condition that results from trying to pop an empty stack • Exception class: Empty. Stack Fall 2013 Yanjun Li CS 2200 18

Array-Based Implementation //pre: Stack has been initialized. //post: function value = (stack is empty)

Array-Based Implementation //pre: Stack has been initialized. //post: function value = (stack is empty) bool Stack. Type: : Is. Empty() const { return ( top == -1); } //pre: stack has been initialized. //post: function value = (stack is full) bool Stack. Type: : Is. Full() const { return ( top == MAX_ITEM-1); } What does const mean? Fall 2013 Yanjun Li CS 2200 19

Array-Based Implementation //pre: stack has been initialized and is not full //post: new. Item

Array-Based Implementation //pre: stack has been initialized and is not full //post: new. Item is at the top of the stack. void Stack. Type: : Push(Item. Type new. Item) { top++; items[top] = new. Item; } Fall 2013 Yanjun Li CS 2200 20

Array-Based Implementation //pre: stack has been initialized. //post: if stack is full, throw an

Array-Based Implementation //pre: stack has been initialized. //post: if stack is full, throw an exception; //Else new. Item is at the top of the stack. void Stack. Type: : Push(Item. Type new. Item) { if (Is. Full()) throw Full. Stack(); top++; What is Full. Stack( ) ? items[top] = new. Item; } Fall 2013 Yanjun Li CS 2200 21

Array-Based Implementation //pre: stack has been initialized and is not empty. //post: top element

Array-Based Implementation //pre: stack has been initialized and is not empty. //post: top element has been removed from stack. void Stack. Type: : Pop() { top--; } //pre: stack has been initialized and is not empty. //post: A copy of the top element is returned. Item. Type Stack. Type: : Top() const { return (items[top]); } Fall 2013 Yanjun Li CS 2200 22

Array-Based Implementation //pre: stack has been initialized. //post: if stack is empty, throw an

Array-Based Implementation //pre: stack has been initialized. //post: if stack is empty, throw an exception; else top element //has been removed from stack. void Stack. Type: : Pop() { if (Is. Empty()) top--; throw Empty. Stack(); } //pre: stack has been initialized. //post: if stack is empty, throw an exception; else a copy of the //top element is returned. Item. Type Stack. Type: : Top() const { if (Is. Empty()) throw Empty. Stack(); return (items[top]); } Fall 2013 What is Empty. Stack ? Yanjun Li CS 2200 23

Array-Based Implementation Which functions have to be changed if we dynamically allocate the array

Array-Based Implementation Which functions have to be changed if we dynamically allocate the array items ? Do we need to add any new functions? Fall 2013 Yanjun Li CS 2200 24

Test Plan • Clear-box strategy to check each operation. – Push() & Pop() while

Test Plan • Clear-box strategy to check each operation. – Push() & Pop() while it is empty or full. Fall 2013 Yanjun Li CS 2200 25

Linked Implementation • The logical level (public part of the class declaration) stays the

Linked Implementation • The logical level (public part of the class declaration) stays the same; class Stack. Type { public: Stack. Type(); ~Stack. Type(); bool Is. Empty() const; bool Is. Full() const; void Push(Item. Type item); void Pop( ); Item. Type Top() const; Fall 2013 Yanjun Li CS 2200 26

Linked Implementation The implementation level (private part of the class declaration) changes private: Node.

Linked Implementation The implementation level (private part of the class declaration) changes private: Node. Type* top. Ptr; }; . info . next ‘D’ Stack. top. Ptr Pointer to the next node in the stack Can we “borrow” code from Unsorted. Type for Push and Pop ? Fall 2013 Yanjun Li CS 2200 27

Linked Implementation //pre: the stack is not full //post: the new item is added

Linked Implementation //pre: the stack is not full //post: the new item is added on top of the stack void Stack. Type: : Push(Item. Type new. Item) { Node. Type* location; location = new Node. Type; location->info = new. Item; location>next = top. Ptr; top. Ptr = location; }. info . next . info new. Item . next ‘D’ . top. Ptr location Fall 2013 Yanjun Li CS 2200 28

Linked Implementation //pre: the stack is not empty //post: the top item is removed

Linked Implementation //pre: the stack is not empty //post: the top item is removed from the top of the //stack void Stack. Type: : Pop() { Does this Node. Type* temp. Ptr; work for temp. Ptr = top. Ptr; stacks of top. Ptr = top. Ptr->next; one item? delete temp. Ptr; More than one item? } Fall 2013 Yanjun Li CS 2200 29

Linked Implementation More than one item Fall 2013 Yanjun Li CS 2200 30

Linked Implementation More than one item Fall 2013 Yanjun Li CS 2200 30

Linked Implementation One item Fall 2013 Yanjun Li CS 2200 31

Linked Implementation One item Fall 2013 Yanjun Li CS 2200 31

Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all

Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all but Top() from class Unsorted. Type List //pre: the stack is not empty //post: the item on the top of the stack is //returned. Item. Type Stack. Type: : Top() { return top. Ptr->info; } Fall 2013 Yanjun Li CS 2200 32

Other Member Functions • Constructor • Destructor – Free all the node spaces. •

Other Member Functions • Constructor • Destructor – Free all the node spaces. • is. Empty • is. Full Fall 2013 Yanjun Li CS 2200 33

Array vs. Linked Structure • A serious drawback of array-based implementation: the size of

Array vs. Linked Structure • A serious drawback of array-based implementation: the size of a stack must be determined when a stack object is declared. – Size is not enough or – Space is wasted. Fall 2013 Yanjun Li CS 2200 34

Big-O Comparison Time Array-Based Linked Implementation Class constructor Class destructor O(1) O(n) Is. Full()

Big-O Comparison Time Array-Based Linked Implementation Class constructor Class destructor O(1) O(n) Is. Full() Is. Empty() O(1) Push() Pop() O(1) Fall 2013 Yanjun Li CS 2200 35

Queues Fall 2013 Yanjun Li CS 2200 36

Queues Fall 2013 Yanjun Li CS 2200 36

Queues Fall 2013 Yanjun Li CS 2200 37

Queues Fall 2013 Yanjun Li CS 2200 37

Queues • What do these composite objects all have in common? Fall 2013 Yanjun

Queues • What do these composite objects all have in common? Fall 2013 Yanjun Li CS 2200 38

Queues Queue An abstract data type in which elements are added to the rear

Queues Queue An abstract data type in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure Fall 2013 Yanjun Li CS 2200 39

Queues • What operations would be appropriate for a queue? Fall 2013 Yanjun Li

Queues • What operations would be appropriate for a queue? Fall 2013 Yanjun Li CS 2200 40

Queues • Transformers – Make. Empty – Enqueue – Dequeue change state • Observers

Queues • Transformers – Make. Empty – Enqueue – Dequeue change state • Observers – Is. Empty – Is. Full observe state What about an iterator? Fall 2013 Yanjun Li CS 2200 41

Queues • For what type of problems would stacks be useful? – Print server

Queues • For what type of problems would stacks be useful? – Print server • maintains a queue of print jobs. – Disk driver • maintains a queue of disk input/output requests. – Scheduler (e. g. , in an operating system) • maintains a queue of processes awaiting a slice of machine time. Fall 2013 Yanjun Li CS 2200 42

Queues class Queue. Type { public: Queue. Type(int max); Queue. Type(); ~Queue. Type(); bool

Queues class Queue. Type { public: Queue. Type(int max); Queue. Type(); ~Queue. Type(); bool Is. Empty() const; bool Is. Full() const; void Enqueue(Item. Type item); Logical Level //add new. Item to the rear of the queue. void Dequeue(Item. Type& item); //remove front item from queue Fall 2013 Yanjun Li CS 2200 43

Array-Based Implementation private: Item. Type* items; // Dynamically allocated array int max. Que; //

Array-Based Implementation private: Item. Type* items; // Dynamically allocated array int max. Que; // Whatever else we need }; Implementation level Fall 2013 Yanjun Li CS 2200 44

Array-Based Implementation One data structure: An array with the front of the queue fixed

Array-Based Implementation One data structure: An array with the front of the queue fixed in the first position Enqueue A, B, C, D Dequeue Move elements down What’s wrong with this design? Fall 2013 Yanjun Li CS 2200 45

Array-Based Implementation Another data structure: An array where the front floats What happens if

Array-Based Implementation Another data structure: An array where the front floats What happens if we add X, Y, and Z ? Fall 2013 Yanjun Li CS 2200 46

Array-Based Implementation We can let the queue wrap around in the array; i. e.

Array-Based Implementation We can let the queue wrap around in the array; i. e. treat the array as a circular structure Fall 2013 Yanjun Li CS 2200 47

Array-Based Implementation (a) Initial conditions A [0] [1] [2] front = 2 [3] [4]

Array-Based Implementation (a) Initial conditions A [0] [1] [2] front = 2 [3] [4] rear = 2 Empty Queue (b) queue. Dequeue(item) front = 3 [0] [1] [2] [3] [4] rear = 2 Full Queue How can we tell the difference? Fall 2013 Yanjun Li CS 2200 48

Array-Based Implementation A third data structure: Front indicates the slot preceding the front item;

Array-Based Implementation A third data structure: Front indicates the slot preceding the front item; it is reserved and not used Empty Queue Full Queue Fall 2013 Yanjun Li CS 2200 49

Array-Based Implementation private: int front; int rear; int max. Que; Complete implementation level Item.

Array-Based Implementation private: int front; int rear; int max. Que; Complete implementation level Item. Type* items; }; To what do we initialize front and rear ? Fall 2013 Yanjun Li CS 2200 50

Array-Based Implementation Queue. Type: : Queue. Type( int max) { max. Que = max

Array-Based Implementation Queue. Type: : Queue. Type( int max) { max. Que = max + 1; front = max. Que - 1; rear = max. Que - 1; items = new Item. Type[max. Que]; } Queue. Type: : Queue. Type( ) { max. Que = 500 + 1; front = max. Que - 1; rear = max. Que - 1; items = new Item. Type[max. Que]; } Why is the array declared max + 1 ? Queue. Type: : ~Queue. Type( ) { delete [] items; } Fall 2013 Yanjun Li CS 2200 51

Array-Based Implementation //Pre: the queue is not full //Post: new. Item is at the

Array-Based Implementation //Pre: the queue is not full //Post: new. Item is at the rear of the queue void Queue. Type: : Enqueue(Item. Type new. Item) { rear = (rear + 1) % max. Que; items[rear] = new. Item; } Fall 2013 Yanjun Li CS 2200 52

Array-Based Implementation //pre: the queue is not empty //post: the front of the queue

Array-Based Implementation //pre: the queue is not empty //post: the front of the queue has been removed // and a copy returned in item void Queue. Type: : Dequeue(Item. Type& item) { front = (front + 1) % max. Que; item = items[front]; } Fall 2013 Yanjun Li CS 2200 53

Array-Based Implementation //returns true if the queue is empty bool Queue. Type: : Is.

Array-Based Implementation //returns true if the queue is empty bool Queue. Type: : Is. Empty( ) { return ( rear == front ); } //returns true if the queue is full bool Queue. Type: : Is. Full( ) { return ( (rear + 1) % max. Que == front ) } Fall 2013 Yanjun Li CS 2200 54

Linked Implementation Data structure for linked queue What data members are needed? Fall 2013

Linked Implementation Data structure for linked queue What data members are needed? Fall 2013 Yanjun Li CS 2200 55

Linked Implementation Enqueue(new. Item) Set new. Node to the address of newly allocated node

Linked Implementation Enqueue(new. Item) Set new. Node to the address of newly allocated node Set Info(new. Node) to new. Item Set Next(new. Node) to NULL Set Next(rear) to new. Node If queue is empty Set front to new. Node else Set Next(rear) to new. Node Fall 2013 Yanjun Li CS 2200 56

Linked Implementation Dequeue(item) Set temp. Ptr to front Set item to Info(front) Set front

Linked Implementation Dequeue(item) Set temp. Ptr to front Set item to Info(front) Set front to Next(front) if queue is now empty Set rear to NULL Deallocate Node(temp. Ptr) Fall 2013 Yanjun Li CS 2200 57

Storage Requirements What does this table tell you ? Fall 2013 Yanjun Li CS

Storage Requirements What does this table tell you ? Fall 2013 Yanjun Li CS 2200 58

Reference • Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale.

Reference • Reproduced from C++ Plus Data Structures, 4 th edition by Nell Dale. • Reproduced by permission of Jones and Bartlett Publishers International. Fall 2013 Yanjun Li CS 2200 59