1 Stacks and Queues Reading: Sections 3.6 and 3.7.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Rossella Lau Lecture 12, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 12: Stack and Expression Evaluation  Stack basic.
Stacks Chapter 11.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Data Structures and Algorithms (60-254)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Stacks and Queues Sections 3.6 and 3.7. Stack ADT Collections:  Elements of some proper type T Operations:  void push(T t)  void pop()  T top() 
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CMPT 225 Stacks-part2.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues & Deques CSC212.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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)
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
CHP-3 STACKS.
1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Stacks Stacks.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Homework 4 questions???.
Objectives In this lesson, you will learn to: Define stacks
Stacks Stack: restricted variant of list
Andy Wang Data Structures, Algorithms, and Generic Programming
CMSC 341 Lecture 5 Stacks, Queues
Depth First Search—Backtracking
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues 1.
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks Chapter 5.
Stack.
C++ STL Stack, Queue, and Deque
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

1 Stacks and Queues Reading: Sections 3.6 and 3.7

2 Stack ADT - LIFO Collections: –Elements of some proper type T Operations: –Feature: Last In, First Out –void push(T t) –void pop() –T top() –bool empty() –unsigned int size() –constructor and destructor

3 Stack Model—LIFO Empty stack S –S.empty() is true –S.top() not defined –S.size() == 0 food chain stack

4 Stack Model—LIFO S.push(“mosquito”) –S.empty() is false –S.top() == “mosquito” –S.size() == 1 mosquito food chain stack

5 Stack Model—LIFO S.push(“fish”) –S.empty() is false –S.top() == “fish” –S.size() == 2 fish mosquito food chain stack

6 Stack Model—LIFO S.push(“raccoon”) –S.empty() is false –S.top() == “raccoon” –S.size() == 3 raccoon fish mosquito food chain stack

7 Stack Model—LIFO S.pop() –S.empty() is false –S.top() == “fish” –S.size() == 2 fish mosquito food chain stack

8 Implementations and Uses of Stack ADT Implementations –Any list implementation –list and vector C++ STL –Vector/List ADTs push_front()/pop_front() push_back()/pop_back() Uses –Depth first search / backtracking –Evaluating postfix expressions –Converting infix to postfix –Function calls (runtime stack) –Recursion

9 Depth First Search—Backtracking Problem –Discover a path from start to goal Solution –Start from Node start –Stop If node is goal –Go deep If there is an unvisited neighbor, go there –Backtrack Retreat along the path to find an unvisited neighbor, if cannot go deeper Outcome –If there is a path from start to goal, DFS finds one such path start goal

10 Depth First Search—Backtracking (2) Stack start goal 1 Push

11 Depth First Search—Backtracking (3) Stack start goal 2 1 Push

12 Depth First Search—Backtracking (4) Stack start goal Push

13 Depth First Search—Backtracking (5) Stack start goal Push

14 Depth First Search—Backtracking (6) Stack start goal Push

15 Depth First Search—Backtracking (7) Stack start goal Push Pop

16 Depth First Search—Backtracking (8) Stack start goal Push Pop

17 Depth First Search—Backtracking (9) Stack start goal 2 1 Push Pop

18 Depth First Search—Backtracking (10) Stack start goal 1 Push Pop

19 Depth First Search—Backtracking (11) Stack start goal 3 1 Push

20 Depth First Search—Backtracking (12) Stack start goal Push

21 Depth First Search—Backtracking (13) Stack start goal Push

