Review Array Array Elements Accessing array elements

Slides:



Advertisements
Similar presentations
Ceng-112 Data Structures I Chapter 5 Queues.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Data Structure Dr. Mohamed Khafagy.
DATA STRUCTURE & ALGORITHMS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Stacks, Queues, and Deques
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Stacks And Queues Chapter 18.
Implementation of QUEUE For more notes and topics visit: eITnotes.com.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
CE 221 Data Structures and Algorithms
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
UNIT II Queue.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
Queues Chapter 4.
Program based on queue & their operations for an application
Queues Rem Collier Room A1.02
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:32 PM Queues.
Stacks, Queues, and Deques
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks, Queues, and Deques
Lesson Objectives Aims
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures and Algorithms for Information Processing
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
CE 221 Data Structures and Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
Using a Queue Chapter 8 introduces the queue data type.
CSCS-200 Data Structure and Algorithms
Data Structures Using C++ 2E
Using a Queue Chapter 8 introduces the queue data type.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Data Structures & Programming
Presentation transcript:

Review Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure String Array of Strings Examples

Queue Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples

INTRODUCTION A queue is logically a first in first out (FIFO or first come first serve) linear data structure. It is a homogeneous collection of elements in which new elements are added at one end called rear, and the existing elements are deleted from other end called front. The basic operations that can be performed on queue are 1. Insert (or add) an element to the queue (push) 2. Delete (or remove) an element from a queue (pop) Push operation will insert (or add) an element to queue, at the rear end, by incrementing the array index. Pop operation will delete (or remove) from the front end by decrementing the array index and will assign the deleted value to a variable.

A Graphic Model of a Queue Head: All items are deleted from this end Tail: All new items are added on this end

Operations on Queues Insert(item): (also called enqueue) It adds a new item to the tail of the queue Remove( ): (also called delete or dequeue) It deletes the head item of the queue, and returns to the caller. If the queue is already empty, this operation returns NULL getHead( ): Returns the value in the head element of the queue getTail( ): Returns the value in the tail element of the queue isEmpty( ) Returns true if the queue has no items size( ) Returns the number of items in the queue

Examples of Queues An electronic mailbox is a queue The ordering is chronological (by arrival time) A waiting line in a store, at a service counter, on a one-lane road Equal-priority processes waiting to run on a processor in a computer system

BASIC OPERATIONS ON QUEUE

The Queue Operations Front Rear A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive. Front Rear

The Queue Operations Front Rear New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear. Front Rear

The Queue Operations Front Rear When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ When an item is removed from a queue, the removal occurs at the front. Front Rear

Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array. 4 8 6 An array of integers to implement a queue of integers We don't care what's in this part of the array.

Array Implementation size The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). 3 first last 2 The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

A Dequeue Operation size When an element leaves the queue, size is decremented, and first changes, too. 2 first 1 last 2 This shows how the member variables change when an item leaves the queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

An Enqueue Operation size When an element enters the queue, size is incremented, and last changes, too. 3 first 1 last 3 And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 8 6 2

At the End of the Array size There is special behavior at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 3 first 3 last 5 An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so… [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 2 6 1

At the End of the Array size The new element goes at the front of the array (if that spot isn’t already used): 4 first 3 last …by putting it at location 0 (if that location is not already used). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 4 2 6 1

Array Implementation size Easy to implement But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. 3 first last 2 Here are some of the key aspects of an array implementation of a queue. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] . . . 4 8 6

Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer. 13 15 A linked list can also be used to implement a queue, but we must maintain both a head and a tail pointer because we need access to both the front and the rear of the queue. 10 7 null head_ptr tail_ptr

Linked List Implementation Which end do you think is the front of the queue? Why? 13 15 Does it matter which end of a singly-linked list we use for the front of the queue? 10 7 null head_ptr tail_ptr

Linked List Implementation The head_ptr points to the front of the list. Because it is harder to remove items from the tail of the list. Front 13 15 Of course, we could put the front of the queue at the end of the linked list, but it would be hard to remove an item. Do you see why? 10 7 null head_ptr Rear tail_ptr

