Lecture16ADTQueue

Lecture16ADTQueue - //changed first to head head.next =...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Abstract Data Type : Queue
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Queue A queue is a collection of items with a “first in first out” (FIFO) retrieval.
Background image of page 2
Queue A queue is a data structure that adds items to the end of the list. The end is referred to as the tail . This operation is often called enqueue . A queue removes items from the head of the queue. This operation is dequeue .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
No Queue class in java library! but you could use the LinkedList class : public class Queue { private LinkedList list; public Queue() { list = new LinkedList(); }
Background image of page 4
public void enqueue (Object x) { list.addLast(x); } public Object dequeue() { return list.removeFirst(); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Write enqueue public void enqueue (Object obj ) { If (head == null) { head = new Node (obj); //changed first to head head.next = null; return; } Node position = head; while (position.next != null) { position = position.next; } position.next = new Node (obj); position.next .next = null; // does this work? return; }
Background image of page 6
enqueue public void enqueue (Object obj ) { If (head == null) { head = new Node (obj);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: //changed first to head head.next = null; tail = head; return; } tail.next = new Node (obj); tail.next .next = null; // does this work? return; } Write enqueue public void enqueue (Object obj ) { If (head == null) { head = new Node (obj); //changed first to head head.next = null; tail = head; return; } tail.next = new Node (obj); tail = tail.next; tail.next = null; return; } How would removeFirst() change? public Object removeFirst( ) { If (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; } dequeue() public Object dequeue( ) { If (head == null) throw new NoSuchElementException(); Object obj = head.data; head = head.next; if (head == null) {tail = null;} return obj; } Array based Queue Constructor private Object list; public Queue () { list = new Object[100]; } enqueue( ) public void enqueue ( Object obj) { } dequeue ( ) public Object dequeue ( ) { return _____; }...
View Full Document

Page1 / 14

Lecture16ADTQueue - //changed first to head head.next =...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online