Sit-in lab 4.  Given a number of strings containing only “a”, “b”, “c”, “d”  Check each string that it follows the pattern a n b m c m d n where m,

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

Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials Stack and Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
EC-211 DATA STRUCTURES LECTURE 5. 2 Stack Data Structure Stack of Books Stack of Notes Stack of Gifts.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Costas Busch - RPI1 Pushdown Automata PDAs. Costas Busch - RPI2 Pushdown Automaton -- PDA Input String Stack States.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Courtesy Costas Busch - RPI1 Pushdown Automata PDAs.
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.
Stacks.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Fall 2004COMP 3351 Pushdown Automata PDAs. Fall 2004COMP 3352 Pushdown Automaton -- PDA Input String Stack States.
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.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Stacks and Queues Introduction to Computing Science and Programming I.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
FIST, Multi Media University Lecture 5 Stack (Array Implementation) Queue (Array Implementation )
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.
Data Structures & Algorithms
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
CS1020E Lab 4 (Stack and Queue)
Reverse Polish Notation Written by J.J. Shepherd.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
STACK Data Structure
Python: Stacks and Queues (as a Linked List) Damian Gordon.
Click to edit Master text styles Stacks Data Structure.
1 Push Down Automata Lecture 6-7 Ref. Handout p29-33,
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Lecture # 21.
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.
COSC160: Data Structures: Lists and Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
5. Stacks and Queues.
Infix to postfix conversion
PDA’s - A new format for FAs
Pushdown Automata PDAs
Pushdown Automata PDAs
Pushdown Automata PDAs
Pushdown Automata PDAs
CSC 172 DATA STRUCTURES.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
COMPUTER 2430 Object Oriented Programming and Data Structures I
Principles of Computing – UFCFA3-30-1
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Lecture No.07 Data Structures Dr. Sohail Aslam
Patterns to KNOW.
CSCE 531 Compiler Construction
Stacks: Implemented using Linked Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks and Queues.
Python: Stacks and Queues (as an Array)
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presentation transcript:

Sit-in lab 4

 Given a number of strings containing only “a”, “b”, “c”, “d”  Check each string that it follows the pattern a n b m c m d n where m, n >= 1  Use only stack OR queue

 Look at pattern: a n b m c m d n  Notice that it is LIFO  So we use a Stack!  Many ways of solving with a stack… here we show one way

 Process one character from the input String at a time.  For example:  for(int j = 0; j < s.length(); j++){ strCheck.nextChar(s.charAt(j)); }

 If character is “a” ◦ We push to stack if  the stack is empty, or  the top of the stack is “a” ◦ Else is not valid string  If character is “b” ◦ Remember that we have a “b” (set a boolean flag) ◦ We push to stack if  the stack is NOT empty, and  the top of the stack is “a” or “b” ◦ Else is not valid string We need to check this so that things like “bc” will be caught

 If character is “c” ◦ We pop from the stack if  the stack is NOT empty, and  the top of the stack is “b” ◦ Else is not valid string  If character is “d” ◦ We pop from the stack if  the stack is NOT empty, and  the top of the stack is “a”, and  there was a “b” read in (special check for things like “ad”) ◦ Else is not valid string

 Finally, before concluding is valid string, check that stack is empty ◦ We need this to catch things like “aaaaaabbccd” (there will be many “a” left behind in the stack)