Chapter 5 ADTs Stack and Queue Yanjun Li

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

Chapter 5 ADTs Stack and Queue 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 Yanjun Li CS 2200 2

Stacks of Coins and Bills Yanjun Li CS 2200 3

Stacks of Coins and Bills Yanjun Li CS 2200 3

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

Stacks of Boxes and Books 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 ? 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. Yanjun Li CS 2200 6

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

Stacks • Logical level • What operations would be appropriate for a stack? 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? 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 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; } Yanjun Li CS 2200 10

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

Stack Applications • Finding palindromes (Lab!) 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 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. 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; Yanjun Li CS 2200 13

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

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

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

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

Array-Based Implementation Give a series of operations that could produce this situation Yanjun Li

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

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

Array-Based Implementation To what do we initialize data member top ? 0 or -1 When push, do we increment or store first? 0: store and increment -1: increment and store Which is better ? • Think about member function Top() Stack. Type: : Stack. Type() { top = -1; } Yanjun Li CS 2200 17

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_ITEMS); } What does const mean? Yanjun Li CS 2200 18

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. bool Stack. Type: : Push(Item. Type new. Item) { top++; items[top] = new. Item; } Yanjun Li CS 2200 19

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 • Stack underflow – The condition that results from trying to pop an empty stack Yanjun Li CS 2200 20

Updated Stacks #include "Item. Type. h" #include "Full. Stack. h" #include "Empty. Stack. h"

Updated Stacks #include "Item. Type. h" #include "Full. Stack. h" #include "Empty. Stack. h" class Stack. Type { public: Stack. Type(); ~Stack. Type(); bool Is. Empty() const; bool Is. Full() const; void Push(Item. Type item) throw(Full. Stack); void Pop( ) throw (Empty. Stack); Item. Type Top() const throw (Empty. Stack); Yanjun Li CS 2200 21

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. bool Stack. Type: : Push(Item. Type new. Item) throw (Full. Stack) { if (Is. Full()) throw Full. Stack(); top++; What is Full. Stack( ) ? items[top] = new. Item; } Yanjun Li CS 2200 22

Exception Class: Full. Stack #ifndef FULLSTACK_H #define FULLSTACK_H #include <stdexcept> using std: : runtime_error;

Exception Class: Full. Stack #ifndef FULLSTACK_H #define FULLSTACK_H #include <stdexcept> using std: : runtime_error; class Full. Stack : public runtime_error { public: Full. Stack() : runtime_error("The stack is full") { } }; #endif Yanjun Li CS 2200 23

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]); } Yanjun Li CS 2200 24

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() throw (Empty. Stack) { 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 throw (Empty. Stack) { if (Is. Empty()) throw Empty. Stack(); return (items[top]); } What is Empty. Stack ? Yanjun Li CS 2200 25

Exception Class: Empty Stack #ifndef EMPTYSTACK_H #define EMPTYSTACK_H #include <stdexcept> using std: : runtime_error;

Exception Class: Empty Stack #ifndef EMPTYSTACK_H #define EMPTYSTACK_H #include <stdexcept> using std: : runtime_error; class Empty. Stack : public runtime_error { public: Empty. Stack() : runtime_error("The stack is empty"){ } }; #endif Yanjun Li CS 2200 26

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. • Black-box strategy to check. – Try-Catch block Yanjun Li CS 2200 27

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; Yanjun Li CS 2200 28

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 ? Yanjun Li CS 2200 29

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 Yanjun Li CS 2200 30

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? } Yanjun Li CS 2200 31

Linked Implementation More than one item Yanjun Li CS 2200 32

Linked Implementation More than one item Yanjun Li CS 2200 32

Linked Implementation One item Yanjun Li CS 2200 33

Linked Implementation One item Yanjun Li CS 2200 33

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; } Yanjun Li CS 2200 34

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

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

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. Yanjun Li CS 2200 36

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) Yanjun Li CS 2200 37

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. Yanjun Li CS 2200 38