Like stacks, queues have many applications. Items enter a queue at the rear and leave a queue at the front. Queues can be implemented using an array or using a linked list. A quick summary . . .

Queue can be implemented in two ways: 1. Using arrays (static) 2. Using pointers (dynamic) If we try to pop (or delete or remove) an element from queue when it is empty, underflow occurs. If we try to push (or insert or add) an element to queue When queue is full, overflow occurs. Total number of elements present in the queue is rear- front+1, when implemented using arrays.

ALGORITHM FOR QUEUE OPERATIONS Let Q be the array of some specified size say SIZE Inserting an element into the queue 1. Initialize front=0 rear = –1 2. Input the value to be inserted and assign to variable “data” 3. If (rear >= SIZE) (a) Display “Queue overflow” (b) Exit 4. Else (a) Rear = rear +1 5. Q[rear] = data 6. Exit

ALGORITHM FOR QUEUE OPERATIONS Deleting an element from queue 1. If (front==-1 II rear< front) (a) Display “The queue is empty” (b) Exit 2. Else (a) Data = Q[front] 3. front = front +1 4. Exit

Queue as a Class Much like stacks and linked lists were designed and implemented as classes, a queue can be conveniently packaged as a class It seems natural to think of a queue as similar to a linked list, but with more basic operations, to enforce the FIFO order of insertion and deletion

A Linked List Implementation of the Queue Class The container for items is a linked list, that is, an object of type List Let’s call it: List q;

A Dynamic-Array Implementation of the Queue Class The container for items is a dynamic array It is accomplished as: datatype *p=new datatype[50];

How head and tail Change head increases by 1 after each dequeue( ) tail increases by 1 after each enqueue( ) head tail Now: 1 2 47 48 49 4 3 head tail After enqueue: 1 2 47 48 49 4 3 head tail After dequeue: 1 2 47 48 49 4 3

False-Overflow Issue First Suppose 50 calls to enqueue have been made, so now the queue array is full Assume 4 calls to dequeue( ) are made Assume a call to enqueue( ) is made now. The tail part seems to have no space, but the front has 4 unused spaces; if never used, they are wasted. 49 48 47 4 3 2 1 49 48 47 4 3 2 1

Solution: A Circular Queue Allow the head (and the tail) to be moving targets When the tail end fills up and front part of the array has empty slots, new insertions should go into the front end Next insertion goes into slot 0, and tail tracks it. The insertion after that goes into a lot 1, etc. head tail 1 2 47 48 49 4 3

Illustration of Circular Queues Current state: After One Call to enqueue() head 1 2 47 48 49 3 4 tail head tail 1 2 47 48 49 3 4 head tail 1 2 47 48 49 3 4

Numerics for Circular Queues head increases by (1 modulo capacity) after each dequeue( ): head = (head +1) % capacity; tail increases by (1 modulo capacity) after each enqueue( ): tail = (tail +1) % capacity;

Queue Applications Real life examples Waiting in line Waiting on hold for tech support Applications related to Computer Science Threads Job scheduling (e.g. Round-Robin algorithm for CPU allocation)

First In First Out rear front D C B A B A C B A D C B A rear front

Applications: Job Scheduling

Wrapped Configuration Implementation 2: Wrapped Configuration EMPTY QUEUE [2] [3] [2] [3] J2 J3 [1] [4] [1] [4] J1 [0] [5] [0] [5] front = 0 front = 0 rear = 0 rear = 3 Can be seen as a circular queue

FULL QUEUE FULL QUEUE [2] [3] [2] [3] J8 J9 J2 J3 [1] [4][1] [4] J7 Leave one empty space when queue is full Why? FULL QUEUE FULL QUEUE [2] [3] [2] [3] J2 J3 J8 J9 [1] [4][1] [4] J1 J4 J7 J6 J5 J5 [0] [5] [0] [5] front =0 rear = 5 front =4 rear =3 How to test when queue is empty? How to test when queue is full?

Queue Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples