Stacks. The stack ADT Stack Implementation. Stack Examples. using arrays using generic linked lists using List ADT. EECS 268 Programming II 1

Similar documents
Data Structures and Algorithms V Otávio Braga

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Module 2 Stacks and Queues: Abstract Data Types

Data Structures and Algorithms Stacks and Queues

Chapter 3: Restricted Structures Page 1

7.1 Our Current Model

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Algorithms and Data Structures

Linked Lists Linked Lists, Queues, and Stacks

DATA STRUCTURES USING C

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Cpt S 223. School of EECS, WSU

10CS35: Data Structures Using C

Stacks. Linear data structures

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Universidad Carlos III de Madrid

Application of Stacks: Postfix Expressions Calculator (cont d.)

Common Data Structures

Introduction to Data Structures

2) Write in detail the issues in the design of code generator.

PES Institute of Technology-BSC QUESTION BANK

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structure [Question Bank]

St S a t ck a ck nd Qu Q eue 1

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Moving from CS 61A Scheme to CS 61B Java

3 Abstract Data Types and Search

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Introduction to Stacks

Stacks. Data Structures and Data Types. Collections

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Glossary of Object Oriented Terms

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

DATA STRUCTURE - STACK

Sequential Data Structures

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Introduction to Data Structures and Algorithms

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Data Structures UNIT III. Model Question Answer

Lecture 9. Semantic Analysis Scoping and Symbol Table

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

14 Stacks, Queues, And Linked Lists

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Stack Allocation. Run-Time Data Structures. Static Structures

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Data Structure with C

Fast nondeterministic recognition of context-free languages using two queues

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

STACKS,QUEUES, AND LINKED LISTS

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

CmpSci 187: Programming with Data Structures Spring 2015

Lecture 2: Data Structures Steven Skiena. skiena

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Analysis of a Search Algorithm

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

AFF 808. Sub. Code 4BIT1C1. B.Sc. DEGREE EXAMINATION, NOVEMBER First Semester. Information Technology

Java Software Structures

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Symbol Tables. Introduction

Data Structures. Chapter 8

Converting a Number from Decimal to Binary

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Data Structures and Algorithms

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

The Elective Part of the NSS ICT Curriculum D. Software Development

Chapter 5 Instructor's Manual

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

Java Application Developer Certificate Program Competencies

C++ INTERVIEW QUESTIONS

Big O and Limits Abstract Data Types Data Structure Grand Tour.

THE FLORIDA STATE UNIVERSITY COLLEGE OF ARTS AND SCIENCES APPLICATION CONFIGURABLE PROCESSORS CHRISTOPHER J. ZIMMER

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Syntaktická analýza. Ján Šturc. Zima 208

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Programming with Data Structures

C Compiler Targeting the Java Virtual Machine

Curriculum Map. Discipline: Computer Science Course: C++

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Transcription:

Stacks The stack ADT Stack Implementation using arrays using generic linked lists using List ADT Stack Examples 1

Stacks and Queues Linear data structures each item has specific first, next, and previous relations with other items in the set examples: arrays, linked lists, vectors, strings Stacks and queues are special types of lists with restricted operations restrict how the items are added and removed from the list 2

Stacks and Queues Stacks Last In First Out (LIFO) add/delete semantics Push() to add item only to the top or front of the list Pop() to remove/delete only the top or front item from the list Queues First In First Out (FIFO) add/delete semantics Enqueue() to add item to the end of the list Dequeue() to remove item from the front of the list will study in next chapter 3

Stacks Analogy of stack stack of dishes in cafeteria Several real-world examples subroutine call stack management at runtime implicitly used during recursion language parsing parenthesis matching algebraic expression evaluation Push Pop 4

Stack ADT Operation Contract for the ADT Stack isempty():boolean {query} push(in newitem:stackitemtype) throw StackException pop() throw StackException pop(out stacktop:stackitemtype) throw StackException gettop(out stacktop:stackitemtype) {query} throw StackException 5

Stack Implementation Can use linked lists or arrays for implementing stacks more important is the interface that is exposed to the developer! A program can use a stack independently of the stack s implementation Use axioms to define an ADT stack formally Example: Specify that the last item inserted is the first item to be removed (astack.push(newitem)).pop()= astack 6

Example: Reverse a List Traverse and output a list in reverse Solution can use either stack or recursion recursion uses the implicit call stack see C6-reverseList.cpp 7

