CSE 326 More Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Slides:



Advertisements
Similar presentations
Data Structures and Algorithms (60-254)
Advertisements

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.
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.
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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2011W2 2.
Advanced Data Structures
Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2009W1 2.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
CSE 326: Data Structures Lecture #0 Introduction Steve Wolfman Winter Quarter 2000.
CSE 326: Data Structures Lecture #5 Alon Halevy Spring Quarter 2001.
Stacks, Queues, and Deques
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.
Stacks, Queues, and Deques. 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.
CSE 326: Data Structures Lecture #0 Introduction Bart Niswonger Summer Quarter 2001.
Stacks, Queues, and Deques
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.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Come up and say hello! (Welcome!) 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Kendra Cooper 2014W1 2.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Come up and say hello! 1. CPSC 221: Algorithms and Data Structures Lecture #0: Introduction Steve Wolfman 2010W2 2.
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.
CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Stacks And Queues Chapter 18.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: 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.
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 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.
Review Array Array Elements Accessing array elements
Data Structure By Amee Trivedi.
Chapter 15 Lists Objectives
CSE 326: Data Structures Lecture #1 2 Degrees of List
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Stacks and Queues CSE 373 Data Structures.
Spring 2016 Richard Anderson Lecture 1
Stacks and Queues 1.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Queues Jyh-Shing Roger Jang (張智星)
Stacks and Queues CSE 373 Data Structures.
CSE 326: Data Structures Lecture #1 Lists, MultiLists & Trees
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
Presentation transcript:

CSE 326 More Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001

Linear ADTsCSE 326 Autumn Housekeeping  Project 1 posted, due Fri Oct 19  Solo project  Later projects will be done in teams  Quiz 1 postponed to Mon Oct 15  Homework 1 due Fri Oct 12

Linear ADTsCSE 326 Autumn Queue ADT Queue implements flow-through semantics (FIFO)  Queue property: if x enters the queue before y, then x will leave the queue before y Lots of real-world analogies  Grocery store line, call center waiting, bank teller line, etc. class Queue { void enqueue(object o) object dequeue() bool is_empty() bool is_full() } EnqueueDequeue abc abcabc

Linear ADTsCSE 326 Autumn Circular Array Queue void enqueue(Object x) { if (is_full()) return Q[back] = x back = (back + 1) % size } Object dequeue() { if (is_empty()) return x = Q[front] front = (front + 1) % size return x } bcdef Q 0 size - 1 frontback bool is_empty() { return (front == back) } bool is_full() { return front == (back + 1) % size } Note robustness checks!!!

Linear ADTsCSE 326 Autumn Circular Queue Example enqueue R enqueue O dequeue enqueue T enqueue A enqueue T dequeue enqueue E dequeue

Linear ADTsCSE 326 Autumn Linked List Q Data Structure bcdef frontback void enqueue(Object x) { if (is_empty()) front = back = new Node(x) else back->next = new Node(x) back = back->next } Object dequeue() { assert(!is_empty) return_data = front->data temp = front front = front->next delete temp return temp->data } bool is_empty() { return front == null }

Linear ADTsCSE 326 Autumn Circular Array vs. Linked List, Fixed Array vs. Stretchy Array Array vs. Linked List  Ease of implementation?  Generality?  Speed?  Flexibility? Fixed Array vs. Stretchy Array  Ditto?

Linear ADTsCSE 326 Autumn Queue Application: Web Services Web server receives requests over the Internet  Request, Response Queues are core subsystems of web server  Listener process enqueues arriving request onto Request Queue, immediately resumes listening  Worker process, often running on a separate processor, dequeues a request, processes it, enqueues result onto Response Queue  Responder process dequeues requests from Response Queue and sends result over the Internet to requestor The Web requests Listener Responder responses Request Q Response Q Worker

Linear ADTsCSE 326 Autumn Stack ADT Stack implements push-down semantics (LIFO)  Stack property: if x is on the stack before y is pushed, then x will be popped after y is popped Lots of real-world analogies  Coin-holder, stack of plates in greasy-spoon diner, RPN calculator, etc. class Stack { void push(object o) object pop() object top() bool is_empty() bool is_full() } A BCDEFBCDEF E D C B A F PushPop

Linear ADTsCSE 326 Autumn Array Stack Data Structure S 0 size - 1 fedcb void push(Object x) { assert(!is_full()) S[back] = x back++ } Object top() { assert(!is_empty()) return S[back - 1] } back Object pop() { back-- return S[back] } bool is_empty() { return back == 0 } bool is_full() { return back == size }

