Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.

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

Stacks, Queues, and Linked Lists
Building Java Programs Chapter 14
The unorganized person’s data structure
Stacks Chapter 11.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
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 Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Stacks, Queues, and Deques
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks  Introduction  Applications  Implementations  Complex Applications.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks (and Queues).
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.
Building Java Programs
Computing with C# and the .NET Framework
Stacks and Queues.
CSE 373: Data Structures and Algorithms
Cinda Heeren / Geoffrey Tien
Stacks.
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Building Java Programs
Building Java Programs Chapter 14
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues.
Building Java Programs
Building Java Programs
CSE 214 – Computer Science II Stack Applications
Stacks and Queues CLRS, Section 10.1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Marty Stepp
Stacks.
More Data Structures (Part 1)
Stacks, Queues, and Deques
Stacks.
Stacks and Queues.
Presentation transcript:

Stacks and Queues Pepper

Why History Simplicity Operating Systems – Function Call Stack

What is a Queue First In, First Out (FIFO) Standard queue methods – add() – remove() – isEmpty() – peek() – size() Interface in Java – Can create as a LinkedList

What is a Stack Last In, First Out (LIFO) Standard Stack Methods – push() – pop() – isEmpty() – peek() – size()

Creating and Adding Items Create both and add 5 items Stack st = new Stack ( ); Queue qu = new LinkedList (); for (int c = 0; c < 5; c++) { qu.add(Integer.toString(c)); st.push(Integer.toString(c)); }

Iterating through every item Code to just run through until empty: while (!st.isEmpty()){ System.out.println(st.pop()); } while (!qu.isEmpty()){ System.out.println(qu.remove()); } Empties the structure though

Queue: Iterate through all & Recreate Queue: Just control by initial size and keep adding back for (int c = 0; c < qu.size(); c++){ String value = qu.remove(); System.out.println(value); qu.add(value); } Removing values – Move the qu.size() call to before the loop because the size will change.

Stack: Iterate through all & Recreate Stack: Cannot just push back because it will become your next value. Need auxillary structure. Stack temp = new Stack (); while (!st.isEmpty()){ String value = st.pop(); System.out.println(value); temp.push(value); } while (!temp.isEmpty()){ st.push(temp.pop()); }

Read Stack – if you only had a Queue Read through the stack and place on queue – Queue holds reverse order Place queue on stack – still reverse Read through the stack and place on queue – Queue now holds original order Place queue on stack – original order

Compare two stacks using one temp Compare every item of Two stack contents Leave stacks the same when done When one has more items, it cannot be the same When you find a false result, cannot just return false – have to put the items back What order to put items back? How do you get a true result?

Comparing stacks public static boolean isSameStack(Stack st1, Stack st2){ Stack temp = new Stack (); boolean res = true; if (st1.size() != st2.size()) { return false; } else { while (!st1.isEmpty()){ String value1 = st1.pop(); String value2 = st2.pop(); temp.push(value1); temp.push(value2); if (!value1.equals(value2)){ // break and set value to false; res = false; break; } } // now put all the values back: while (!temp.isEmpty()){ st2.push(temp.pop()); st1.push(temp.pop()); } return res; // it will be true if nothing ever set it to false; } }

Splitting Tokens To split an expression like (4 + (5 – 3)) Why not Scanner? Making your own splitter – Holds Queue of characters Next entry waiting Special characters we are searching for – Methods peek hasNext next

Loading the Character Queue public StringSplitter(String str){ ch = new LinkedList (); for (int c = 0; c < str.length(); c++){ ch.add(str.charAt(c)); } findNextToken(); }

How to find the next token Skip whitespace If the queue is empty, set the token to null Pull out one character – If it is a special character, you are done – If it is a number, keep going until you get whitespace or a special character

Finding the Token public String findNextToken(){ this.token = ""; // skip any whitespace while (!ch.isEmpty() && Character.isWhitespace(ch.peek())){ ch.remove(); // just throw away the whitespace } // now we are past the whitespace. Continue until end or special char //if the next one is empty there is no token if (ch.isEmpty()) { token = null; } else // something in token {

Finding the token part 2 token = "" + ch.remove(); //was it a special character or number if (!SPECIAL_CHAR.contains(token)){ // keep going until end of number skipping white space, // ending at number's end boolean done = false; while (!ch.isEmpty() && !done){ char nextone = ch.peek(); if (Character.isWhitespace(nextone) || SPECIAL_CHAR.contains("" + nextone)) { done = true;} else { token = token + ch.remove(); } } } } return token; }

Next The token is already waiting for you, so give it and determine the next token. public String next(){ if (token == null) { throw new NoSuchElementException(); } String nextToken = token; findNextToken(); // reset the token return nextToken; }

Simple test public class StringSplitterTester { public static void main (String[] args) { StringSplitter ss = new StringSplitter( "18.4-((2.3*8.5 ) / ( (2.7 ^ 4.9 ))))"); while(ss.hasNext()){ System.out.println(ss.next()); }

Evaluating the Expression Dijkstra's method Push all symbols on one stack All numbers on the other stack When you reach a ), evaluate 2 numbers and their one operation Push that result back onto the stack

Expression – Create the ADTs Stack symbols = new Stack (); Stack numbers = new Stack (); StringSplitter ss = new StringSplitter(expression); boolean bad = false;

Evaluate Expression - body while(ss.hasNext()){ String token = ss.next(); if (token.equals(")")) { // process the expression } else if (StringSplitter.SPECIAL_CHAR.contains(token)) { symbols.push(token); } else { numbers.push(Integer.parseInt(token)); } } if (bad) { System.out.println("The answer is - no answer"); } else { System.out.println("The answer is " + numbers.pop()); } } }

Calculating 2 operands String operation = symbols.pop(); Integer firstNum = numbers.pop(); Integer nextNum = numbers.pop(); String openparen = symbols.pop(); if (openparen.equals("(")) { if (operation.equals("+")){ numbers.push(firstNum + nextNum); } else if (operation.equals("-")) { numbers.push( nextNum - firstNum); } else { bad = true; } } else { bad = true; }

HTML Splitter Split by instead of (*,etc Toss anything not inside Need to identify the starter and ender characters: – < – >

HTML Processor Read through the queue pushing onto the stack whenever a token must be matched. – When you encounter a / at the end, it means it is a self closing tag, so don't push it onto the stack When you encounter a / at the beginning, it is supposed to close something that is right before it. – Pop the last item, it should have matching first words of the tag.

Stacks and Queues What Why Patterns: – Read and restore Queues just keep moving onto itself Stacks require an auxillary storage – Compare