Stacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks

Similar documents
Lecture 4: Stacks and Queues

EENG/INFE 212 Stacks

MIT Loop Optimizations. Martin Rinard

Lists, Stacks, and Queues (plus Priority Queues)

Introduction to Computing II (ITI 1121) MIDTERM EXAMINATION

Data Structures and Algorithms Winter Semester

Computing Static Single Assignment (SSA) Form

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Solution suggestions for examination of Logic, Algorithms and Data Structures,

Compiling Techniques

Queues. Principles of Computer Science II. Basic features of Queues

Binary Search Trees. Motivation

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Tutorial 4. Dynamic Set: Amortized Analysis

Introduction to Computing II (ITI1121) FINAL EXAMINATION

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal

A Simple Implementation Technique for Priority Search Queues

Warmup. A programmer s wife tells him, Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.

Part I: Definitions and Properties

ITI Introduction to Computing II

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Theory of Computation

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Notes for Comp 497 (Comp 454) Week 10 4/5/05

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

CSC D70: Compiler Optimization Static Single Assignment (SSA)

Topics on Compilers

Searching, mainly via Hash tables

Amortized analysis. Amortized analysis

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

10:00 12:30. Do not open this problem booklet until the start of the examination is announced.

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

Computer Science Introductory Course MSc - Introduction to Java

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations(ODE), v0.12.

ENS Lyon Camp. Day 5. Basic group. C October

Since CFL is closed under intersection with regular languages, we can do

Data Structures and Algorithm. Xiaoqing Zheng

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

Notes for Comp 497 (454) Week 10

MTH401A Theory of Computation. Lecture 17

Lecture 14: Nov. 11 & 13

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

Abstract Data Types. EECS 214, Fall 2017

CS481F01 Prelim 2 Solutions

The Midterm Exam. Hsuan-Tien Lin. Department of CSIE, NTU. OOP Class, April 26-27, 2010

CSE 105 THEORY OF COMPUTATION

Computability and Complexity

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Please give details of your answer. A direct answer without explanation is not counted.

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Appendix of Computational Protein Design Using AND/OR Branch and Bound Search

Heaps and Priority Queues

Bin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then

Construction of Static Single-Assignment Form

Rumination on the Formal Definition of DPDA

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

FP-growth and PrefixSpan

Speculative Parallelism in Cilk++

Lecture Overview SSA Maximal SSA Semipruned SSA o Placing -functions o Renaming Translation out of SSA Using SSA: Sparse Simple Constant Propagation

Section 1 (closed-book) Total points 30

Outline. 1 Merging. 2 Merge Sort. 3 Complexity of Sorting. 4 Merge Sort and Other Sorts 2 / 10

Lecture 7: Amortized analysis

Solutions to EoPL3 Exercises

Outline Resource Introduction Usage Testing Exercises. Linked Lists COMP SCI / SFWR ENG 2S03. Department of Computing and Software McMaster University

Programming with classical quantum datatypes

Übung Informatik I - Programmierung - Blatt 7

MATHEMATICAL OBJECTS in

CS361 Homework #3 Solutions

CISC4090: Theory of Computation

CS 21 Decidability and Tractability Winter Solution Set 3

Skip Lists. What is a Skip List. Skip Lists 3/19/14

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Tute 10. Liam O'Connor. May 23, 2017

Syntax Analysis Part III

Representing Multidimensional Trees. David Brown, Colin Kern, Alex Lemann, Greg Sandstrom Earlham College

Context-Free Languages

MiniMat: Matrix Language in OCaml LLVM

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

Lecture 4: Nondeterministic Finite Automata

Data Structures and Algorithms " Search Trees!!

CSE 105 THEORY OF COMPUTATION

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

Efficient Enumeration of Regular Languages

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work

Models of Computation. by Costas Busch, LSU

CS 314 Principles of Programming Languages

Outline 1 PCP. 2 Decision problems about CFGs. M.Mitra (ISI) Post s Correspondence Problem 1 / 10

Pushdown Automata. Chapter 12

Skip List. CS 561, Lecture 11. Skip List. Skip List

(pp ) PDAs and CFGs (Sec. 2.2)

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

CS20a: Turing Machines (Oct 29, 2002)

Constructors - Cont. must be distinguished by the number or type of their arguments.

Non-context-Free Languages. CS215, Lecture 5 c

CSE302: Compiler Design

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Transcription:

Stacks Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks

Stacks: Definitions and Operations A LIFO list: Last In, First Out. Restricted form of list: Insert/Remove only at front or TOP of list. Accessible Element: TOP Data Structures 2 Stacks

