Download presentation

Presentation is loading. Please wait.

1
CSC 212 Stacks & Queues

2
Announcement Daily quizzes accepted electronically only Submit via one or other Dropbox Cannot force you to compile & test them Can make it much less work Please, please, please use this as an excuse to practice your Java Next homework assignment is on web

3
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

4
Queues Elements may be inserted at any time Only 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 line Dequeue here Back of line Enqueue here

5
Queue vs. Stack Stack follows LIFO policy Last In-First Out Objects removed in reverse order of addition Queue follows FIFO policy First In-First Out Objects removed in order they were added

6
Queue ADT Queue supports two key methods: enqueue(obj): Insert obj at the rear of the queue dequeue(): Returns and removes the object from front of queue; sends error when queue is empty

7
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

8
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 element r, index of the rear element

9
Array-based Queue Pictorial

10
Array-Based Queue Pseudocode int size(): return (N - f + r) mod N boolean isEmpty(): return(f == r) Object front(): if isEmpty() then throw QueueEmptyException return q [f ]

11
Array-Based Queue Pseudocode Object dequeue(): if isEmpty() then throw QueueEmptyException temp q[f] q[f] null f (f +1) mod N return temp

12
Array-Based Queue Pseudocode Object enqueue(obj): if size() = N - 1 then throw QueueFullException q[r] obj r (r + 1) mod N

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

14
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

15
Adding to Linked Lists Maintain a “head” field/variable To insert new node into linked list Create & initialize new node newNode new Node(elem) Set new node’s next field to current value of head newNode.setNext(head) Assign head to the new node head newNode

16
Deleting from Linked List Easy to delete head node Save value of head returnMe head Move head to next node head head.getNext() Return previous head return returnMe

17
Using Linked Lists What data structure does a linked list resemble? What is the maximum size the linked list can hold?

18
Using Linked Lists What is complexity of insert and remove? How would you design isEmpty()? What is its complexity?

19
Using Linked List algorithm size Node trav head int count = 0 while (trav != null) count++; trav trav.getNext() endwhile return count What is this complexity?

20
Linked List and Queues What is needed to implement a queue with a linked list? How would we write this algorithm?

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

22
Deque Pronounced “deck” Avoids mistaking it for dequeue method Stands for “double ended queue” Enables inserting and removing items at both front and rear Uses doubly-linked list So uses DLNode rather than Node

23
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?

24
Daily Quiz #2 Use the Queue interface, & Node, EmptyQueueException classes found on the quiz web page Implement a linked-list based queue You do not need to do any javadoc documentation But you can (it is worth 30% of programming assignment grades)!

Similar presentations

© 2020 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google