22 DFS Implementation DFS() { stack S; // mark the start location as visited S.push(start); while (!S.empty()) { t = S.top(); if (t == goal) Success(S); if (// t has unvisited neighbors) { // choose an unvisited neighbor n // mark n visited; S.push(n); } else { BackTrack(S); } Failure(S); }

23 DFS Implementation (2) BackTrack(S) { while (!S.empty() && S.top() has no unvisited neighbors) { S.pop(); } Success(S) { // print success while (!S.empty()) { output(S.top()); S.pop(); } Failure(S) { // print failure while (!S.empty()) { S.pop(); }

24 Evaluating Postfix Expressions Infix expression –Operators in middle of operands –25 + x*(y – 5) Postfix expressions –operands precede operator –Z = 25 x y 5 - * + Tokens: atomics of expressions, either operator or operand Example: –25 + x*(y – 5) –Tokens: 25, +, x, *, (, y, -, 5, )

25 Evaluating Postfix Expressions (2) Evaluation algorithm: –Use stack of tokens –Repeat If operand, push onto stack If operator –pop operands off the stack –evaluate operator on operands –push result onto stack Until expression is read Return top of stack Most CPUs have hardware support for this algorithm Translation from infix to postfix also uses a stack (software)

26 Evaluating Postfix Expressions (3) Original expression: –1 + (2 + 3) * Evaluate: – * + 5 +

27 Evaluating Postfix Expressions (4) Input: * Push(1) 1

28 Evaluating Postfix Expressions (5) Input: * Push(2) 2 1

29 Evaluating Postfix Expressions (6) Input: * Push(3) 3 2 1

30 Evaluating Postfix Expressions (7) Input: + 4 * Pop() == 3 Pop() == 2 1

31 Evaluating Postfix Expressions (8) Input: + 4 * Push(2 + 3) 5 1

32 Evaluating Postfix Expressions (9) Input: 4 * Push(4) 4 5 1

33 Evaluating Postfix Expressions (10) Input: * Pop() == 4 Pop() == 5 1

34 Evaluating Postfix Expressions (11) Input: * Push(5 * 4) 20 1

35 Evaluating Postfix Expressions (12) Input: Pop() == 20 Pop() == 1

36 Evaluating Postfix Expressions (13) Input: Push(1 + 20) 21

37 Evaluating Postfix Expressions (14) Input: 5 + Push(5) 5 21

38 Evaluating Postfix Expressions (15) Input: + Pop() == 21 Pop() == 5

39 Evaluating Postfix Expressions (16) Input: + Push(21 + 5) 26

40 Evaluating Postfix Expressions (17) Input: Pop() == 26

41 Postfix Evaluation Implementation Evaluate(postfix expression) { // use stack of tokens; while(// expression is not empty) { t = next token; if (t is operand) { // push onto stack } else { // pop operands for t off stack // evaluate t on these operands // push result onto stack } // return top of stack }

42 Infix to Postfix Conversion Depends on operator precedence and associativity We present a limited version –+, -, *, /, (, ) –Assuming usual precedence and associativity High level idea –If input token is an operand, output directly –If input token is an operator, we need to compare the precedence of this operator with other neighboring operators, output the one with highest precedence –Parentheses need to handle differently ( has highest precedence when encountered in input compared to operators in stack, so we always push a ( ) is used to pop everything till ( in stack

43 Example Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b +

44 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c + *

45 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + +

46 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d + (

47 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d e + ( *

48 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d e * f + ( +

49 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d e * f + +

50 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d e * f + g + *

51 Example (cont’d) Infix: –a + b * c + ( d * e + f ) * g Postfix –a b c * + d e * f + g * + a + b * c + ( d * e + f ) * g a b c * + d e * f + g * +

Infix to Postfix Conversion 52 void infix2postfix(const vector &infix) { stack s; for (I = 0; I < infix.size(); ++I) { if (infix[I] is operand) print infix[I]; else if (infix[I] is +, -, *, /, or ( ) { while (s.top() != ‘(‘ && s.top().precedence >= infix[I].precedence) { print s.top(); s.pop(); } s.push(infix[I]); } else if (infix[I] == ) ) { while (s.top() != ( ) { print s.top(); s.pop(); } s.pop(); // remove ( } while (!s.empty()) { print s.top(); s.pop(); } }

53 Runtime Stack Runtime environment –Static Executable code Global variables –Stack Push for each function call Pop for each function return Local variables –Heap Dynamically allocated memories new and delete static stack heap program memory

54 Recursion Order 1: function calls itself Order 2: f() calls g(), and g() calls f() Facilitated by stack

55 Reading Exercise How to use stack to –To check if brackets are balance? (Section 3.6.3)

56 Queue ADT - FIFO Collection –Elements of some proper type T Operations –Feature: First In, First Out –void push(T t) –void pop() –T front() –bool empty() –unsigned int size() –Constructors and destructors

57 Queue Model—FIFO Empty Q animal parade queue

58 Queue Model—FIFO Q.Push(“ant”) ant front back animal parade queue

59 Queue Model—FIFO Q.Push(“bee”) antbee front back animal parade queue

60 Queue Model—FIFO Q.Push(“cat”) antbeecat front back animal parade queue

61 Queue Model—FIFO Q.Push(“dog”) antbeecatdog front back animal parade queue

62 Queue Model—FIFO Q.Pop() beecatdog front back animal parade queue

63 Queue Model—FIFO Q.Pop() catdog front back animal parade queue

64 Queue Model—FIFO Q.Push(“eel”) Q.Pop() eel front back animal parade queue

65 Implementations and Uses of Queue ADT Implementations –Any list implementation push_front()/pop_back() push_back()/pop_front() Uses –Buffers –Breadth first search –Simulations –Producer-Consumer Problems

66 Breadth First Search Problem –Find a shortest path from start to goal Solution –Start from Node start –Visit All neighbors of the node –Stop If a neighbor is goal –Otherwise Visit neighbors two hops away –Repeat (Stop/Otherwise) Visiting neighbors N hops away start goal

67 Breadth First Search (2) Queue start goal 1 Push

68 Breadth First Search (3) Queue start goal Pop

69 Breadth First Search (4) Queue start goal 234 Push

70 Breadth First Search (5) Queue start goal Pop 34

71 Breadth First Search (6) Queue start goal 3456 Push

72 Breadth First Search (7) Queue start goal 456 Pop

73 Breadth First Search (8) Queue start goal Push

74 Breadth First Search (9) Queue start goal 5678 Pop

75 Breadth First Search (10) Queue start goal 678 Pop

76 Breadth First Search (11) Queue start goal 78 Pop

77 Breadth First Search (12) Queue start goal 789 Push

78 Breadth First Search (13) Queue start goal 89 Pop

79 Breadth First Search (14) Queue start goal 8910 Push

80 BFS Implementation BFS { queue Q; // mark the start location as visited Q.push(start); while (Q is not empty) { t = Q.front(); for (// each unvisited neighbor n of node t) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }

81 Adaptor Class Adapts the public interface of another class Adaptee: the class being used Adaptor: the new class being defined –Uses protected object of the adaptee type –Uses the adaptee’s methods to define adaptor methods Stack and Queue implemented via adaptor classes

82 Stack Adaptor Requirements Stack –push() –pop() –top() –empty() –size() Can use List, Deque –Push(): push_back() –Pop(): pop_back()

83 Class Stack template class Stack { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_back(); } T top() const { return c.back(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } }; Declaration –Stack > floatStack; –Stack > intStack; For STL stack container –template > class stack; –stack charStack;

84 Queue Adaptor Requirements Queue –push() –pop () –front() –empty() –size() Can use List, Deque –Push(): push_back() –Pop(): pop_front()

85 Class Queue template class Queue { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_front(); } T front() const { return c.front(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } }; Declaration Queue > floatQueue; Queue > intQueue; For STL stack container template > class queue; queue charQueue;

86 Reading assignment Double-end queues –Section A problem to consider –A palindrome is a sequence of characters that can be same way forward and backward. –Can you think of a recursive algorithm to determine if an input string (line) is a palindrome or not. Character case is ignored (that is, low-case and upper-case characters are considered the same). The new line character is not part of the input string.