CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Data Structures and Algorithms (60-254)
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 Balancing Symbols 3. Applications
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 08 / 2008 Instructor: Michael Eckmann.
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Stacks And Queues Chapter 18.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
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.
Infix to postfix conversion
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stacks and Queues.
Infix to Postfix Conversion
The List, Stack, and Queue ADTs
Data Structures and Algorithms for Information Processing
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
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:

CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2008 Today’s Topics Questions? Stacks Queues

Queues and Stacks Let's implement a stack with an array –what will be our instance variables?

Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this how?

Queues and Stacks Applications of stacks –balancing symbols. e.g. curly braces (left { ) and (right })‏ compilers need to figure out if there are the same number of lefts as there are rights. a stack is handy for this –when see a left curly, push it on the stack –when see a right curly try to pop the stack --- there should be a left curly to pop, otherwise it's an error –when get to the end of the source code, if the stack is empty (and we haven't run into the error above) then the curly braces are balanced.

Queues and Stacks Applications of stacks –postfix expressions (reverse Polish notation)‏ infix (what we're used to) is like: 1.07* *8.75 postfix is like: 1.07, 8.75, *, 14.05, +, 1.07, 3.85, *, + infix result requires us to do the multiplies first and then store the intermediate values and then to the 2 adds. postfix operates in the following way: –when see a number, push it –when see an operator, perform it to the two top numbers on the stack and push the result final result in our example is:

Queues and Stacks Applications of stacks –converting from infix (with only +, *, (, ) ) to postfix expressions infix example (assume normal precedence): a + b*c + ( d*e + f ) * g postfix for our example could be: a b c * + d e * f + g * + –let's look at the algorithm to do this using a stack

Queues and Stacks converting infix to postfix –when an operand (a, b, c, etc.) is read write it to the output –if we see a ) then we pop the stack and write to output until a left paren is on top of the stack --- at which point we pop the left paren but do not output it. –when a + or * is read, examine the top of the stack – if top is of lower precedence than the one just read, then push the operator –when a + or * is read – if top is of the stack does not have lower precedence than the one just read, then pop the operator and write to output –when a ( is read push it –Let's try this algorithm with our example: infix: a + b*c + ( d*e + f ) * g postfix (expected output): a b c * + d e * f + g * +

Queues and Stacks converting infix to postfix –what changes will need to be made if we add in – and / –what changes will need to be made if we add in exponentiation?

Queues and Stacks converting infix to postfix –what changes will need to be made if we add in – and / make – and + be of equal precedence make / and * be of equal precedence –what changes will need to be made if we add in exponentiation? 2^2 = 4 2^3 = 8 2^8 = 256 what about this: 2^2^3 = 2^8 = 256 this means that exponentiation is right-to-left associative

Queues and Stacks Let's implement a queue with an array –what will be our instance variables?

Queues and Stacks Let's implement a queue with an array –what will be our instance variables? –if we want to keep a fixed sized array and we don't want to keep shifting the values around in the array we can use a scheme that stores an index to the front, an index to the rear –if we want the front to be the index that we would dequeue from and rear to be the index of the last element (so that we would enqueue to rear+1) then we should do the following: –front and rear being the same value means we have 1 element in the queue, so for an empty queue we would want front and rear start off at 0 and -1 –when enqueue first one, front stays 0 and rear becomes 0 –also, when rear goes off the end of the array we can wrap it around to 0, only if front is not still 0. Make sense?

Queues and Stacks Let's implement a queue with an array –how would we tell if the queue is full? when front is 0 and rear is maxindex or when...

Queues and Stacks Let's use that Queue class in an interesting sorting method called Radix Sort.

Queues and Stacks Radix Sort. –consider the job of sorting (base 10) integers let's limit them to 0-99 for now (all >=0 and <= 100)‏ –we handled this in several ways already –a totally different way is the following way start with an unsorted list separate the numbers to be sorted into 10 different bins based on their 1's digit then, get the numbers out of the bins and make a new list (take numbers from bin 0, then add on to the end of the list the numbers in bin 1,... and so on, finally adding to the end of the list the numbers from bin 9)‏ then separate this list into 10 bins based on the 10's digit. get the numbers out of the bins like before to get a sorted list.

Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 }

Queues and Stacks Radix Sort. –example on board with the following: { 12, 15, 88, 76, 63, 21, 22, 1, 52, 2, 23, 5 } –each of the 10 bins were queues –also if you want to sort numbers that have a maximum of n digits in them, then you need n passes through the sort and each pass is a higher place in the number –we could use an array of 10 queues each index to the array corresponds to the digit in whatever place we're working on