Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Data Structures Lecture 5 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Chapter 3 Stacks.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
Click to edit Master text styles Stacks Data Structure.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks (and Queues).
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Stacks © 2013 Goodrich, Tamassia, Goldwasser Stacks.
Stacks.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Stacks.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks.
Stacks.
Computing Spans Given an an array X, the span S[i] of X[i] is
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Stacks

2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow

Stacks3 The Stack ADT The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think of a spring-loaded plate dispenser Main stack operations: push(object o): inserts element o pop(): removes and returns the last inserted element Auxiliary stack operations: getTop(): returns a reference to the last inserted element without removing it size(): returns the number of elements stored isEmpty(): returns a Boolean value indicating whether no elements are stored

Stacks4 Applications of Stacks Direct applications Page-visited history in a Web browser Undo sequence in a text editor Saving local variables when one function calls another, and this one calls another, and so on. Indirect applications Auxiliary data structure for algorithms Component of other data structures

Stacks5 Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then error (Empty Stack) else t  t  1 return S[t + 1]

Stacks6 Array-based Stack (cont.) The array storing the stack elements may become full A push operation will then throw a Full Stack Error condition Limitation of the array- based implementation Not intrinsic to the Stack ADT S 012 t … Algorithm push(o) if (t = S.length  1) then error (Full Stack) else t  t + 1 S[t]  o

