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

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Data Structures ADT List
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
Stacks, Queues, and Deques
Data Structures - Queues
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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++
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.
Stacks And Queues Chapter 18.
18-1 Queues Data Structures and Design with Java and JUnit © Rick Mercer.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Sections 3.4 Formal Specification
UNIT II Queue.
Queue.
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Queues.
Linked Lists: Implementation of Queue & Deque
COMPUTER 2430 Object Oriented Programming and Data Structures I
Revised based on textbook author’s notes.
Queues: Implemented using Arrays
ADT list.
Stacks: Implemented using Linked Lists
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
ADT Queue (Array Implementation)
Circular Queues: Implemented using Arrays
Python: Stacks and Queues (as an Array)
Queues: Implemented using Linked Lists
Stacks, Queues, and Deques
Getting queues right … finally (?)
Presentation transcript:

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

Properties of queues First In First Out (FIFO) Data added at one end only (the Tail of the queue) Data removed at other end only (the Head of the queue)

Queue operations Initialise queue Add item (to tail of queue) Remove item (from head of queue) Check if queue empty Check if queue full

Queues using arrays Use array to store queue elements Define a data item Head which identifies the item at Head of queue Define a data item Tail which identifies the first empty location after last item in queue Tail identifies location where next item is placed in queue

Queues using arrays (first try) Head Tail Empty queue (Tail == Head)

Queues using arrays (first try) Head Tail Add item T at location Tail T

Queues using arrays (first try) Head Tail Add item H at location Tail TH

Queues using arrays (first try) Head Tail Add item I at location Tail THI

Queues using arrays (first try) Head Tail Remove item T from Head HI

Queues using arrays (first try) Head Tail Continue until Tail == ArraySize Queue full? AQUEUE

Queues using arrays (first try) Head Tail Must shift queue contents back to start of array - inefficient! AQUEUE

Circular Queue Use a circular queue Consider (perceive?) the array as a circular structure (i.e. as if the last element of the array is connected/joined to the first element of the array) The benefit of this insight is that we never have to shift data

A circular array MaxSize - 1 MaxSize - 2 MaxSize - 3

A queue using a circular array MaxSize - 1 Tail Head Empty queue Tail == Head 0

A queue using a circular array T MaxSize - 1 Tail Head Add T at Tail Tail = (Tail + 1) % MaxSize 0

A queue using a circular array T H MaxSize - 1 Tail Head Add H at Tail Tail = (Tail +1) % MaxSize 0

A queue using a circular array T H MaxSize - 1 Tail Head Add I at Tail Tail = (Tail +1) % MaxSize 0 I

A queue using a circular array H MaxSize - 1 Tail Head Remove T from Head Head = (Head +1) % MaxSize 0 I

A queue using a circular array MaxSize - 1 Tail Head Continue until Tail == MaxSize E U E U Q A

A queue using a circular array MaxSize - 1 Tail Head 0 E U E U Q A Add Z at Tail Tail = (Tail +1) % MaxSize i.e. [(MaxSize - 1) + 1] % MaxSize = MaxSize % MaxSize = 0 Z

Empty and Full Queue Tests Empty queue condition: Head = = Tail Full queue condition: (Tail + 1) % MaxSize = = Head

Queue ADT in Java Constructor isempty isfull Join Leave

Constructor public QueueOfInts() {Queue = new int[10] ; Head = Tail = 0 ; } public QueueOfInts(int capacity) {Queue = new int[capacity] ; Head = Tail = 0 ; }

Test for empty queue public boolean isempty() { return Head == Tail) } }

Test for full queue public boolean isfull() { if ((Tail + 1) % Queue.length == Head) { return true; } else { return false; }

Adding an element public void Join(int val) {Queue[Tail] = val ; Tail = (Tail + 1) % Queue.length; }

Remove an element public int Leave() {int Removed ; Removed = Queue[Head]; Head = (Head + 1) % Queue.length ; return Removed ; }