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() 

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Implementing and Using Stacks
Topic 15 Implementing and Using Stacks
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
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
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,
Stack and Queue.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
COSC 2007 Data Structures II
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
1 Stacks and Queues Sections 3.6 and 3.7 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
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.
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.
Final Exam Review COP4530.
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 Stacks.
COMPSCI 107 Computer Science Fundamentals
Homework 4 questions???.
Stacks Stack: restricted variant of list
Priority Queues Sections 6.1 to 6.5.
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.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Stacks, Queues, and Deques
Final Exam Review COP4530.
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Queues Lecture 30 Fri, Apr 2, /3/2019 Queues.
5.3 Implementing a Stack Chapter 5 – The Stack.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

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()  bool empty()  unsigned int size()  constructor and destructor

Uses of ADT Stack Depth first search / backtracking Evaluating postfix expressions Converting infix to postfix Function calls (runtime stack) Recursion

Stack Model—LIFO Empty stack S  S.empty() is true  S.top() not defined  S.size() == 0 S.push(“mosquito”)  S.empty() is false  S.top() == “mosquito”  S.size() == 1 S.push(“fish”)  S.empty() is false  S.top() == “fish”  S.size() == 2 food chain stack

Stack Model—LIFO S.push(“raccoon”)  S.empty() is false  S.top() == “raccoon”  S.size() == 3 S.pop()  S.empty() is false  S.top() == “fish”  S.size() == 2 food chain stack

Queue ADT - FIFO Collection  Elements of some proper type T Operations  void push(T t)  void pop()  T front()  bool empty()  unsigned int size()  Constructors and destructors

Queue Model—FIFO Empty Q animal_parade_queue front back

Queue Model—FIFO Q.push( “ant” ) Q.push( “bee” ) Q.push( “cat” ) Q.push( “dog” ) front back animal_parade_queue back

Queue Model—FIFO Q.pop(); Q.push( “eel” ); Q.pop(); front animal_parade_queue back

Uses of ADT Queue Buffers Breadth first search Simulations Producer-Consumer Problems

Depth First Search—Backtracking Problem  Discover a path from start to goal Solution  Go deep If there is an unvisited neighbor, go there  Backtrack Retreat along the path to find an unvisited neighbor Outcome  If there is a path from start to goal, DFS finds one such path start goal

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

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

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

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

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

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

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

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

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

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

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

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

DFS Implementation DFS() { stack S; // mark the start location as visited S.push(start); while (S is not 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); }

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(); }

Breadth First Search Problem  Find a shortest path from start to goal start goal

Breadth First Search (2) Queue start goal 1 push

Breadth First Search (3) Queue start goal pop

Breadth First Search (4) Queue start goal 234 pop

Breadth First Search (5) Queue start goal pop 34

Breadth First Search (6) Queue start goal 3456 push

Breadth First Search (7) Queue start goal 456 Pop

Breadth First Search (8) Queue start goal Push

Breadth First Search (9) Queue start goal 5678 Pop

Breadth First Search (10) Queue start goal 678 Pop

Breadth First Search (11) Queue start goal 78 Pop

Breadth First Search (12) Queue start goal 789 Push

Breadth First Search (13) Queue start goal 89 Pop

Breadth First Search (14) Queue start goal 8910 Push

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) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }

Evaluating Postfix Expressions Postfix expressions: operands precede operator Tokens: atomics of expressions, either operator or operand Example:  z = 25 + x*(y – 5)  Tokens: z, =, 25, +, x, *, (, y, -, 5, )

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

Evaluating Postfix Expressions (3) Most CPUs have hardware support for this algorithm Translation from infix to postfix also uses a stack (software)

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

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

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

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

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

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

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

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

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

Evaluating Postfix Expressions (13) Input: Pop() == 20 Pop() == 1

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

Evaluating Postfix Expressions (15) Input: 5 + Push(5) 5 21

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

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

Evaluating Postfix Expressions (18) Input: Pop() == 26

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 }

Reading Exercise How to use stack to  Balance brackets  Convert infix to postfix

Runtime Stack Runtime environment  Static Executable code Global variables  Stack Push for each function call Pop for each function return  Heap Dynamic new and delete static stack heap program memory

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

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

Stack Adaptor Requirements Stack  push()  pop()  top()  empty()  size() Can use List, Deque

Queue Adaptor Requirements Queue  push()  pop ()  front()  back()  empty()  size() Can use List, Deque

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(); } };

Declarations Stack > floatStack; Stack > intStack;

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(); } };