The Stack and Queue Types Lecture 10 Hartmut Kaiser

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Chapter 16 Stacks and Queues Saurav Karmakar Spring 2007.
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stacks CHAPTER 7 MAY 21, 2015 Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005.
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)
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.
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
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)
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
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.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Chapter 5.
Stacks Stacks.
Homework 4 questions???.
Stacks Chapter 7 introduces the stack data type.
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Stacks Chapter 4.
Stack application: postponing data usage
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Presentation transcript:

The Stack and Queue Types Lecture 10 Hartmut Kaiser

Programming Principle of the Day Do the simplest thing that could possibly work ▫A good question to ask one’s self when programming is “What is the simplest thing that could possibly work?” ▫This helps keep us on the path towards simplicity in the design. 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 2

Abstract This lecture will focus on two other sequential data types, the stack and the queue. We will use stacks to implement conversion between ‘normal expressions’ and the equivalent reverse polish notation. 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 3

Introduction to Stacks A stack is a last-in-first-out (LIFO) data structure Limited access vector (or list) Main operations: ▫Adding an item  Referred to as pushing it onto the stack ▫Removing an item  Referred to as popping it from the stack CSC 1254, Fall 2012, Stacks and Queues 4 10/2/2012, Lecture 10

CSC 1254, Fall 2012, Stacks and Queues 5 Introduction to Stacks Definition: ▫An ordered collection of data items ▫Can be accessed at only one end (the top) Operations: ▫Construct a stack (usually empty) ▫Check if it is empty ▫push: add an element to the top ▫top: retrieve the top element ▫pop: remove the top element ▫size: returns number of elements in stack 10/2/2012, Lecture 10

Introduction to Stacks Useful for ▫Reversing a sequence ▫Managing a series of undo-actions ▫Tracking history when browsing the web ▫Function call hierarchy is implemented with a stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 6

7 Push Push means place a new data element at the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

8 Push (cont.) Push means place a new data element at the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

9 Push (cont.) Push means place a new data element at the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

10 Push (cont.) Push means place a new data element at the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

11 Pop Pop means take a data element off the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

12 Pop (cont.) Pop means take a data element off the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

13 Pop (cont.) Pop means take a data element off the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

14 Pop (cont.) Pop means take a data element off the top of the stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

15 Top Top means retrieve the top of the stack without removing it 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

16 Top (cont.) Top means retrieve the top of the stack without removing it 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

17 Top (cont.) Top means retrieve the top of the stack without removing it 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

18 Linked-List Stack Stacks can also be implemented with a linked list The front node is the top of the stack In fact, there is std::stack<> which is an adaptor usable with different types of underlying containers (std::list is one possibility) std::stack : implements a stack of ‘T’s T top(); void push(T const&); void pop(); std::stack ::size_type size() const; bool empty(); 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

Linked-List Stack (cont.) To pop, we remove the node at the front of the linked list, and return the element to the client… 19 top 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

Linked-List Stack (cont.) To pop, we remove the node at the front of the linked list, and return the element to the client… 20 top 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

Linked-List Stack (cont.) To push, we place the new element in a node and insert it at the front of the linked list… 21 top 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

Linked-List Stack (cont.) To push, we place a new element in a node and insert it at the front of the linked list… 22 top 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues

Example: Implementing a Stack Implementing a stack on top of a list is trivial ▫Live coding 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 23

Application of Stacks Consider the arithmetic statement in the assignment: x = a * b + c Compiler must generate machine instructions: LOAD a MULT b ADD c STORE x CSC 1254, Fall 2012, Stacks and Queues 24 Note: this is "infix" notation The operators are between the operands 10/2/2012, Lecture 10

Reverse Polish Notation Postfix Notation ▫Most compilers convert an expression in infix notation to postfix  The operators are written after the operands ▫So a * b + c becomes a b * c + ▫Advantage:  Expressions can be written without parentheses 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 25

Postfix and Prefix Examples InfixRPN (Postfix)Prefix A + BA B ++ A B A * B + CA B * C ++ * A B C A * (B + C)A B C + ** A + B C A – (B – (C – D))A B C D – – –– A – B – C – D A – B – C – DA B – C – D –– – – A B C D 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 26 Prefix: Operators come before the operands