Checking for Balanced Braces Problem: Develop an algorithm to read an expression one symbol at a time and check for matching braces A stack can be used to verify whether a program contains balanced braces An example of balanced braces abc{defg{ijk}{l{mn}}op}qr An example of unbalanced braces abc{def}}{ghij{kl}m 8

Checking for Balanced Braces Function performed by parsers/compilers also in several editors, like emacs, vi Requirements for balanced braces Each time you encounter a }, it matches an already encountered { When you reach the end of the string, you have matched each { Use the stack API as done in the previous problem to develop your own solution. 9

Checking for Balanced Braces Stepping through the algorithm for 3 expressions 10

Recognizing Strings in a Language L = {w$w : w is a possibly empty string of characters other than $, w = reverse(w) } A solution using a stack Traverse the first half of the string, pushing each character onto a stack Once you reach the $, for each character in the second half of the string, match a popped character off the stack see C6-strRecog.cpp 11

Implementations of the ADT Stack The ADT stack can be implemented using An array will have size limit A linked list The ADT list All three implementations use a StackException class to handle possible exceptions 12

Implementations of the ADT Stack Figure 6-4 Implementations of the ADT stack that use (a) an array; (b) a linked list; (c) an ADT list 13

An Array-Based Implementation of the ADT Stack Private data fields An array of items of type StackItemType The index top to the top item Compiler-generated destructor and copy constructor Figure 6-5 An array-based implementation see C6-StackA.cpp 14

A Pointer-Based Implementation of the ADT Stack A pointer-based implementation Enables the stack to grow and shrink dynamically topptr is a pointer to the head of a linked list of items A copy constructor and destructor must be supplied see C6-StackP.cpp 15

An Implementation That Uses the ADT List The ADT list can represent the items in a stack Let the item in position 1 of the list be the top push(newitem) insert(1, newitem) pop() remove(1) gettop(stacktop) retrieve(1, stacktop) see C6-StackL.cpp 16

Comparing Implementations Fixed size versus dynamic size A statically allocated array-based implementation Fixed-size stack that can get full Prevents the push operation from adding an item to the stack, if the array is full A dynamically allocated array-based implementation or a pointer-based implementation No size restriction on the stack 17

Comparing Implementations A pointer-based implementation vs. one that uses a pointer-based implementation of the ADT list Pointer-based implementation is more efficient ADT list approach reuses an already implemented class Much simpler to write Saves programming time 18

Application: Algebraic Expressions Infix expressions most commonly used a*b-c, a+b+c/d need operator precedence rules and parenthesis Postfix / prefix expressions definitive, unambiguous grammars no need for precedence rules or parenthesis Infix Prefix Postfix a*b-c -*abc ab*ca*(b-c) *a-bc abc-* 19

Application: Algebraic Expressions Postfix/prefix expressions are easier to evaluate than infix Evaluate an infix expression convert the infix expression to postfix form evaluate the postfix expression We use stack can use either array, pointer, or List ADT based implementation interface of stack ADT is important implementation of the stack ADT is not 20

Evaluating Postfix Expressions When an operand is entered push it onto a stack When an operator is entered apply it to the top two operands of the stack pop the operands from the stack push the result of the operation onto the stack Simplifying assumptions the string is a syntactically correct postfix expression no unary operators are present no exponentiation operators are present operands are single lowercase letters that represent integer values 21

Evaluating Postfix Expressions 22

Converting Infix Expressions to Equivalent Postfix Expressions Evaluate infix expression by first converting it into an equivalent postfix expression Facts about converting from infix to postfix operands always stay in the same order with respect to one another operator will move only to the right with respect to the operands all parentheses are removed Stack used to hold pending operators until they can be emitted in their right position 23

Converting Infix Expressions to Equivalent Postfix Expressions Steps to process infix expression append an operand to the end of an initially empty string postfixexpr push ( onto a stack push an operator onto the stack, if stack is empty; otherwise pop operators and append them to postfixexpr as long as they have a precedence >= that of the operator in the infix expression at ), pop operators from stack and append them to postfixexpr until ( is popped see C6-InfixEval.cpp 24

Infix to Postfix Expressions Convert a-(b+c*d)/e to postfix 25

Application: A Search Problem Indicate whether a sequence of flights exists from the origin city to the destination city The flight map is a graph two adjacent vertices are joined by an edge a directed path is a sequence of directed edges 26

Stack-Based Nonrecursive Solution The solution performs an exhaustive search feasible only for small search spaces beginning at the origin city, try every possible sequence of flights until either we find a sequence that gets to the destination city we determines that no such sequence exists Backtracking used to recover from choosing a wrong city 27

Stack-Based Nonrecursive Solution 28

A Recursive Solution Possible outcomes of recursive search strategy we eventually reach the destination city and can conclude that it is possible to fly from the origin to the destination we reach a city C from which there are no departing flights we go around in circles 29

A Recursive Solution A refined recursive search strategy searchr(in origincity:city, in destinationcity:city):boolean Mark origincity as visited if (origincity is destinationcity) Terminate -- the destination is reached else for (each unvisited city C adjacent to origincity) searchr(c, destinationcity) 30

The Relationship Between Stacks and Recursion Typically, stacks are used by compilers to implement recursive methods during execution, each recursive call generates an activation record that is pushed onto a stack Stacks can be used to implement a nonrecursive version of a recursive algorithm 31

Summary ADT stack operations have a last-in, first-out (LIFO) behavior Have a wide range of practical applications algorithms that operate on algebraic expressions flight maps A strong relationship exists between recursion and stacks 32