csc 212 stacks & queues. announcement daily quizzes accepted electronically only submit via one...

24
CSC 212 Stacks & Queues

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

CSC 212

Stacks & Queues

Page 2: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Announcement

Daily quizzes accepted electronically onlySubmit via one or other DropboxCannot force you to compile & test themCan make it much less workPlease, please, please use this as an excuse

to practice your Java Next homework assignment is on web

Page 3: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Working Together

Goal is for students to learn Means mistakes are made, questions raised Peers useful when learning material

But the goal is for YOU to learn material

Talk about homework, daily quizzes all you want Leave conversation with only memories, nothing

written, typed, dictated, displayed on a screen… Wait at least 15 minutes before continuing with

homework

Page 4: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Queues

Elements may be inserted at any timeOnly element which has been in queue the

longest may be removed Items enqueued into rear of queue Items dequeued from front of queue

cashier

Front of lineDequeue here

Back of lineEnqueue here

Page 5: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Queue vs. Stack

Stack follows LIFO policyLast In-First OutObjects removed in reverse order of addition

Queue follows FIFO policyFirst In-First OutObjects removed in order they were added

Page 6: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Queue ADT

Queue supports two key methods:enqueue(obj):

Insert obj at the rear of the queuedequeue():

Returns and removes the object from front of queue; sends error when queue is empty

Page 7: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Queue ADT

Queue also defines other methods:size():

Number of objects in the queue isEmpty():

Returns if the queue is empty. front():

Return but do not remove object from front of queue; send error when queue is empty

Page 8: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Array-based Queue

Queue uses array in circular manner Also specifies maximum size of queue The queue consists of:

N-element array, q f, index of the front elementr, index of the rear element

Page 9: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Array-based Queue Pictorial

Page 10: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Array-Based Queue Pseudocode

int size(): return (N - f + r) mod N

boolean isEmpty(): return(f == r)

Object front():

if isEmpty() then

throw QueueEmptyExceptionreturn q [f ]

Page 11: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Array-Based Queue Pseudocode

Object dequeue():

if isEmpty() then throw QueueEmptyExceptiontemp q[f]q[f] null

f (f +1) mod Nreturn temp

Page 12: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Array-Based Queue Pseudocode

Object enqueue(obj):

if size() = N - 1 then

throw QueueFullException

q[r] objr (r + 1) mod N

Page 13: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Daily Quiz #1

Show the output (if any) and what q, f, & r would equal after each of the following calls (assume N = 5):

enqueue(5)

dequeue()

enqueue(3)

enqueue(4)

enqueue(7)

isEmpty()

front()

dequeue()

enqueue(1)

enqueue(2)

size()

enqueue(9)

Page 14: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Linked Lists

Linked lists are dynamically allocated collection of nodes

Pseudocode of Node class:public class Node {Object element;Node next;// constructors, get & set methods}

next

elem node

Page 15: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Adding to Linked Lists

Maintain a “head” field/variable To insert new node into linked list

Create & initialize new nodenewNode new Node(elem)

Set new node’s next fieldto current value of headnewNode.setNext(head)

Assign head to the new nodehead newNode

Page 16: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Deleting from Linked List

Easy to delete head nodeSave value of headreturnMe head

Move head to next nodehead head.getNext()

Return previous headreturn returnMe

Page 17: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Using Linked Lists

What data structure does a linked list resemble?

What is the maximum size the linked list can hold?

Page 18: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Using Linked Lists

What is complexity of insert and remove?

How would you design isEmpty()?

What is its complexity?

Page 19: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Using Linked List

algorithm size

Node trav headint count = 0while (trav != null)

count++;trav trav.getNext()

endwhilereturn count

What is this complexity?

Page 20: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Linked List and Queues

What is needed to implement a queue with a linked list?

How would we write this algorithm?

Page 21: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

DLNode

public class DLNode {

/** prev links to the previous DLNode in the linked list */

protected DLNode prev;

/** next links to the next DLNode in the linked list */

protected DLNode next;

/** element is the data stored at this node */

protected Object element;

// Not shown here: constructors, get & set // routines

}

Page 22: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Deque

Pronounced “deck”Avoids mistaking it for dequeue methodStands for “double ended queue”

Enables inserting and removing items at both front and rear

Uses doubly-linked list So uses DLNode rather than Node

Page 23: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Deque ADT

Defines the following methods: insertFirst(Object e), insertLast(Object e)Object removeFirst(), Object removeLast()Object first(), Object last() int size()boolean isEmpty()

What exceptions should be thrown?

Page 24: CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test

Daily Quiz #2

Use the Queue interface, & Node, EmptyQueueException classes found on the quiz web page

Implement a linked-list based queueYou do not need to do any javadoc

documentationBut you can (it is worth 30% of programming

assignment grades)!