Queues.

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

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
Data Structures ADT List
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
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.
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.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
Queues.
Stacks, Queues & Deques CSC212.
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.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
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.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Review Array Array Elements Accessing array elements
UNIT II Queue.
CC 215 Data Structures Queue ADT
Queues Rem Collier Room A1.02
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Priority Queue.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues.
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures ADT List
Data Structures ADT List
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
ADT list.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
Copyright © Aiman Hanna All rights reserved
ADT Queue (Array Implementation)
CE 221 Data Structures and Algorithms
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
CSC 248 – fundamentals of Data structure
Queues: Implemented using Linked Lists
Data Structures & Programming
Presentation transcript:

Queues

Queues A queue is restricted list in which items are inserted from rear end and deleted from front end. Example

Queues A queue is a linear list structure where elements can be inserted at any time, but only the elements that have been in the queue the longest that can be remove. The queue’s storage policy or principle is First-In, First-Out (FIFO). This means that the first element inserted will be the first to be removed in a sequence.

Application Of Queues Just like stack, queue will frequently be used by the programmer to model real-world situations . Some of the uses of Queue are listed below: When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. Print Server. When placed on hold for telephone operators. For example, when you phone the toll-free number for your bank, you may get a recording that says, "Thank you for calling A-1 Bank. Your call will be answered by the next available operator. Please wait." This is a queuing system. 11/17/2018

Queue Operations Queue operations are: Enqueue(e) -> Insert element e at the rear of the queue. Dequeue -> Remove and return from the queue the object at the front length() -> Return the number of elements in the queue Isempty() -> Return a Boolean value that indicates whether queue is empty. Isfull() -> Return a Boolean value that indicates whether queue is full.

Implementation of Queues Queue can be implemented in two ways Arrays Linked Lists

Using Arrays First the size has to be defined and fixed. For example: take size of queue is 6. Size of the Queue is 6 Enqueue(20) Enqueue(5) Front Front 20 20 5 Rear Rear Front = 0 Rear = 0 Front = 0 Rear = 1

Using Arrays Enqueue(10) Enqueue(23) Front Front length()=3 Elements isfull() = False isempty() = False front() = 20 Rear() = 10 20 5 10 20 5 10 23 Rear Rear Front = 0 Rear = 2 Front = 0 Rear = 3 Enqueue(11) Enqueue(14) Front Front length()=6 Elements isfull() = True isempty() = False front() = 20 Rear() = 14 20 5 10 23 11 20 5 10 23 11 14 Rear Front = 0 Rear = 5 Rear Front = 0 Rear = 4

Using Arrays Enqueue(45) length () = 6 Elements isfull() = True isempty() = False front() = 20 Rear() = 14 This state is called queue is full Front 20 5 10 23 11 14 Front = 0 Rear = 5 Rear Dequeue() Dequeue() Dequeue() Front Front Front 5 10 23 11 14 10 23 11 14 23 11 14 Front = 1 Rear = 5 Rear Front = 2 Rear = 5 Rear Front = 3 Rear = 5 Rear

Using Arrays Dequeue() Front length () = 2 Elements isfull() = False isempty() = False front() = 11 Rear() = 14 11 14 Front = 4 Rear = 5 Rear Enqueue(75) To Insert Element 75, move all elements to first position. Such queue is called shifting queue. Front Front 11 14 11 14 75 Rear Rear Front = 4 Rear = 5 Front = 4 Rear = 5 Front = 0 Rear = 2

Using Arrays Dequeue() Dequeue() Front Front 14 75 75 Rear Rear

ADT Queue (Array Implementation) Array implementation of the queue…a fixed size array is used to store the data elements. As data elements are enqueued & served the queue crawls through the array from low to high index values. As the queue crawls forward, it also expands and contracts.

ADT Queue (Array Implementation) Head Tail After one En-queue and one Serve Head Tail

ADT Queue (Array Implementation) Head Tail Where to En-queue this?

ADT Queue (Array Implementaion) public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

ADT Queue (Array Implementation) public boolean full () { return size == maxsize ? true : false; } public int length () { return size; public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1); size++;

ADT Queue (Array Implementation) public T serve () { T e = nodes[head]; head = (head + 1); size--; return e; }

Queues(linked list implementation) Front Tail

ADT Queue: Specification Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head. Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue

ADT Queue: Specification Operations: Method Enqueue (Type e) requires: Queue Q is not full. input: Type e. results: Element e is added to the queue at its tail. output: none. Method Serve (Type e) requires: Queue Q is not empty. input: results: the element at the head of Q is removed and its value assigned to e. output: Type e. Method Length (int length) input: results: The number of element in the Queue Q is returned. output: length.

ADT Queue: Specification Operations: Method Full (boolean flag). requires: input: results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.

ADT Queue (Linked Implementation) public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }

ADT Queue (Linked Implementation) public boolean full() { return false; } public int length (){ return size;

ADT Queue (Linked Implementation) public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; size++;

ADT Queue (Linked Implementation) public Type serve() { Type x; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }

Series of Queue Operations Output Queue enqueue(5) ----- 5 enqueue(3) ----- 5, 3 enqueue(-7) ----- 5, 3, -7 dequeue() 5 3, -7 enqueue(8) ----- 3, -7, 8 enqueue(-10) ----- 3, -7, 8, -10 dequeue() 3 -7, 8, -10 Size() 3 -7, 8, 10 dequeue() -7 8, 10 front() 8 8, 10

Queues- Evaluation Efficiency Same as stack: enqueue: only at the back ? dequeue: only at the front ? Access: only at the front ? So, items can be inserted and removed from a queue in O(1) time.