Stack Operations Create/Delete IsFull()/IsEmpty() Push (Insert), Pop (remove) TopValue() Stack Implementations Array Based. Linked List Based. Data Structures 3 Stacks

Array-Based Stack top D top A C B A top E B A top F A push (A) push (B) push (C) push (D) pop () pop () push (E) pop () pop () push (F) top pop () pop () Data Structures 4 Stacks

Stack Interface / Stack ADT / public interface Stack<E> { / R e i n i t i a l i z e the stack. The user i s responsible f o r e c l a i m i n g the storage used by the stack elements. public void c l e a r ( ) ; / Push an element onto the top of the stack. p u b l i c void push (E i t ) ; / Remove and r e t u r n the element at the top of the sta p u b l i c E pop ( ) ; / @return A copy of the top element. / public E topvalue ( ) ; / @return The number of elements i n the stack. / public i n t l e n g t h ( ) ; Data Structures 5 Stacks

Array-Based Stack / Array based stack implementation / class AStack<E> implements Stack<E> { private s t a t i c f i n a l i n t d e f a u l t S i z e = 10; private i n t maxsize ; / / Maximum size of stack private i n t top ; / / Index f o r top Object private E [ ] l i s t A r r a y ; / / Array holding stack / / Constructors AStack ( ) { this ( d e f a u l t S i z e ) ; AStack ( i n t size ) { maxsize = size ; top = 0; l i s t A r r a y = (E [ ] ) new Object [ size ] ; / / Create l i s t A r r a y Data Structures 6 Stacks

Array-Based Stack public void c l e a r ( ) / / R e i n i t i a l i z e stack { top = 0; public void push (E i t ) { / / Push i t onto stack a s s e r t top!= maxsize : Stack i s f u l l ; l i s t A r r a y [ top ++] = i t ; public E pop ( ) { / / Pop top element a s s e r t top!= 0 : Stack i s empty ; return l i s t A r r a y [ top ] ; public E topvalue ( ) { / / Return t o p element a s s e r t top!= 0 : Stack i s empty ; return l i s t A r r a y [ top 1]; public i n t l e n g t h ( ) { return top ; / / Return l e n g t h Data Structures 7 Stacks

Linked Stacks A simplified version of the linked list, where insertions/deletions are only through the head of the list. Freelist implementation is an example. No header node is used. Data Structures 8 Stacks

Linked Stack Implementation class LStack<E> implements Stack<E> { private Link<E> top ; / / P o i n t e r to f i r s t element private i n t size ; / / Number of elements / / Constructors public LStack ( ) { top = null ; s ize = 0; public LStack ( i n t sz ) { top = null ; s ize = 0; / / R e i n i t i a l i z e stack public void c l e a r ( ) { top = null ; s ize = 0; public void push (E i t ) { / / Put i t on stack top = new Link<E>( i t, top ) ; size ++; Data Structures 9 Stacks

Linked Stack Implementation public E pop ( ) { / / Remove i t from stack a s s e r t top!= null : Stack i s empty ; E i t = top. element ( ) ; top = top. next ( ) ; size ; return i t ; public E topvalue ( ) { / / Return top value a s s e r t top!= null : Stack i s empty ; return top. element ( ) ; public i n t l e n g t h ( ) { return size ; / / Return l e n g t h Data Structures 10 Stacks

Stack Application:Expression Evaluation Infix to Postfix Conversion A + B C D E // Infix Form ABC DE + // Postfix Form Evaluation of expressions in postfix form. Data Structures 11 Stacks

Stack Application:Recursion Subroutine calls need to preserve return addresses, parameters and local variables. Implemented using a program stack. Data Structures 12 Stacks

Stack Application:Infix To Postfix I n i t i a l i z e the stack. / / stack i s empty for ( each operator or operand i n the i n p u t expression ) { i f ( an operand ) add to P o s t x expression else i f ( an operator ) { i f ( stack i s empty ) push oper. i n t o stack else { while ( i n p u t oper. prec. < than stack top oper. prec ) { pop operator from stack, add to P o s t f i x expression i f stack i s empty, break / / out of the loop push i n p u t oper. i n t o stack while ( stack i s not empty ) { pop the stack ; add operator to P o s t x expression % Data Structures 13 Stacks

Stack Application:Postfix Expression Evaluation I n i t i a l i z e the stack. / / stack i s empty for ( each operator or operand i n the i n p u t expression ){ i f ( an operand ) push i n t o stack else i f ( an operator ) { opnd2 = pop stack opnd1 = pop stack r e s u l t = Evaluate opnd1 oper opnd2 push r e s u l t onto stack r e s u l t = pop stack Data Structures 14 Stacks