CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
Fundamentals of Python: From First Programs Through Data Structures
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
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.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
Chapter 12: Data Structures
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.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 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.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
TCSS 342, Winter 2005 Lecture Notes
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
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.”
Objectives of these slides:
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
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.
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.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Stacks and Queues Introduction to Computing Science and Programming I.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
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’
Data structures Abstract data types Java classes for Data structures and ADTs.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
CSCI 62 Data Structures Dr. Joshua Stough September 2, 2008.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Linear Data Structures
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
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.
Prefix notation in action
Objectives In this lesson, you will learn to: Define stacks
Stack and Queue APURBO DATTA.
Pointers and Linked Lists
Stacks and 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
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Lecture 9: Stack and Queue
DATA STRUCTURES IN PYTHON
Presentation transcript:

CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008

Today Linear Structures –Grow and Shrink in predetermined manner. –Stacks – Last In – First Out –Queues – First In – First Out es/Documentation.htmlhttp:// es/Documentation.html

Linear Interface

AbstractLinear

Stacks Like a stack of paper (or cafeteria trays) –“last-in first-out” (LIFO) can only add to the top - push can only remove from the top - pop Why? –Often used to keep track of execution in a program when returning from a method call, the computer has to remember where it last was

AbstractStack push() pop() peek() That’s it! –All the rest is complication that can be abstracted away.

Stack top Node Only need access to the top of the stack Everything O(1) How about with Vector? May be implemented as a linked list push – addFirst() pop – removeFirst() peek – getFirst()

Stacks Interesting idea –Vector O(1) add time, sometimes O(1) sometimes O(n) If that lack of reliability is offputing, then need list implementation

Stacks Example 10 public shuffle() { 11 int ind1 = nextInt(NUM_CARDS); } 1 public static void main (String[] args) { 2 deck = new Deck(); 3 deck.shuffle(); 4 System.out.println (deck); } 20 public int nextInt (int num) { 21 return 0; } Call Stack top

Stacks used to simulate recursion CallFrame CallStack to organize the CallFrames CallFrame contains –Local variables –Method parameters –Program counter – declaring location within routine

Recursive QuickSort

Iterative QuickSort CallFrame

Iterative QuickSort

Stack Question Suppose you wish to fill a stack with a copy of another, maintaining the order of elements. Using only Stack operations, describe how this would be done. How many additional stacks are necessary? public static void copy(Stack s, Stack t)

Stack copy answer

Stack Question Suppose you wish to reverse the order of elements of a stack. Using only Stack operations, describe how this would be done. Assuming you place the result in the original stack, how many additional stacks are necessary? public static void reverse(Stack s)

Stack: as List or Vector. What’s required of a Stack? –Add to top, remove from top. As a List –Complexity of push, peek, pop? As a Vector –Complexity of push, peek, pop?

Post-Fix Notation (10) 3 2 – 4 + (5) (21) Quick: Backus-Naur Form E := L | E E O O := + | - | / | * | ^ | % L := some number

Solving Postfix Notation using a Stack The expression to evaluate is either a L or a E. Push all the tokens onto the stack: Take the top: –If L, then we know the value. –If O, Get two more E off the stack. Perform the appropriate operation and return that value.

Queues Standing in line –“first-in first-out” (FIFO) add to the “tail” of the list (back of line) - enqueue remove from the “head” (head of line) - dequeue Why? –often used to keep things in the order that they arrived –processes to be scheduled on the CPU –packets arriving to a router

Queue – as Linked List – using a Linked List Queue head Node Only have access to the head and tail of the queue May be implemented as a linked list -add to tail -remove from head -SinglyLinked with tail would be fine. tail

Queue – as a Vector Add elements to the back –Complexity? Get elements from the front –Complexity? Cost of adding elements?

Queue – as Array Keep ints for head and count Use % to keep track of head upon remove

Stack vs Queues Stacks depth-first, Queues breadth-first.