1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.

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

Stack & Queues COP 3502.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Stacks.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Introduction and a Review of Basic Concepts
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Stacks and Queues Introduction to Computing Science and Programming I.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
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.
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.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Algorithms and Data Structures Lecture VI
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks Chapter 3 Objectives Upon completion you will be able to
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
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.
Cinda Heeren / Geoffrey Tien
Building Java Programs
i206: Lecture 11: Stacks, Queues
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
i206: Lecture 10: Lists, Stacks, Queues
structures and their relationships." - Linus Torvalds
Data Structures and Algorithms
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks and Queues CSE 373 Data Structures.
Recall What is a Data Structure Very Fundamental Data Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
Dynamic allocation (continued)
CS210- Lecture 3 Jun 6, 2005 Announcements
CSCS-200 Data Structure and Algorithms
structures and their relationships." - Linus Torvalds
Lecture 9: Stack and Queue
Presentation transcript:

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues

2 Lingering Question Why is the expected value of accessing an array of length n going to take time n/2? The expected value is the number of steps you’d expect to take on average. The average number of steps you’ll have to take walking through an array of length n is: –( … + n)/n –This is (n(n+1)/2)/n = (n+1)/2 –This is O(n/2) which is O(n)

3 Lingering Question Why is O(n/2) equivalent to O(n)? Recall the definition of O(g(n)): f(n) is  (g(n)) if there exist positive constants n0 and c such that for all n>=n0, f(n) <= c*g(n) –In the case of n/2 vs. n, there is a constant: 2 –f(n) = n/2 –g(n) = n –f(n) <= 2*g(n) –Thus f(n) is O(g(n))

4 Lingering Question Why is O(n) not equivalent to O( )? Recall the defintion of O(g(n)): f(n) is  (g(n)) if there exist positive constants n0 and c such that for all n>=n0, f(n) <= c*g(n) –In the case of n vs., there is no constant that satisfies the condition for all values of n. –Example: Set c to 100,000 Say n =10,000 Then = 100,000,000 –Thus c is too small. We just can’t pick a large enough c.

5 Definition of Big-Oh A running time is O(g(n)) if there exist constants n 0 > 0 and c > 0 such that for all problem sizes n > n 0, the running time for a problem of size n is at most c(g(n)). In other words, c(g(n)) is an upper bound on the running time for sufficiently large n. c g(n)

6 What is a Data Structure? A systematic way of organizing and accessing data. (Goodrich & Tamassia) An organization of information, usually in memory, for better algorithm efficiency, such as a queue, stack linked list, heap, and tree. It may include redundant information, such as length of the list or number of nodes in a tree. (

7 Stacks

8 Stack Container of objects that are inserted and removed according to the principle of –Last-in-first-out –LIFO Objects can be inserted at any time, but only the most recently inserted can be removed at any time. Operations: –Pop: remove item from stack –Push: enter item onto stack

9 Why Stacks? The Undo facility in software applications –Is LIFO a good model for this? Why or why not? The Back button facility in web browsers –Is LIFO a good model? Why or why not Certain mathematical calculators (operand stack) –Makes it easy to do parenthesized expressions –Example: 10 Enter (pushes 10) 30 Enter(pushes 30) 15 Enter (pushes 15) Plus (pops and adds the most recent pair; then pushes the result onto the top of the stack) Plus(same as above; end up with 55 as only entry)

10 Push notes onto the stack (empty stack) Push do Push re Push mi Push fa Push so Push la Push ti Push do --- Pop (8 times) Halt. -- do re do mi re do fa mi re do so fa mi re do la so fa mi re do ti la so fa mi re do do ti la so fa mi re do Top of stack What do the pops sound like?

11 What do the pops sound like? (Sing the note each time you do a pop operation) Push do Push re Push mi Pop Push fa Push so Pop Halt. do re do mi re do re do do fa do so fa do fa do do -- Top of stack

12 From Goodrich & Tamassia

13 Stack Running Times What is the running time of each operation? Push O(1) Pop O(1) isEmpty() O(1)

14 More Definitions What is an ADT? –Abstract data type –A model of a data structure that specifies The type of data stored The operations supported The types of input and output parameters –Specifies what the program does, but not how it does it What is an API? –Application Programming Interface The names of the methods supported by an ADT How those methods are to be declared and used –e.g., what order the parameters are listed in The API specifies the programming details of the ADT

15 From Goodrich & Tamassia

16 From Goodrich & Tamassia

17 Stack Example Backtracking 8 Queens Example

18 Stacks in the Java VM Each process running in a java program has a java method stack Each time a method is called, its state is pushed onto the method stack –The local variables and necessary object references (pointers) are also stored with the method –All this info together is called a stack frame. This is useful for –Printing error messages (showing the stack trace) –Doing recursive method calls

19 From Goodrich & Tamassia

20 Memory usage in the Java VM Makes memory available for new objects –This is called “the heap” –When the process calls “new”, we allocate the appropriate amount of memory The heap can be implemented as a queue of blocks of memory Program codeJava stack Free Memory Heap Doesn’t grow Stores method call frames Stores objects

21 Queues

22 Queue Container of objects that are inserted and removed according to the principle of –First-in-first-out –FIFO Objects can be inserted at any time, but only the least recently inserted can be removed at any time. Operations: –Enqueue: put item onto queue –Dequeue: remove item from queue

23 Queue Running Times What is the running time of each operation? Enqueue O(1) Dequeue O(1) isEmpty() O(1)

24

25 How are Queues Used? Queues are used extensively in –The OS For scheduling processes to receive resources –Computer networking For keeping storing and sending network packets

26 Use of Queues in the OS Processes waiting in a queue

27 Use of Queues in Distributed Systems Animation by Remzi Arpaci-Dusseau

28 Sequences, Lists, & Vectors There are many ways to implement sequences of items In order of abstractness: –Sequence > List > Vector Know about Vectors in Java –Can be more intuitive to program with –But can be less efficient –Implements the Enumeration interface

29 Java Vector API

30 Java Vector API

31 Vectors in Java How do they differ from arrays? –Can grow the length dynamically –Can insert items into any position –Have a different API (set of method calls) What are the running times of the operations? –boolean isEmpty() O(1) –void copyInto(Object[] anArray) O(n) –Object firstElement() O(1) –boolean contains(Object elem) O(n) –Object elementAt(int index) O(1)