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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
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.
Introduction to C Programming CE Lecture 12 Circular Queue and Priority Queue Data Structures.
CHAPTER 7 Queues.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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.
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 CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO 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. 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.
Stacks, Queues, and Deques
Objectives of these slides:
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
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’
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
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 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
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.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
CS505 Data Structures and Algorithms
Queue data structure.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues: Implemented using Arrays
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks CS-240 Dick Steflik.
Circular Queues: Implemented using Arrays
Stacks, Queues, and Deques
Data Structures & Programming
Presentation transcript:

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 chronological order (oldest first) examples: grocery store line, tube feed ammunition magazine, digital shift register,

Applications discrete event simulation - digital delay line task scheduling in an operation system staging area for data acquisition systems I/O buffers print queues sorting

front rear checkout

ADT Methods create – make the queue and initialize empty enqueue - add an item (at the back, move it up as far as possible dequeue - return the value of the item at the front of the queue then delete the item and move all items forward one position. destroy – dispose of the queue

Overflow / Underflow Same problem as stacks –trying to dequeue from an empty queue –trying to enqueue to a full queue Solution: –isEmpty - return false/true depending if the queue is empty; true if it is, false otherwise –isFull – return false/ depending if the queue is empty; true if it is, false otherwise

Data strategies use an array to hold items and use an int as an index for the array to indicate where the back of the queue is same as above, but use a dynamic array same as above but treat array as if it were circular and use two ints, one to indicate the front and the other to indicate the back make a type using a struct with two ints for the front and rear pointers and an array for the data part of the queue use a struct to define a node and add nodes dynamically as they are needed; use one static pointer to a node to point at the front node and another to point at the back node.

Simple array implementation #define front 0 //assume front is always at location 0 of data int back = 0; int size = 0; int data[MAXSIZE] = {0}; void enqueue(int a) { data[back] = a ; back++; size++ }; int dequeue(void) { int temp = data[front] ; for (i=0 ; i < back ; i++) data[i] = data[i+i] ; back--; return temp; }; int isEmpty() { return back == 0; }; int isFull() { back == MAXSIZE; };

Static struct Implementation typedef struct { int front,rear; int data[MAXQSIZE] queue; void enqueue(queue * p, int v) { if (isFull(p)) printf(“queue is full \n”); else { p->data[p->rear] = v; p->rear += 1; } int main() { queue q1 = {0}; enqueue(&q1,50); }

Circular Queue Please read: Think of the array as being circular; i.e. the item following the item in the last array position is the first array position, this can be done using the mod (%) operator. Mod returns the integer remainder after a division. Assume the data portion of a queue has 10 elements and first and last start at the same position (this is the empty situation), lets say front rear empty

adding enqueue(1) enqueue(2) enqueue(3) enqueue(4) enqueue(5) enqueue(6) front rear rear always points to next place to add so (rear++)%size when we added the 5, rear was 9 and (9+1)%10 = 0 so when we add the 6, rear was 0 and (0+1)%10 = 1 the queue is empty if front == rear, how do we sense a full condition?

when is it full? add a count element to the queue that always has the current number of elements –enqueue always increments the count –dequeue always decrements the count require that only maxsize-1 elements can ever be used, there is always one unused element (i.e. |rear-front| > 1), or if |rear-front| == 1 then queue is full

Priority Queues Operates like a queue except entry is at the back but items move up to the end of their priority list new item frontback 4

Priority Queues Because of the possible high amount of data movement (in moving an item up in the queue) there are better ways of implementing priority queues as either linked lists or trees. A linked list would produce a possible O(n) and a Tree could approach O(log 2 n) for insertion and O(1) for retrieval. We’ll examine these later in the course