Linear ADTsCSE 326 Autumn Linked List Stack Data Structure bcdef back void push(Object x) { temp = back back = new Node(x) back->next = temp } Object top() { assert(!is_empty()) return back->data } Object pop() { assert(!is_empty()) return_data = back->data temp = back back = back->next delete temp return return_data } bool is_empty() { return back == null }

Linear ADTsCSE 326 Autumn f2  f3 stack frame f1  f2 stack frame Stack Application: Function Calls Most recursive languages implement function calls with a call stack made up of stack frames Scenario: f1 calls f2, f2 calls f3 Caller  push register contents  push return address  push call parameters Callee  pop call parameters  run local code  pop return address  push return value  return (jump to return address) Caller  pop return value  pop register contents  resume f1 registers f1 return addr f1  f2 params f2 registers f2 return addr f2  f3 params

Linear ADTsCSE 326 Autumn Applications of Linked Lists Pretty much everything!  Class list  Operating systems: list of running programs  Compilers: list of functions in a program, statements in a function  Graphics: list of polygons to be drawn to the screen  Stacks and Queues: supporting structure  Probably the most ubiquitous structure in computer science! Many ADTS such as graphs, relations, sparse matrices, multivariate polynomials use multiple linked lists General principle throughout the course  Use a simpler ADT to implement a more complicated one

Linear ADTsCSE 326 Autumn Applications of Multiple-List Data Structures Higher dimensionality can cause combinatorial explosion  List sparseness can address this problem Hence, many high-d applications use multiple linked lists  graphs (2D)  relations (multi-D)  matrices (2-D, usually)  multivariate polynomials (multi-D)  radix sort (2-D)

Linear ADTsCSE 326 Autumn Implementations of Multiple-List Data Structures  Array of linked lists  Linked list of linked lists  Cross-List

Linear ADTsCSE 326 Autumn Cross-List Cross-List has distinguished node types  Exterior node is an ordinary node  D different types of exterior node  Each exterior node type is a dimension  Interior node contains D next pointers, 1 per dimension Example: enrollments of students in courses  35,000 students; 6,000 courses  ~2 million unique combinations  Array implementation requires 2-million-cell array  Assume each student takes 5 courses  Multi-list implementation requires ~200k nodes; 90% space savings Benefit: efficient storage for sparse lists

Linear ADTsCSE 326 Autumn Cross-List Application: Enrollment ADT  Registry ADT for UW - stores which students are taking which courses  Example operations: int TakingCourse(int UWID, int SLN)  tells if the student is taking the course specified by SLN void PrintSchedule(int UWID)  print a list of all the courses this student is taking void PrintCourseList(int SLN)  print a list of all the students in this course

Linear ADTsCSE 326 Autumn Array-of-Lists Application: Adjacency List for Graphs G Array G of unordered linked lists Each list entry corresponds to an edge in the graph

Linear ADTsCSE 326 Autumn Reachability by Marking  Suppose we want to mark all the nodes in the graph which are reachable from a given node k.  Let G[1..n] be the adjacency list rep. of the graph  Let M[1..n] be the mark array, initially all false s. void mark(int i) { M[i] = true; x = G[i] while (x != NULL) { if (M[x->node] == false) mark(G[x->node]) x = x->next }

Linear ADTsCSE 326 Autumn G M Reach

Linear ADTsCSE 326 Autumn Thoughts on Reachability  The marking algorithm visits each node and each edge at most once. Why?  This marking algorithm uses Depth First Search. DFS uses a stack to track nodes. Where?  Graph reachability is closely related to garbage collection  the nodes are blocks of memory  marking starts at all global and active local variables  the marked blocks are reachable from a variable  unmarked blocks are garbage

Linear ADTsCSE 326 Autumn Linked Lists of Linked Lists: Multivariate Polynomials y + xy 10 + xy 5 + 3x x 12 y 10 = ( y)x 0 + (y 5 + y 10 )x 1 + (3 + 4y 10 )x x0x0 x1x1 x 12 exponent coefficient 0 y0y0 201 y1y1 15 y5y5 110 y 10 ?

Linear ADTsCSE 326 Autumn To do  Try some list/stack/queue problems in Weiss chapter 3 (not on HW1, but will be on Quiz 1)  Start reading Weiss chapter 4 on Trees