Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
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 12 Abstract Data Type.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Topic 15 Implementing and Using Stacks
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
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,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Topic 3 The Stack ADT.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: 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.
Stack Applications.
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.
Stacks and Queues Introduction to Computing Science and Programming I.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Computer Science Department Data Structure & Algorithms Problem Solving with 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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
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.
CSC 231 Stacks 1 Devon M. Simmonds University of North Carolina, Wilmington TIME: Tuesday/Thursday 11:11:50am in 1012 & Thursday 3:30-5:10pm in Office.
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.
Click to edit Master text styles Stacks Data Structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
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 5.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Objectives In this lesson, you will learn to: Define stacks
COMPSCI 107 Computer Science Fundamentals
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

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 2COMPSCI Computer Science Fundamentals

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

 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 4COMPSCI Computer Science Fundamentals

 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 5COMPSCI Computer Science Fundamentals

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

 Data  […, -3, -2, -1, 0, 1, 2, 3, …]  Operations  Addition  Subtraction  …  Equality  Ordering  Representation for printing 7COMPSCI Computer Science Fundamentals

 Data  An unordered collection of unique elements  Operations  Add  Remove  Union  Intersection  Complement 8COMPSCI Computer Science Fundamentals

 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 9COMPSCI Computer Science Fundamentals

 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 10COMPSCI Computer Science Fundamentals

 Implementation using Python list  What is the big-O of push()?  What is the big-O of pop()? 11COMPSCI Computer Science Fundamentals 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)

 Implementation using Python list  What is the big-O of push()?  What is the big-O of pop()? 12COMPSCI Computer Science Fundamentals 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)

 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 13COMPSCI Computer Science Fundamentals

 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.  ( ( ) )  ( ( ) (  [ ( ) { } ]  [ [ ( ) ] [ { } ] ) 14COMPSCI Computer Science Fundamentals

 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))’) 15COMPSCI Computer Science Fundamentals

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

 Convert the following infix expressions into postfix notation  4 * 5 – 2 * 8 – 1  1 – * 3 * 5  9 – 6 / * (2 + 3)  2 * ((4 + 2) * 3 + 2) – 1 17COMPSCI Computer Science Fundamentals

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

 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 19COMPSCI Computer Science Fundamentals

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

 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. 21COMPSCI Computer Science Fundamentals

22COMPSCI Computer Science Fundamentals  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: * 3 / +