CST230 -- Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Stacks, Queues, and Linked Lists
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.
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
queues1 Queues Data structures that wait their turn.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Chapter 3 Stacks.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks.
Chapter 3 Collection Classes Anshuman Razdan Department of Engineering
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.
Stacks1 Stacks The unorganized person’s data structure.
Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies
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,
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,
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
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.
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,
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
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.
Data Structures and Algorithms
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
Information and Computer Sciences University of Hawaii, Manoa
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
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.
Stacks  Introduction  Applications  Implementations  Complex Applications.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
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.
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.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
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.
Click to edit Master text styles Stacks Data Structure.
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 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Revised based on textbook author’s notes.
Stacks.
Stacks Chapter 7 introduces the stack data type.
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.
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Stack.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing Studies

CST Razdan et al2 Stacks A stack is a data structure of ordered items such that items can be inserted and removed only at one end (called the top). It is a last-in-first-out (LIFO) data structure typical interface: –isEmpty –peek (also called top) –pop –push –size

CST Razdan et al3 Sample Stack Implementation from Main pages array implementation (use array to store stack items) methods: clone, ensureCapactiy, getCapacity, isEmpty, peek, pop, push, size, trimtoSize

CST Razdan et al4 public class ObjectStack implements Cloneable{ private Object[] data; private int manyItems; public ObjectStack(){ final int INITIAL_CAPACITY = 10; manyItems = 0; data = new Object[ INITIAL_CAPACITY ]; } public ObjectStack( int initialCapacity ){ if( initialCapacity < 0 ) throw new IllegalArgumentException( "initialCapacity too small “ + initialCapacity); manyItems = 0; data = new Object[ initialCapacity ]; }

CST Razdan et al5 public Object clone(){ ObjectStack answer; try{ answer = (ObjectStack) super.clone(); } catch( CloneNotSupportedException e ){ throw new RuntimeException( "This class does not implement Cloneable."); } answer.data = (Object[]) data.clone(); return answer; }

CST Razdan et al6 public void ensureCapacity( int minimumCapacity ){ Object biggerArray[]; if( data.length < minimumCapacity ){ biggerArray = new Object[ minimumCapacity ]; System.arraycopy( data, 0, biggerArray, 0, manyItems ); data = biggerArray; } public int getCapacity(){ return data.length; } public boolean isEmpty(){ return( manyItems == 0 ); }

CST Razdan et al7 public Object peek(){ if( manyItems == 0 ) throw new EmptyStackException(); return data[manyItems-1]; } public Object pop(){ Object answer; if( manyItems == 0 ) throw new EmptyStackException(); answer = data[--manyItems]; data[manyItems] = null; return answer; }

CST Razdan et al8 public Object peek(){ if( manyItems == 0 ) throw new EmptyStackException(); return data[manyItems-1]; } public Object pop(){ Object answer; if( manyItems == 0 ) throw new EmptyStackException(); answer = data[--manyItems]; data[manyItems] = null; return answer; }

CST Razdan et al9 public void trimToSize(){ Object trimmedArray[]; if( data.length != manyItems ){ trimmedArray = new Object[manyItems]; System.arraycopy( data, 0, trimmedArray, 0, manyItems ); data = trimmedArray; }

CST Razdan et al10 Stack Applications Balanced Parentheses Expression evaluation Postfix Expression evaluation

CST Razdan et al11 Balanced Parentheses main idea: –scan expression from left to right. –whenever open paren(, {, [ is encountered, push on stack. –whenever close paren ), }, ] is encountered pop matching open paren off stack. if the matching paren is not at the top of the stack, parantheses were not balanced –the stack should be empty after all characters in the expression have been encountered. Example: {[X+Y*(Z+7)]*(A+B)}

CST Razdan et al12 Expression Evaluation evaluate well-parenthesized expression main idea: –Push open paren ( to stack –push numbers to stack –push operators to different stack –when close paren encountered, pop operation, two numbers, and open paren. perform calculation, and push result –stack should contain a single result after processing entire expression. Example: (((6+9) / 3) * (6 – 4))

CST Razdan et al13 Notations for Arithmetic Expressions we typically use infix notation for arithmetic expressions, in which the operator is placed in between the operands. –e.g., other notation for arithmetic expressions: –prefix – operator placed before operands e.g., –postfix – operator placed after operands e.g., 2 3 +

CST Razdan et al14 Postfix Expressions nice characteristic of prefix and postfix notation is that no parentheses are EVER needed. convert (10 – (4 * 2)) to: –prefix –postfix

CST Razdan et al15 Evaluation of Postfix Expression do if( next input is number ) read input and push onto stack else{ read next character, which is operation pop two numbers off stack combine numbers using the operation push result onto stack } while( there is more input in expression ) answer is single value on stack

CST Razdan et al16 Example Evaluate: * + 4 – 5 +