Queue.

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

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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
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.
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.
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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Queues Objectives: Describe a queue
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Topic 3 Basic Data Structures continued CSE1303 Part A Data Structures and Algorithms.
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 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 7 Queues Introduction Queue applications Implementations.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
CHAPTER 5 QUEUE v2 by Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
 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.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
CSCE 210 Data Structures and Algorithms
Data Abstraction & Problem Solving with C++
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Queues.
CS505 Data Structures and Algorithms
UNIT II Queue.
COSC160: Data Structures: Lists and Queues
CC 215 Data Structures Queue ADT
CENG 213 Data Structure Queue 7/2/2018.
Stacks and Queues.
Basic Data Structures – Continued (Queues)
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
Queues.
Queues.
Queues.
DATA STRUCTURE QUEUE.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
CSC 143 Queues [Chapter 7].
Queues: Implemented using Arrays
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Circular Queues: Implemented using Arrays
Stack.
Queues.
General List.
CSCS-200 Data Structure and Algorithms
Priority Queue and Heap
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Data Structures & Programming
Presentation transcript:

Queue

What is Queue? Definition A collection of elements that are inserted and removed according to the first-in first-out (FIFO) principle. The first element added to the queue will be the first one to be removed. All insertions are made at one end, called rear. All deletions are made at the other end, called front. rear front dequeue enqueue

What is Queue? Terminology Front : The front of queue, where deletions take place or the position of the first item Rear : The rear of queue, where insertions take place or the next of the last item EnQueue: Insert an item at the rear. DeQueue: Delete the item at the front. 7 Empty DeQueue EnQueue 2 2 EnQueue 5 7 5 EnQueue 7 2 7 DeQueue 5

What is Queue? Operations InitQueue: Make queue empty. IsFull: Check whether queue is full. IsEmpty: Check whether queue is empty. Peek : Read the item at the front. EnQueue: Insert an item at the rear. DeQueue: Remove an item at the front. 2 7 rear front Q: Can we access items other than at the front? A: By definition, No

Linear Queue It performs deletion at one end of the list and the insertion at the other end. Empty DeQueue 5 front,rear front rear EnQueue 2 2 5 8 EnQueue 8 front rear front rear EnQueue 7 2 7 EnQueue 3 5 8 3 front rear front rear 7 DeQueue Problem: We cannot add any more even though we have empty spaces front rear 7 5 EnQueue 5 front rear

Circular Queue The last position is connected back to the first position to make a circle. rear [2] [3] [2] [3] [2] [3] Empty EnQueue 2 EnQueue 7 [1] [4] [1] [4] [1] 7 [4] rear 2 2 [0] [5] [0] [5] [0] [5] front front front,rear rear rear front rear [2] [3] [2] [3] [2] [3] EnQueue 5 DeQueue DeQueue 5 5 5 [1] 7 [4] [1] 7 [4] [1] [4] front 2 [0] [5] [0] [5] [0] [5] front

Circular Queue How to distinguish Empty and Full? Full: check whether front is 0 and rear is MAX_SIZE – 1. [1] [2] [3] [4] [0] [5] Empty front = 0 rear = 0 front,rear [1] [2] [3] [4] [0] [5] 2 7 5 4 3 8 Full front = 0 rear = 0 front,rear [1] [2] [3] [4] [0] [5] 2 7 5 4 3 rear front Full front = 0 rear = 5

Array-based Implementation Queue representation 0 1 2 3 4 … MAX_QUEUE–1 rear items[ ] front #define MAX_QUEUE 100 typedef enum { false, true } bool; typedef int Data; typedef struct { int front, rear; Data items[MAX_QUEUE]; } Queue;

Array-based Implementation Operations // Make a queue empty. void InitQueue(Queue *pqueue); // Check whether a queue is full. bool IsFull(Queue *pqueue); // Check whether a queue is empty. bool IsEmpty(Queue *pqueue); // Read the item at the front. Data Peek(Queue *pqueue); // Insert an item at the rear. void EnQueue(Queue *pqueue, Data item); // Delete an item at the front. void DeQueue(Queue *pqueue);

Array-based Implementation InitQueue and IsFull operations [1] [2] [3] [4] [0] [5] rear front Empty // Make a queue empty. void InitQueue(Queue *pqueue) { pqueue->front = pqueue->rear = 0; } [1] [2] [3] [4] [0] [5] 2 7 5 4 3 rear front Full // Check whether a queue is full. bool IsFull(Queue *pqueue) { return pqueue->front == (pqueue->rear + 1) % MAX_QUEUE; }

Array-based Implementation IsEmpty and Peek operations [1] [2] [3] [4] [0] [5] rear front Empty // Check whether a queue is empty. bool IsEmpty(Queue *pqueue) { return pqueue->front == pqueue->rear; } [1] [2] [3] [4] [0] [5] 2 rear front 7 5 // Read the item at the front. Data Peek(Queue *pqueue) { if (IsEmpty(pqueue)) exit(1); //error: empty stack return pqueue->items[pqueue->front]; }

Array-based Implementation Enqueue operation [1] [2] [3] [4] [0] [5] 2 rear front 7 EnQueue 5 5 // Insert an item at the rear. void EnQueue(Queue *pqueue, Data item) { if (IsFull(pqueue)) exit(1); //error: stack full pqueue->items[pqueue->rear] = item; pqueue->rear = (pqueue->rear + 1) % MAX_QUEUE; }

