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.

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

Automata Theory December 2001 NPDAPart 3:. 2 NPDA example Example: a calculator for Reverse Polish expressions Infix expressions like: a + log((b + c)/d)
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
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.
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 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.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Stack Applications.
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.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Linear Data Structures LIFO – Polish notation Context Saving.
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)
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
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.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Stacks Chapter 3 Objectives Upon completion you will be able to
Stacks. Stack ADT where we can only work with "top" – Top() : get value on top – Pop() : remove value on top – Push(value) : put new value on top.
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.
BCA II Data Structure Using C
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.
Stacks Chapter 6.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Stack as an ADT.
ASTs, Grammars, Parsing, Tree traversals
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
ADTs, Grammars, Parsing, Tree traversals
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.
COMPUTER 2430 Object Oriented Programming and Data Structures I
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.
Stacks.
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: 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 accessed n As in lists, each node contains some data (‘soap’ or ‘garlic’) n The data may also be numeric or other type (‘5’ or ‘true’)

cis 335 Fall 2001 Barry Cohen Last in, first out (LIFO) n This restriction on access is sometimes called LIFO - ‘last in, first out’ n Example: the dishes in a cafeteria n Question: Is a movie line a stack? Is it LIFO?

cis 335 Fall 2001 Barry Cohen Stack operations n createStack() n destroyStack() n bool isEmpty() n push(in newItem) Put an item onto the stack n pop(out topItem) Remove and return top item n top(out topItem) Return top item, but don’t remove it. (Is this operation necessary?)

cis 335 Fall 2001 Barry Cohen List or stack? n Can a list do everything a stack can do? n Can a stack do everything a list can do? n Compare the operations. n Which to use? The simplest which can do the job.

cis 335 Fall 2001 Barry Cohen Example: valid parentheses n It this a legal expression? ((a + b) / (c - d / 2) n First test: an even number of parens n It this a legal expression? ((a + (b / (c - d ) / 2) n Second test: equal number of open and close parens n It this a legal expression? (a + b) / c) - (d (e / 2)

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