CSC 212 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

CSC 212

Description:

Title: CSC 212 Author: Matthew Hertz Last modified by: Matthew Hertz Created Date: 9/21/2005 1:20:44 PM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 25
Provided by: Matthew555
Category:
Tags: csc

less

Transcript and Presenter's Notes

Title: CSC 212


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 QueueEmptyExceptionreturn q f

11
Array-Based Queue Pseudocode
  • Object dequeue()
  • if isEmpty() then throw QueueEmptyExceptiontem
    p ? qfqf ? null
  • f ? (f 1) mod Nreturn temp

12
Array-Based Queue Pseudocode
  • Object enqueue(obj)
  • if size() N - 1 then
  • throw QueueFullException
  • qr ? objr ? (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
node
elem
15
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 nodes next fieldto current value of
    headnewNode.setNext(head)
  • Assign head to the new nodehead ? newNode

16
Deleting from Linked List
  • Easy to delete head node
  • Save value of headreturnMe ? head
  • Move head to next nodehead ? head.getNext()
  • Return previous headreturn 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)!
Write a Comment
User Comments (0)
About PowerShow.com