+ All Categories
Home > Documents > CSE 326 More Lists, Stacks and Queues

CSE 326 More Lists, Stacks and Queues

Date post: 21-Jan-2016
Category:
Upload: axelle
View: 29 times
Download: 0 times
Share this document with a friend
Description:
CSE 326 More Lists, Stacks and Queues. David Kaplan Dept of Computer Science & Engineering Autumn 2001. 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. Queue ADT. - PowerPoint PPT Presentation
23
CSE 326 More Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001
Transcript
Page 1: CSE 326 More Lists, Stacks and Queues

CSE 326More Lists, Stacks and Queues

David Kaplan

Dept of Computer Science & EngineeringAutumn 2001

Page 2: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

2

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

Page 3: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

3

Queue ADTQueue 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()}

Enqueue

Dequeueabcabc abc

Page 4: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

4

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}

b c d e f

Q0 size - 1

front back

bool is_empty() {

return (front == back)

}

bool is_full() {

return front == (back + 1) % size

}

Note robustness checks!!!

Page 5: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

5

Circular Queue Exampleenqueue Renqueue Odequeueenqueue Tenqueue Aenqueue Tdequeue dequeueenqueue Edequeue

Page 6: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

6

Linked List Q Data Structure

b c d e f

front back

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 }

Page 7: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

7

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?

Page 8: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

8

Queue Application:

Web ServicesWeb 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

requestsListener

Responderresponses

Request Q

Response QWorker

Page 9: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

9

Stack ADTStack 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

BCDEF

E D C B A

F

Push Pop

Page 10: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

10

Array Stack Data StructureS

0 size - 1

f e d c b

void push(Object x) {assert(!is_full())S[back] = xback++

}

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 }

Page 11: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

11

Linked List Stack Data Structure

b c d e f

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 }

Page 12: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

12

f2f3stack frame

f1f2stack frame

Stack Application:

Function CallsMost 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

f1f2 params

f2 registers

f2 return addr

f2f3 params

Page 13: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

13

Applications of Linked ListsPretty 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

Page 14: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

14

Applications of

Multiple-List Data StructuresHigher 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)

Page 15: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

15

Implementations of

Multiple-List Data Structures Array of linked lists Linked list of linked lists Cross-List

Page 16: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

16

Cross-ListCross-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

Page 17: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

17

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

SLNvoid PrintSchedule(int UWID)

print a list of all the courses this student is takingvoid PrintCourseList(int SLN)

print a list of all the students in this course

Page 18: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

18

Array-of-Lists Application:

Adjacency List for Graphs

4 3 5

1 4

1 3

4

2

5

G12345

5 3

5 2•Array G of unordered linked lists•Each list entry corresponds to an edge in the graph

Page 19: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

19

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 falses.

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 }}

Page 20: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

20

1 3

4

2

5

4 3 5

1 4

G

1

2

3

4

5

5 3

5 2

M

Reach

Page 21: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

21

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

Page 22: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

22

Linked Lists of Linked Lists:

Multivariate Polynomials15 + 20y + xy10 + xy5 + 3x12 + 4x12y10 =(15 + 20y)x0 + (y5 + y10)x1 + (3 + 4y10)x12

0

15

1 12

x0 x1 x12

exponentcoefficient

0y0

201y1

15y5

110y10

?

Page 23: CSE 326 More Lists, Stacks and Queues

Linear ADTsCSE 326 Autumn 2001

23

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


Recommended