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

Post on 21-Dec-2015

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CSC 212

Stacks & Queues

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

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

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

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

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

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

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

Array-based Queue Pictorial

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 ]

Array-Based Queue Pseudocode

Object dequeue():

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

f (f +1) mod Nreturn temp

Array-Based Queue Pseudocode

Object enqueue(obj):

if size() = N - 1 then

throw QueueFullException

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

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)

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

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

Deleting from Linked List

Easy to delete head nodeSave value of headreturnMe head

Move head to next nodehead head.getNext()

Return previous headreturn returnMe

Using Linked Lists

What data structure does a linked list resemble?

What is the maximum size the linked list can hold?

Using Linked Lists

What is complexity of insert and remove?

How would you design isEmpty()?

What is its complexity?

Using Linked List

algorithm size

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

count++;trav trav.getNext()

endwhilereturn count

What is this complexity?

Linked List and Queues

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

How would we write this algorithm?

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

}

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

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?

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)!

top related