QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q



Similar documents
DATA STRUCTURE - QUEUE

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Common Data Structures

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Linked Lists Linked Lists, Queues, and Stacks

Programming with Data Structures

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

DATA STRUCTURES USING C

Chapter 3: Restricted Structures Page 1

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

St S a t ck a ck nd Qu Q eue 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Module 2 Stacks and Queues: Abstract Data Types

Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

DATA STRUCTURE - STACK

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Data Structures and Algorithms Stacks and Queues

EECE 276 Embedded Systems

Introduction to Data Structures and Algorithms

Data Structures and Data Manipulation

Application of Stacks: Postfix Expressions Calculator (cont d.)

STACKS,QUEUES, AND LINKED LISTS

Analysis of a Search Algorithm

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Algorithms and Data Structures

Analysis of Algorithms, I

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Stacks. Linear data structures

Universidad Carlos III de Madrid

CmpSci 187: Programming with Data Structures Spring 2015

Chapter 13. Chapter Outline. Disk Storage, Basic File Structures, and Hashing

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Static Load Balancing

I/O Management. General Computer Architecture. Goals for I/O. Levels of I/O. Naming. I/O Management. COMP755 Advanced Operating Systems 1

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Algorithms and Data Structures Written Exam Proposed SOLUTION

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Record Storage and Primary File Organization

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Lecture 2: Data Structures Steven Skiena. skiena

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Sequential Data Structures

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

PROBLEMS (Cap. 4 - Istruzioni macchina)

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Basic Data Structures and Algorithms

Design and Implementation of Distributed Process Execution Environment

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Data Structure [Question Bank]

10CS35: Data Structures Using C

Last Class: Semaphores

Cpt S 223. School of EECS, WSU

Linux Driver Devices. Why, When, Which, How?

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

Monitors, Java, Threads and Processes

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

Lecture 12 Doubly Linked Lists (with Recursion)

COMPUTER SCIENCE. Paper 1 (THEORY)


Cpt S 223. School of EECS, WSU

Section IV.1: Recursive Algorithms and Recursion Trees

Chapter 11 I/O Management and Disk Scheduling

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

CS414 SP 2007 Assignment 1

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

Chapter 11 I/O Management and Disk Scheduling

Load balancing Static Load Balancing

Unordered Linked Lists

Evolving Data Structures with Genetic Programming

Data Structures Fibonacci Heaps, Amortized Analysis

Load Balancing and Termination Detection

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Data Structures UNIT III. Model Question Answer

Operating Systems. Virtual Memory

1 Abstract Data Types Information Hiding

How To Solve The Stable Roommates Problem

Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Transcription:

QUEUES A queue is simply a waiting line that grows by adding elements to its end and shrinks by removing elements from the. Compared to stack, it reflects the more commonly used maxim in real-world, namely, first come, first served. Waiting lines in supermarkets, banks, food counters are common examples of queues. A formal definition of queue as a data structure: It is a list from which items may be deleted at one end () and into which items may be inserted at the other end (). It is also referred to as a first-in-first-out (FIFO) data structure. Queues have many applications in computer systems: Handling jobs in a single processor computer print spooling transmitting information packets in computer networks. Primitive Queue operations enqueue (q, x): inserts item x at the of the queue q x = dequeue (q): removes the element from q and returns its value. isempty(q) : Check to see if the queue is empty. isfull(q) : checks to see if there is space to insert more items in the queue. 1

Example: Consider the following sequence of operations being performed on a queue q which stores single characters: enqueue(q, A ); enqueue(q, B ); enqueue(q, C ); x = dequeue(q); enqueue(q, D ); enqueue(q, E ); x = dequeue(q); enqueue(q, H ); x = dequeue(q); enqueue(q, J ); The contents of the queue q after these operations would be D E H J 2

Array Implementation The array to implement the queue would need two variables (indices) called and to point to the first and the last elements of the queue. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 3 8 11 9 4 Initially: q-> = -1; q-> = -1; For each enqueue operation is incremented by one, and for each dequeue operation, is incremented by one. While the enqueue and dequeue operations are easy to implement, there is a big disadvantage in this set up. The size of the array needs to be huge, as the number of slots would go on increasing as long as there are items to be added to the list (irrespective of how many items are deleted, as these two are independent operations.) Problems with this representation: Although there is space in the following queue, we may not be able to add a new item. An attempt will cause an overflow. 0 1 2 3 4 C D E 3

It is possible to have an empty queue yet no new item can be inserted.( when moves to the point of, and the last item is deleted.) 0 1 2 3 4 E A solution: Circular Array Let us now imagine that the above array is wrapped around a cylinder, such that the first and last elements of the array are next to each other. When the queue gets apparently full, it can continue to store elements in the empty spaces in the beginning of the array. This makes efficient use of the array slots. It is also referred to as a circular array. This enables us to utilize the unavailable slots, provided the indices and are handled carefully. Here again refers to the index of the element to be next removed from the queue, and refers to the index of the last element added to the queue. 4

1 0 size-1 Size - 2 equivalently: 5

The queue is implemented on a circular array. With each enqueue, the index is incremented, and with each dequeue the index is incremented. The wrap around is achieved by taking the mod function with the capacity of the array. The queue is initialized with = = 0. When the first element is enqueued, the index becomes 1 while the index remains 0. When the second element is enqueued, the index becomes 2 while the index remains 0. If we now dequeue, the index becomes 1, and at the next dequeue operation, the index becomes 2. The queue is now empty as two elements were stored and these two were retrieved. The index values are = = 2. In fact, at any stage the condition = indicates that the queue is empty. Consider a queue with n slots. Suppose the index is zero, and we go on filling up the queue. When the last but one slot is filled up the index will have value n-1. Now if we want to insert another element in the queue, the index will be n. But since we do not have the slot n, it would point to the slot 0. Thus, while the queue is actually full we have = = 0, which is not possible because that is a condition for the queue to be empty. Here we are faced with a contradiction. To overcome this problem, the last element is never filled up in a circular queue, and when only a single slot is empty, we declare the queue to be full. Queue Size : The and indices can be used to find out the current size of the queue, that is, the number of elements currently in the queue. The value gives us the size, and when this value is negative, we simply add the capacity to this to give us the size. Thus in general we have size = ( + capacity) % capacity. 6

Enqueue and Dequeue algorithms: Here are the enqueue and the dequeue algorithms. It is assumed here that the variables,, and capacity are globally visible. initially, = 0; = 0; void enqueue(int Q[], int d) { plus1 = (+1)% capacity; if (plus1 == ) print Q full ; else { Q[]= d; = plus1; void dequeue( int Q[ ], *dd ) { if( == ) { *dd = -9999 ; print Q empty else { *dd = Q[]; fron =( +1)%capacity; 7