Stacks7 Example: What does this code slice do? int main( ) { int n; double item; double numbers[n]; printf( " Type in an integer n followed by n decimal numbers.\n"); printf(" The numbers will be printed in reverse order.\n”); scanf(“%d”, &n); for (int i = 0; i < n; i++) { scanf(“%d, &item); push(item); } printf( “\n\n”); while (!empty( )) { printf( “%d ”, getTop( )); item = pop( ); } return 0; }

Stacks8 Stack: Example const int MaxStack = 10; const char EmptyFlag = '\0'; int top; int items[MaxStack]; enum { FullStack = MaxStack, EmptyStack = -1 }; // better enum Err_code {FullStack...} top = EmtyStack; bool push(int); int pop(); int getTop() bool empty(); bool full(); bool push(int x){ if (full()) return false; items[++top] = x; return true; } int pop(){ if (empty()) return EmptyFlag; return items[top--]; } int getTop(){ if (empty()) return EmptyStack; return items[top]; }

Stacks9 Stack: Example bool full() { if (top + 1 == FullStack) { printf( "Stack Full at %d \n“, MaxStack); return true; } return false; } bool empty(){ if (top == EmptyStack) { printf("Stack empty \n“); return true; } return false; } int main(void){ /* Define stack */ int s1 [ MaxStack ]; /* fill in here the necessary statements that use stack functions */ }

Stacks10 Exp: Prime Divisors of a Number Read an integer and print all its prime divisors in descending order. Note that the smallest divisor greater than 1 of any integer is guaranteed to be a prime 2100/2 = /2 = /2 = /2 = /3 = /3 = /5 = /5 = /5 = /5 = /7= /7= *5*5*3*2*2 = *5*5*3*2*2 = 2100

Stacks11 Reverse Polish Calculator ? Read an operand push it onto the stack +,-,*,/ Arithmetic operations = Print the top of the stack Examples: ?a?b+= Read and store a and b, calculate and store their sum, and then print the sum (a+b)= ?a?b +?c?d+*= do (a+b)*(c+d) and print ?a?b?c - = * ?d + = do (a*(b-c))+d and print: 1. Push a, b, c onto the stacks, 2. Replace the pair b,c by b-c and print its value, 3. Calculate a*(b-c), 4. Push d onto the stack. 5. Finally, calculate and print (a*(b - z)) + d.

Stacks12 Get command of calculator char get command( ) { char command; bool waiting = true; printf( "Select command and press :“); while (waiting) { scanf(“%c”, &command); command = tolower(command); if (command == ‘?’ || command == ‘=’ || command == ‘+’ || command == ‘−’|| command == ‘*’ || command == ‘/’ || command == ‘q’) waiting = false; else { printf( "Please enter a valid command: \n" ); printf(“[?]push to stack [=]print top \n"); printf(“[+] [−] [*] [/] are arithmetic operations \n"); printf(“[Q]uit. \n"); } return command; }

Stacks13 Adding and printing ?p?q+= bool do_command(char command) { double p, q; switch (command) { case ‘?’: printf(“Enter a number: "); flush(); scanf(“%d, &p); if (push(p) == false) printf(“Warning: Stack full, lost number \n"); break; case ‘=’: if ( (p = getTop()) == EmptyFlag) printf(“Stack empty \n”); else printf(“%d \n”, p); break; case ‘+’: if ((p = getTop()) == EmptyFlag) printf(“Stack empty \n”); else { p = pop( ); if ((q = getTop()) == EmptyFlag { printf(“Stack has just one entry\n”); push(p); } else { q = pop( ); if (push(q + p) == false) printf(“Warning: Stack full, lost result\n”); } } break; /* Add options for further user commands. */ case ‘q’: printf(“Calculation Finished.\n"; return false; } return true; }

Stacks14 Exp: Bracket checking int main( ) /* Post: Notice the use of any bracket mismatch in the standard input */ char symbol, match; bool is_matched = true; while (is_matched && (symbol = getchar( )) != “\n”) { if (symbol == ‘{‘ || symbol == ‘(‘ || symbol == ‘[‘) push(symbol); if (symbol == ‘}’ || symbol == ‘)’ || symbol == ‘]’) { if (empty( )) { printf("Unmatched closing bracket %c detected \n“); is_matched = false; } else { match = getTop(); match = pop( ); is_matched = (symbol == ‘}’ && match == ‘{‘) || (symbol == ‘)’ && match == ‘(‘) || (symbol == ‘]’ && match == ‘[‘); if (!is_matched) printf("Bad match %c %c \n“, match, symbol”); } if (! empty( )) printf("Unmatched opening bracket(s) detected.\n“); }

Stacks15 Performance and Limitations Performance Let n be the number of elements in the stack The space used is O(n) Each operation runs in time O(1) Limitations The maximum size of the stack must be defined a priori, and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception

Stacks16 Computing Spans Given an an array X, the span S[i] of X[i] is the maximum number of consecutive elements X[j] immediately preceding X[i] such that X[j]  X[i] Spans have applications to financial analysis E.g., stock at 52-week high X S

Stacks17 Quadratic Algorithm Algorithm spans1(X, n) Input array X of n integers Output array S of spans of X # S  new array of n integers n for i  0 to n  1 don s  1n while s  i  X[i  s]  X[i] 1  2  …  (n  1) s  s  11  2  …  (n  1) S[i]  s n return S 1 Algorithm spans1 runs in O(n 2 ) time

Stacks18 Computing Spans with a Stack Keep in a stack the indices of the elements visible when “looking back” Scan the array from left to right Let i be the current index Pop indices from the stack until finding index j such that X[i]  X[j] Set S[i]  i  j Push x onto the stack

Stacks19 Linear Algorithm Algorithm spans2(X, n) # S  new array of n integers n A  new empty stack 1 for i  0 to n  1 don while (  A.isEmpty()  X[top()]  X[i] ) don j  A.pop()n if A.isEmpty() then n S[i]  i  1n else S[i]  i  jn A.push(i)n return S 1 Each index of the array Is pushed into the stack exactly once Is popped from the stack at most once The statements in the while-loop are executed at most n times Algorithm spans2 runs in O(n) time

Stacks20 Growable Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(o) if t = S.length  1 then A  new array of size … for i  0 to t do A[i]  S[i] S  A t  t + 1 S[t]  o

Stacks21 Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T(n)/n

Stacks22 Incremental Strategy Analysis We replace the array k = n / c times The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c( … + k) = n + ck(k + 1)/2 Since c is a constant, T(n) is O(n + k 2 ), i.e., O(n 2 ) The amortized time of a push operation is O(n)

Stacks23 Doubling Strategy Analysis We replace the array k = log 2 n times The total time T(n) of a series of n push operations is proportional to n …+ 2 k = n  2 k + 1  1 = 2n  1 T(n) is O(n) The amortized time of a push operation is O(1) geometric series