i206: Lecture 11: Stacks, Queues

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
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.
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.”
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
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.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
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’
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.
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.
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.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
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: –
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.
Stack ADT (Abstract Data Type) N …
Stacks II David Lillis School of Computer Science and Informatics
Review Array Array Elements Accessing array elements
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues.
September 29 – Stacks and queues
Queues Rem Collier Room A1.02
Chapter 15 Lists Objectives
Stacks.
Stacks and Queues.
Queues Queues Queues.
Stacks and Queues.
HW-6 Deadline Extended to April 27th
Stacks and Queues.
Building Java Programs
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
i206: Lecture 10: Lists, Stacks, Queues
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Stacks and Queues.
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
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.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Abstract Data Type Abstract Data Type as a design tool
slides created by Marty Stepp
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks.
Stacks and Queues.
Topic 15 Implementing and Using Stacks
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Stacks and Queues.
Lecture 9: Stack and Queue
CMPT 225 Lecture 8 – Queue.
Presentation transcript:

i206: Lecture 11: Stacks, Queues Marti Hearst Spring 2012

Outline What is a data structure Basic building blocks: arrays and linked lists Data structures (uses, methods, performance): List, stack, queue Dictionary Tree Graph

List An ordered collection of objects Stacks and queues are special cases of lists Brookshear Figure 8.1

Stacks

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

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)

Stack Methods push (o) – Insert an item into/onto the stack Input: an object. Output: none. If the stack has a fixed size and the stack cannot accept the push, an stack-overflow exception/error is thrown (or returned) pop() – Returns the most recently inserted object from the stack and removes the object from the stack (an object is removed in last-in-first-out order) Input: none. Output: an object. If the stack is empty, a stack-empty exception/error is thrown (or returned)

Stack Methods Auxiliary/Support Methods size() – Returns the number of objects in the stack Input: none. Output: non-negative integer. isEmpty() (or empty()) – Returns true if there are no objects in the stack Input: none. Output: true or false peek() (or top()) – Returns a reference to (alternatively, a copy of) the most recent item put into the stack Input: none. Output: reference to an object (or an object if a copy)

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

Stack Implementation In Python, the list can be used as a stack: list.append(x) list.pop() Let’s try to implement our own stack as an exercise in understanding what it takes to implement a data structure

Stack Implementation First review a bit about python lists. What do these mean if we have an array a = [1,5,7]: len(a) a[:] a[2:] a[:2] a[0:] a[-1] a[-1:] a[:-1] a[len(a):]

Stack Implementation in Python Brookshear Figure 8.10

More list fun Same effect: a[len(a):] = [item] a.append(item) Different ways to build the same list of numbers: s = [] for i in range(0,9): s.append(i) s = Stack() s.push(i) s = [i for i in range(0,9)]

Queues http://www.lib.uconn.edu/DoddCenter/ASC/Wilcox/Students.htm

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

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

Queue Methods enqueue(item) – Insert the item into the queue. If the queue has a fixed capacity, an exception/error will be returned if attempting to enqueue an item into a filled queue. Input: item. Output: none. dequeue() – Returns a reference to and removes the item that was least recently put into the queue (first-in-first-out) If the queue is empty, an exception/error will be returned if attempting to dequeue. Input: none. Output: item

Queue Methods size() (or getSize()) – Returns the number of items in the queue. Input: none. Output: integer. isEmpty() – Checks if the queue has no items in it. Returns true if the queue is empty. Input: none. Output: true or false. front() – Returns a reference to the “first” item in the queue (the least recent item). If the queue is empty, an exception/error will be returned if attempting to dequeue. Input: none. Output: item.

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

Use of Queues in Distributed Systems Figure by Remzi Arpaci-Dusseau

Queue Implementation In Python, the list can be used as a queue: list.append(x) list.pop(0) Brookshear Figure 8.13