CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CHAPTER 7 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.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 20 / 2008 Instructor: Michael Eckmann.
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 / 29 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 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.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
Arrays and Collections Tonga Institute of Higher Education.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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.
CS 206 Introduction to Computer Science II 04 / 08 / 2009 Instructor: Michael Eckmann.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Chapter 15 Lists Objectives
Stacks and Queues.
Queues Queues Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Stacks and Queues.
ITEC 2620M Introduction to Data Structures
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data Structures and Algorithms for Information Processing
Visit for more Learning Resources
Stacks and Queues.
Lecture 9: Stack and Queue
DATA STRUCTURES IN PYTHON
Presentation transcript:

CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2009 Today’s Topics Questions? Comments? Queue Stack

Queues and Stacks A queue is a data structure that has the following characteristics – It is linear – Uses FIFO (first in, first out) processing Queue operations – Enqueue – add an item to the rear of the queue – Dequeue – remove an item from the front of the queue – Empty – returns true if the queue is empty What's significant about a queue is that there are no insert in anywhere or remove from anywhere in the queue. The only place to add something to the queue is the rear, and the only place to remove something from the queue is the front.

Queues and Stacks A stack is a data structure that has the following characteristics – It is linear – Uses LIFO (last in, first out) processing Stack operations – Push – add an item to the top of the stack – Pop – remove an item from the top of the stack – Empty – returns true if the stack is empty – Peek – retrieve information about the item on top of the stack without removing it The only allowable ways to put an item into and to get an item from the stack is via push and pop. There are no insert in anywhere or remove from anywhere in the stack. What if there was no peek? Is it redundant --- could a series of the existing operations achieve the same functionality.

Queues and Stacks Can anyone think of real world examples that are naturally modeled by queues? Can anyone think of a real world example that is naturally modeled by a stack? Let's see visual representations of a queue and a stack on the board.

Queues and Stacks Can anyone think of real world examples that are naturally modeled by queues? – Line of people at grocery store checkout – Line of airplanes waiting for takeoff Can anyone think of a real world example that is naturally modeled by a stack? – Plates at a salad bar A customer takes the top plate (pop)‏ When new plates come out, they are “pushed” to the top of the stack. – Why is this example not a queue?

Queues and Stacks Method calls A new call to a method causes it's local data and other state information (where in the method does it return to, etc.) to be pushed onto the stack The method calls finish in reverse order, so when a method call ends, it's local data & state is popped off the stack –You've seen me write information about method calls on the board in stack format.

Queues and Stacks Could we implement a Queue with – a linked list – an array Could we implement a Stack with – a linked list – an array

Queues and Stacks Let's implement a stack with an array –what will be our instance variables? 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?

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