Array-based Implementation Dequeue operation [1] [2] [3] [4] [0] [5] 2 rear front 7 5 DeQueue // Delete an item at the front. void DeQueue(Queue *pqueue) { if (IsEmpty(pqueue)) exit(1); //error: empty stack pqueue->front = (pqueue->front + 1) % MAX_QUEUE; }

Buffer Management The queue is used to implement a buffer that connects two processes. A buffer is a memory storage used to temporarily store data while it is moved from one place to another. Queue …… Queue Sending Receiving

Buffer Management // Example of a producer process void Producer(Queue* buffer, Data data) { if (lock(buffer) == false) { if (!IsFull(buffer)) { // Append the data to the buffer. EnQueue(buffer, data); } unlock(buffer); // Example of a consumer process void Consumer(Queue* buffer, Data data) if (!IsEmpty(buffer)) { Data data = Peek(buffer); DeQueue(buffer); // Consume the data. // ....

Bank Simulation Each customer has id, arrival time, and service time. Customers have arrived sequentially. Customers wait until previous customers finish their services. #include "queue.h" #include <stdlib.h> #include <stdio.h> #define MAX_SERV_TIME 10 typedef struct { int id; int arrival_time; int service_time; } Customer; int waited_time = 0; int served_customers = 0; int num_customers = 0; Queue

Results of Bank Simulation Time = 1 Time = 2 Customer 1 enters. service time: 5 mins Customer 1: 5 mins service starts. waiting time: 0 mins Time = 3 Customer 2 enters. service time: 10 mins Time = 4 Time = 5 Time = 6 Customer 3 enters. service time: 3 mins Time = 7 Customer 4 enters. service time: 6 mins Time = 8 Customer 5 enters. service time: 2 mins Customer 2: 10 mins service starts. waiting time: 5 mins Time = 9 Customer 6 enters. service time: 2 mins Time = 10 Total waiting time = 5 mins Average waiting time per customer = 2.50 mins Number of served customer = 2 Number of waiting customers = 4

Implementing Bank Simulation int main() { int service_time = 0, duration = 10; int clock = 0, id = 1; Queue queue; InitQueue(&queue); while (clock < duration){ clock++; printf("Time = %d\n", clock); if (IsCustomerArrived()) // Insert a customer in a sequential manner. InsertCustomer(&queue, id++, clock); // Remove a customer in a sequential manner. if (service_time > 0) service_time--; else service_time = RemoveCustomer(&queue, clock); } PrintStat(); // Print statistics on customers. return 0;

Implementing Bank Simulation InsertCustomer function A customer enters unless the queue is full. void InsertCustomer(Queue* pqueue, int id, int clock) { Customer c; int service_time = (int)(rand() % MAX_SERV_TIME) + 1; if (IsFull(pqueue)) return; c.id = id; c.arrival_time = clock; c.service_time = service_time; EnQueue(pqueue, c); printf("Customer %2d enters. service time: %d mins\n", id, service_time); num_customers++; }

Implementing Bank Simulation RemoveCustomer function A customer’s service starts. The other customers wait until the service is completed. int RemoveCustomer(Queue* pqueue, int clock) { Customer customer; int service_time = 0; if (IsEmpty(pqueue)) return 0; customer = Peek(pqueue); DeQueue(pqueue); service_time = customer.service_time; printf("Customer %2d: %2d mins service starts. waiting time: %2d mins\n", customer.id, service_time, clock - customer.arrival_time); served_customers++; waited_time += clock - customer.arrival_time; return service_time; }

Implementing Bank Simulation IsCustomerArrived and PrintStat functions IsCustomerArririved checks whether a customer is arrived or not. PrintStat prints general statistics on bank service. bool IsCustomerArrived() { double prob = rand() / (double)RAND_MAX; if (prob >= 0.5) return true; else return false; } void PrintStat() { printf("\nTotal waiting time = %d mins\n", waited_time); printf("Average waiting time per customer = %.2f mins\n", (double)waited_time / served_customers); printf("Number of served customer = %d\n", served_customers); printf("Number of waiting customers = %d\n", num_customers - served_customers); }

What is Deque? Definition Double-ended queue that generalizes a queue Elements are added to or removed from either the front or rear. Enqueue for both front and rear. Dequeue for both front end rear. rear front dequeue enqueue

What is Deque? Operations InitDeque : make deque empty. IsFull : check whether deque is full. IsEmpty : check whether deque is empty. AddFront: Insert an item at the front. AddRear: Insert an item at the rear. RemoveFront: Delete an item at the front. RemoveRear: Delete an item at the rear. PeekFront : Read the item at the front. PeekRear : Read the item at the rear.

Palindrome Checker A palindrome is a sequence of characters that reads the same backward as forward. bool checkPalindrome(char * str, int len) { Deque deq; InitDeque(&dep); for (int i = 0; i < len; i++) AddRear(&dep, str[i]); while (len > 1) if (PeekFront(&dep) == PeekRear(&deq)) RemoveFront(&dep), RemoveRear(&deq); len = len - 2; } else return false; return true;

Palindrome Checker Checking a palindrome using stack and queue bool checkPalindrome(char* str, int len) { Stack stack; Queue queue; InitStack(&stack); InitQueue(&queue); for (int i = 0; i < len; i++) { Push(&stack, str[i]); EnQueue(&queue, str[i]); } while (!IsEmpty(&queue)) { if (Peek(&stack) == Peek(&queue)) { Pop(&stack); DeQueue(&queue); else return false; return true;