Evaluating RPN Expressions "By hand" (Underlining technique): 1.Scan the expression from left to right to find an operator. 2.Locate ("underline") the last two preceding operands and combine them using this operator. 3.Repeat until the end of the expression is reached. Example: * 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 27

Evaluating RPN Expressions *  *  *  * 2 8 * 16 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 28 2 * ((3 + 4) – (5 – 6))

Evaluating RPN Expressions By using a stack algorithm ▫Initialize an empty stack Repeat the following until the end of the expression is encountered ▫Get the next token (const, var, operator) in the expression ▫Operand – push onto stack Operator – do the following  Pop 2 values from stack  Apply operator to the two values  Push resulting value back onto stack When end of expression encountered, value of expression is the (only) number left in stack 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 29 Note: if only 1 value on stack, this is an invalid RPN expression

CSC 1254, Fall 2012, Stacks and Queues 30 Evaluating of Postfix Note the changing status of the stack 10/2/2012, Lecture 10

Converting between Notations By hand: Represent infix expression as an expression tree: 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 31 A * B + C + C * A B A * (B + C) * A + B C ((A + B) * C) / (D - E) C A B D E * + / -

Converting RPN (Postfix) Traverse the tree in Left-Right-Parent order (post-order) to get RPN: 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 32 C AB D E * + / - A B + C * D E - /

Converting to Prefix Traverse tree in Parent-Left-Right order (pre- order) to get prefix: 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 33 / * + A B C - D E C AB D E * + / -

Converting to Infix Traverse tree in Left-Parent-Right order (in- order) to get infix (must insert parenthesis) 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 34 ((A + B)* C)/(D – E)) C AB D E * + / -

Another RPN Conversion Method By hand: "Fully parenthesize-move-erase" method: 1.Fully parenthesize the expression. 2.Replace each right parenthesis by the corresponding operator. 3.Erase all left parentheses. 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 35 A * B + C   ((A B * C +  A B * C + A * (B + C)   (A (B C + *  A B C + * ((A * B) + C) (A * (B + C ) )

Stack Algorithm Initialize an empty stack of operators While no error and not end of expression ▫Get next input "token" from infix expression ▫If token is …  "(": push onto stack  ")": pop and display stack elements until "(" occurs, do not display it 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 36 constants, variables, operators, left or right parenthesis

Stack Algorithm  operator if operator has higher priority than top of stack push token onto stack else pop and display top of stack repeat comparison of token with top of stack  operand display it When end of infix reached, pop and display stack items until empty 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 37 Note: Left parenthesis in stack has lower priority than operators

Introduction to Queue A queue is a first-in-first-out (FIFO) data structure Limited access vector (or list) Main operations: ▫Adding an item  Referred to as pushing it onto the queue (enqueue an item, adding to the end) ▫Removing an item  Referred to as popping it from the queue (dequeue an item, remove from the front) CSC 1254, Fall 2012, Stacks and Queues 38 10/2/2012, Lecture 10

CSC 1254, Fall 2012, Stacks and Queues 39 Introduction to Queues Definition: ▫An ordered collection of data items ▫Can be read at only one end (the front) and written to only at the other end (the back) Operations: ▫Construct a queue (usually empty) ▫Check if it is empty ▫push: add an element to the back ▫front: retrieve the front element ▫pop: remove the front element ▫size: returns number of elements in queue 10/2/2012, Lecture 10

Introduction to Queues Useful for ▫All kind of simulations (traffic, supermarket, etc.) ▫Computers use queues for scheduling  Handling keyboard events  Handling mouse events  Scrolling the screen  Printer spooler ▫C++ I/O streams are queues  Even if we only access one end, the other end is managed by the operating system 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 40

Example: Queue of Strings void manage_queue() { queue waiting_strings; // queue of 'waiting' strings while (true) { cout "; // ask for next line of text string response; getline(cin, response); if (response.empty()) break; if (response == "next") { // try to dequeue if (waiting_strings.empty()) cout << "No one waiting!" << endl; else { cout << waiting_strings.front() << endl; waiting_strings.pop(); } else { // enqueue the line read waiting_strings.push(response); } 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 41

Example: Implementing a Queue Implementing a queue on top of a list is equally trivial ▫Live coding 10/2/2012, Lecture 10 CSC 1254, Fall 2012, Stacks and Queues 42