Queue FIFO (First In First Out) Java Doc peek, element offer poll, add

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
The List ADT Textbook Sections
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
COMP 103 Linked Stack and Linked Queue.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
LECTURE 27: DEQUES CSC 212 – Data Structures. Roses are red and violets are blue Implement push, pop, & top And you’re a Stack too! Stack & ADT Memory.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queue FIFO (First In First Out) Java Doc peek, element poll, remove
Double-Ended Queues Chapter 5.
Marcus Biel, Software Craftsman
Queues Rem Collier Room A1.02
Priority Queue.
Queues, Deques and Priority Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
THURSDAY, OCTOBER 17 IN LAB
Queues, Deques and Priority Queues
Queue.
null, true, and false are also reserved.
Queues FIFO Enqueue Dequeue Peek.
Stacks and Queues 1.
Chapter 4 Queues.
Lecture 16 Stacks and Queues CSE /26/2018.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Data Structures & Programming
Queues, Deques, and Priority Queues
Presentation transcript:

Queue FIFO (First In First Out) Java Doc peek, element offer poll, add capacity offer add poll, remove a b h e f size front rear Java Doc java.util Interface Queue<E> 4/24/2017 IT 179

Queue interface /*********************************************************/ /* API for Queue */ public interface Queue<T> { boolean offer(T item); // return false if failed T remove(); // throw NoSuchElementException if empty T poll(); // return null if empty T peek(); // return null if empty T element();// throw NoSuchElementException if empty int size(); // return the number of element in the queue int capacity(); // return the capacity of the queue // -1 if unlimited } 4/24/2017 IT 179

Queue interface front rear 2 3 5 size=3 capacity=5 public interface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } front 2 3 5 rear 4/24/2017 IT 179

Queue interface rear front 2 3 5 size=3 public interface Queuek<T> { boolean offer(T item); T remove(); T poll(); T peek(); T element(); int size(); int capacity(); } 5 rear 3 2 front 4/24/2017 IT 179

Using arrays to implement queues offer: rear++ poll: front++ offer offer offer poll poll front front front front rear rear rear front front rear rear rear 4/24/2017 IT 179

Using arrays to implement queues offer: rear= rear+1)%capacity poll: front=(front+1)%capacity poll offer poll offer offer offer rear front front front front front front rear rear rear rear rear rear 4/24/2017 IT 179

Using linked lists to implement queues Data structures front rear 1 2 3 4 Boolean offer(T item); T remove(); T poll(); T peek(); T element() int size(); int capacity(); Operations 4/24/2017 IT 179

Queue using single linked lists package myUtil; import java.util.NoSuchElementException; /* This LQueue uses a linked list to implement interface Queue. */ public class LQueue<T> implements Queue<T> { /* This is an inner class for internal nodes */ private class Node<E> { private E data; private Node<E> next; private Node(E data, Node<E> next) { this.data = data; this.next = next; } private Node<T> front, rear; private int size, capacity; /* Default constructor initializes an empty queue. */ public LQueue() { front = rear = null; size=0; capacity=-1; // -1 : no limit /* Set capacity. */ public LQueue(int n) { front = rear = null; size=0; capacity=n; IT 179 4/24/2017

Implementation using linked lists public boolean offer(T item) { if (size == capacity) return false; if (rear == null) front = rear = new Node<T>(item, null); else rear = rear.next = new Node<T>(item, null); size++; return true; } public T peek() { if (front==null) return null; return front.data; } public T element() { if (front==null) throw new NoSuchElementException(); return front.data; } 4/24/2017 IT 179

Implementation using linked lists public T remove() { if (front==null) throw new NoSuchElementException(); T item = front.data; if (front == rear) front = rear = null; else front = front.next; size--; return item; } public T poll() { if (front==null) return null; T item = front.data; if (front == rear) front = rear = null; else front = front.next; size--; return item; } 4/24/2017 IT 179

Queue using single arrays package myUtil; import java.util.NoSuchElementException; /* This AQueue uses a linked list to implement interface Queue. */ public class AQueue<T> implements Queue<T> { private Object[] queue; private int front, rear, size, capacity; public AQueue() { front = rear = size = 0; capacity = -1; // -1: unlimited queue = new Object[10]; } public AQueue(int capacity) { queue = new Object[capacity]; this.capacity = capacity; front = rear = size = 0; IT 179 4/24/2017

Queue using single arrays /** * A private method that doubles the size of the array, queue, * if its size is too small for a new item. */ private void doubleCapacity(){ Object[] newQ = new Object[queue.length*2]; for (int i=0; i<size; i++) { newQ[i]=queue[front]; front = (front+1)%size; } front = 0; rear = size-1; queue = newQ; IT 179 4/24/2017

Queue using single arrays public boolean offer(T item) { if (size==queue.length && capacity==-1) doubleCapacity(); if (size==queue.length) return false; if (size != 0) rear = (rear+1)%queue.length; queue[rear] = item; size++; return true; } public T remove() { if (size==0) throw new NoSuchElementException(); size--; T item =(T) queue[front]; if (size != 0) front = (front+1)%queue.length; return item; IT 179 4/24/2017

Double Ended Queue Deque<E> Java Doc offerFirst addFirst pollLast removeLast peekFirst, getFirst peekLast, getLast capacity a b h e f size offerLast addLast front rear pollFirst, removeFirst Java Doc java.util Interface Deque<E> 4/24/2017 IT 179