Lecture 12 ADTs and Stacks

Similar documents
What is an algorithm?

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Stack Abstract Data Type

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Revised based on textbook author s notes.

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

March 13/2003 Jayakanth Srinivasan,

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

STACKS AND QUEUES. Problem Solving with Computers-II

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Student Number: Lab day:

Stack and Its Implementation

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks and their Applications

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

Lab 7 1 Due Thu., 6 Apr. 2017

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Stacks Fall 2018 Margaret Reid-Miller

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CSC148H Week 3. Sadia Sharmin. May 24, /20

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

CSC148 Week 2. Larry Zhang

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Lecture Data Structure Stack

Formal Languages and Automata Theory, SS Project (due Week 14)

Walk through previous lectures

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

The Stack and Queue Types

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks Calculator Application. Alexandra Stefan

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

PA3 Design Specification

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Top of the Stack. Stack ADT

Infix to Postfix Conversion

Top of the Stack. Stack ADT

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

[CS302-Data Structures] Homework 2: Stacks

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

CISC-235. At this point we fnally turned our atention to a data structure: the stack

CSE 214 Computer Science II Stack

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Lecture 8: Simple Calculator Application

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

DATA STRUCTURES AND ALGORITHMS

Linear Data Structure

Lists are great, but. Stacks 2

CMPT 225. Lecture 9 Stack

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structures and Algorithms

Postfix (and prefix) notation

CS 211 Programming Practicum Spring 2018

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

BBM 201 DATA STRUCTURES

CS 211. Project 5 Infix Expression Evaluation

ECE Spring 2018 Problem Set #0 Due: 1/30/18

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CS 211 Programming Practicum Fall 2018

Project 1: Implementation of the Stack ADT and Its Application

An Introduction to Trees

CS 211 Programming Practicum Spring 2017

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Lecture 4 Stack and Queue

Data Structures Week #3. Stacks

CDA 3103 Computer Organization Homework #7 Solution Set

Lecture No.04. Data Structures

CS 106B Lecture 5: Stacks and Queues

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Stacks, Queues (cont d)

infix expressions (review)

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

12 Abstract Data Types

1. Stack Implementation Using 1D Array

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Queues Fall 2018 Margaret Reid-Miller

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

CISC

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

BBM 201 DATA STRUCTURES

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

DATA STRUCTURE UNIT I

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Data structure and algorithm in Python

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Foundations of Data Structures

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

10/26/2017 CHAPTER 3 & 4. Stacks & Queues. The Collection Framework

MEMOIZATION, RECURSIVE DATA, AND SETS

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Transcription:

Lecture 12 ADTs and Stacks

Modularity Divide the program into smaller parts Advantages Keeps the complexity managable Isolates errors (parts can be tested independently) Can replace parts easily Eliminates redundancies (each part takes care of own data) Easier to write, Easier to read, Easier to test, easier to modify 2

Task that requires module A function calls Interface for module A Implementation X Implementation Y 3

Separates the purpose from the implementation Procedural abstraction Specification (function header and return type) separate from implementation Can replace implementation if required Data Abstraction Think about what can be done with the data, separate from how it is done Example: Mapping of keys to values Use two parallel lists Use a dictionary 4

A collection of data and a set of operations on that data Specifications of an ADT focus on what the operations are Implementation is not specified ADT is not a data structure Data structure is a construct within a programming language List, tuple, dictionary 5

ADT An interface which is visible to the user of the ADT (the client ) ADT Data structure A data structure used to implement the ADT A wall of ADT operations isolates the data structure and the implementation from the program that uses it. 6

Data [, -3, -2, -1, 0, 1, 2, 3, ] Operations Addition Subtraction Equality Ordering Representation for printing 7

Data An unordered collection of unique elements Operations Add Remove Union Intersection Complement 8

Data collection Position of the elements matters and is stable Two ends to the structure (front and back, first and last) Different structures has different ways to add and remove elements 9

Ordered collection of data Addition of items and removal of items happens at the same end Top of the stack Remove data in reverse order of data added Last in first out (LIFO) Operations Push Pop Peek Is_empty Size 10

Implementation using Python list What is the big-o of push()? What is the big-o of pop()? class StackV1: def init (self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.insert(0,item) def pop(self): return self.items.pop(0) def size(self): return len(self.items) 11

Implementation using Python list What is the big-o of push()? What is the big-o of pop()? class StackV2: def init (self): self.items = [] def is_empty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def size(self): return len(self.items) 12

Many computer languages use braces to signify start and end They need to be matched to be correct They need to be nested correctly Stacks can be used to determine correct use of braces Algorithm: Add each open brace to the stack When a closing brace is encountered, check to see if a matching brace is on the top of the stack When the last token is checked, the stack should be empty 13

Use a stack to check if the braces are used correctly in the following strings. Show the state of the stack after each stack operation. ( ( ) ) ( ( ) ( [ ( ) { } ] [ [ ( ) ] [ { } ] ) 14

To be done after class: Write a program that uses a Stack to check if an input string has correctly matching braces check_braces( (this) [is] {best} ([done] {with} (stacks)) ) 15

Standard mathematics represents expressions using infix notation Operators appear between the operands operand operator 4 + 5 operand Postfix notation puts the operator after the operands No brackets are needed to specify order of precedence 4 5 + 16

Convert the following infix expressions into postfix notation 4 * 5 2 * 8 1 1 4 + 2 * 3 * 5 9 6 / 4 + 2 * (2 + 3) 2 * ((4 + 2) * 3 + 2) 1 17

A stack can be used in the algorithm to convert infix to postfix Divide expression into tokens Operators: +. -, *, / Operands: single digits Other tokens: brackets 18

Create a stack to store operators and a list for the output tokens Scan the tokens from left to right If the token is an operand, add it to the output list If the token is a left parenthesis, push it to the operator stack If the token is a right parenthesis, pop the operator stack until the left parenthesis is removed. Append each operator to the output list If the token is an operator, push it onto the operator stack. But first, remove any operators that have higher or equal precedence and append them to the output list When there are no more tokens, remove operators on the stack and append to the output list 19

Show the operator stack and the output list at every step as the following infix expression is converted to postfix 12 / ( 3 + 4 ) * 2 + 4 20

Create an empty stack Scan the list of tokens from left to right If the token is an operand, push it to the operand stack If the token is an operator, pop the stack twice The first element popped is the right operand The second element popped is the left operand Apply the operator to the operands and push the result onto the stack When there are no more tokens, the stack should contain the result. 21

Following the algorithm to evaluate postfix expressions, show the operand stack, and the token being processed (at each step) as the following postfix expression is evaluated: 7 12 8 9 - * 3 / + 22