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.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Data Structures and Algorithms (60-254)
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Balancing Symbols 3. Applications
Topic 15 Implementing and Using Stacks
1 Postfix Demo: The Equation Infix: (1 + (2 * ((3 + (4 * 5)) * 6))) Postfix: * + 6 * * + 3+1(2(*((+45*)*)6))()(45*)3(+)(*6)(2*)+1() 3+12*+45**6.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
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,
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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,
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
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.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Stacks and Queues Introduction to Computing Science and Programming I.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
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.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
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.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Reverse Polish Notation Written by J.J. Shepherd.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Programming Abstractions
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.
Data Abstraction & Problem Solving with C++
Stacks Chapter 6.
Revised based on textbook author’s notes.
CSC 172 DATA STRUCTURES.
Stacks Stack: restricted variant of list
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
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.
Data Structures and Algorithms for Information Processing
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Computer Science 2 5/17/2016 Finish the Queue Program
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Applications of Stacks and Queues
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
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:

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 some data (‘soap’ or ‘garlic’ or 5) n Access is FIFO - first in, first out.

cis 335 Fall 2001 Barry Cohen First in, first out (FIFO) n This restriction on access is sometimes called FIFO - ‘first in, first out’ n Example: a movie line. n A computer example: a buffer of characters read from the keyboard.

cis 335 Fall 2001 Barry Cohen Queue operations n createQueue() n destroyQueue() n bool isEmpty() n enqueue(in newItem:NewItemType) Put an item onto the stack n dequeue() Remove the earliest item. No value returned n getFront(out queueFront:QueueItemType) Return earliest item, but don’t remove it.

cis 335 Fall 2001 Barry Cohen Palindrome test n A palindrome reads the same backward and forward. (‘Madam, I’m Adam.’) n Use stack and queue to test. n Put the string in each, and then compare.

cis 335 Fall 2001 Barry Cohen STL class queue n #include n It this a legal expression? ((a + (b / (c - d ) / 2) n bool empty() const; n size_type size() const; n T &front(); n T &back(); n void pop(); n void push(const T& x);

cis 335 Fall 2001 Barry Cohen Use the ‘counting test’ n Get an intuition: open paren: +1 close paren: -1 n Start with 0 n Value must always be nonnegative n Must end with 0

cis 335 Fall 2001 Barry Cohen Test expression using a stack n Start with empty stack n Open paren: push n Close paren: pop n End with empty stack

cis 335 Fall 2001 Barry Cohen Problems with stack solutions n Evaluating algebraic equations n Parsing a computer language (like C) n Parsing human language (at least some of it) n Executing computer programs (including recursive ones)

cis 335 Fall 2001 Barry Cohen Evaluate a postfix expression n An expression like you find on HP calculator (also called ‘reverse Polish notation’ - RPN) n Evaluating infix is complicated n Evaluating postfix is easy n Solution: infix -> postfix evaluate postfix

cis 335 Fall 2001 Barry Cohen Keep it simple n Assume correct expression n Only binary operators (+, -, *, /). (No unary ‘-’, for example.) n Only left to right order (no ‘**’, for example). n Each number is a single digit (0-9)

cis 335 Fall 2001 Barry Cohen Evaluate postfix n Evaluate postfix: * n Algorithm: while (there’s still input) * see a number * push it * see an op * pop two numbers from stack * apply op * push result