Queues. Manolis Koubarakis. Data Structures and Programming Techniques



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

CS 2412 Data Structures. Chapter 3 Queues

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

Linked Lists Linked Lists, Queues, and Stacks

Common Data Structures

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 Using C++ 2E. Chapter 5 Linked Lists

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

Unordered Linked Lists

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

Queue ADT. March 16, 2000

Lecture 12 Doubly Linked Lists (with Recursion)

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

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.

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

Basic Data Structures and Algorithms

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

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

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

Analysis of a Search Algorithm

DATA STRUCTURE - STACK

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

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

Stacks. Linear data structures

DATA STRUCTURE - QUEUE

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

DATA STRUCTURES USING C

Linked List as an ADT (cont d.)

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

recursion, O(n), linked lists 6/14

Module 2 Stacks and Queues: Abstract Data Types

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

Programming with Data Structures

Lecture Notes on Binary Search Trees

Data Structures and Algorithms

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

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Sequential Data Structures

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

10CS35: Data Structures Using C

Data Structure with C

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

STACKS,QUEUES, AND LINKED LISTS

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

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

LINKED DATA STRUCTURES

TREE BASIC TERMINOLOGIES

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

Converting a Number from Decimal to Binary

CSE 211: Data Structures Lecture Notes VII

Introduction to Data Structures and Algorithms

Lecture Notes on Stacks & Queues

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

Universidad Carlos III de Madrid

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!

ADTs,, Arrays, Linked Lists

Circular Linked List. Algorithms and Data Structures

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Data Structures and Algorithms Stacks and Queues

Data Structures and Algorithms Lists

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

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

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Data Structures Using C++

Level 3 Develop software using Java (7266/ )

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Chapter 3: Restricted Structures Page 1

Algorithms and Data Structures

Monitors, Java, Threads and Processes

arrays C Programming Language - Arrays

Computer Programming using C

Timing of a Disk I/O Transfer

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Lecture Notes on Binary Search Trees

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

CmpSci 187: Programming with Data Structures Spring 2015

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Data Structure [Question Bank]

Chapter 11 I/O Management and Disk Scheduling

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures and Data Manipulation

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Data Structures UNIT III. Model Question Answer

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

Algorithms and Abstract Data Types

1 Abstract Data Types Information Hiding

3 Abstract Data Types and Search

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

OPTIMAL BINARY SEARCH TREES

Data Structures and Algorithms

Threads Scheduling on Linux Operating Systems

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

Why Use Binary Trees?

Data Structures. Level 6 C Module Descriptor

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Stacks. Data Structures and Data Types. Collections

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Data Structures and Algorithms C++ Implementation

Transcription:

Queues Manolis Koubarakis 1

The ADT Queue A queue Q of items of type T is a sequence of items of type T on which the following operations are defined: Initialize the queue to the empty queue. Determine whether or not the queue is empty. Determine whether or not the queue is full. Insert a new item onto the rear of the queue. Provided Q is nonempty, remove an item from the front of Q. 2

The ADT Queue (cont d) Queues are also known as FIFO lists (first-in first-out). 3

Queue Representations The ADT queue can be implemented using either sequential or linked representations. 4

Sequential Queue Representations We can use an array as follows: Departures Arrivals q 1 q 2 q 3 q 4 Direction of travel through memory 5

Sequential Queue Representations (cont d) This representation is not very handy. The positions of the array to the right will be filled until there is space to do so, while the positions to the left of the array will be freed but we will not be able to use that free space. The bounded space representation proposed next is a better one. 6

Circular Queue Representation Front Rear q 1 q 2 q 6 q 3 q 5 q 4 7

Circular Queue Representation (cont d) If we have an array Items[0:N-1] and two pointers Front and Rear as in the previous figure, then we can use the following assignment statements to increment the pointers so that they always wrap around after falling off the high end of the array. Front=(Front+1)%N Rear=(Rear+1)%N The operator % computes the remainder of the division by N so the values of Front and Rear are always in the range 1 to N-1. 8

Defining the Queue Data Type /* This is the file QueueTypes.h */ #define MAXQUEUESIZE 100 typedef int ItemType; /* the item type can be arbitrary */ typedef struct { int Count; int Front; int Rear; ItemType Items[MAXQUEUESIZE]; Queue; 9

The Interface File /* This is the file QueueInterface.h */ #include QueueTypes.h void InitializeQueue(Queue *Q); int Empty(Queue *Q); int Full(Queue *Q); void Insert(ItemType R, Queue *Q); void Remove(Queue *Q, ItemType *F); 10

The Implementation /* This is the file QueueImplementation.c */ #include <stdio.h> #include <stdlib.h> #include QueueInterface.h void InitializeQueue(Queue *Q) { Q->Count=0; Q->Front=0; Q->Rear=0; 11

The Implementation (cont d) int Empty(Queue *Q) { return(q->count==0); int Full(Queue *Q) { return(q->count==maxqueuesize); 12

The Implementation (cont d) void Insert(ItemType R, Queue *Q) { if (Q->Count==MAXQUEUESIZE){ printf( attempt to insert item into a full queue ); else { Q->Items[Q->Rear]=R; Q->Rear=(Q->Rear+1)%MAXQUEUESIZE; ++(Q->Count); 13

The Implementation (cont d) void Remove(Queue *Q, ItemType *F) { if (Q->Count==0){ printf( attempt to remove item from empty queue ); else { *F=Q->Items[Q->Front]; Q->Front=(Q->Front+1)%MAXQUEUESIZE; --(Q->Count); 14

Linked Queue Representation In this implementation, we represent a queue by a struct containing pointers to the front and rear of a linked list of nodes. Q: Front Rear. Item Link Item Link Item Link Item x 1 x 2 x 3 x 4 Link 15

Linked Queue Representation (cont d) The empty queue is a special case and it is represented by a queue struct whose front and rear pointers are NULL. Front. Rear. Q: 16

Defining the Queue Data Type /* This is the file QueueTypes.h */ typedef int ItemType; /* the item type can be arbitrary */ typedef struct QueueNodeTag { ItemType Item; struct QueueNodeTag *Link; QueueNode; typedef struct { QueueNode *Front; QueueNode *Rear; Queue; 17

The Implementation /* This is the file QueueImplementation.c */ #include <stdio.h> #include <stdlib.h> #include QueueInterface.h void InitializeQueue(Queue *Q) { Q->Front=NULL; Q->Rear=NULL; 18

The Implementation (cont d) int Empty(Queue *Q) { return(q->front==null); int Full(Queue *Q) { return(0); /* We assume an already constructed queue */ /* is not full since it can potentially grow */ /* as a linked structure. */ 19

The Implementation (cont d) void Insert(ItemType R, Queue *Q) { QueueNode *Temp; Temp=(QueueNode *)malloc(sizeof(queuenode)); if (Temp==NULL){ printf( System storage is exhausted ); else { Temp->Item=R; Temp->Link=NULL; if (Q->Rear==NULL){ Q->Front=Temp; Q->Rear=Temp; else { Q->Rear->Link=Temp; Q->Rear=Temp; 20

The Implementation (cont d) void Remove(Queue *Q, ItemType *F) { QueueNode *Temp; if (Q->Front==NULL){ printf( attempt to remove item from an empty queue ); else { *F=Q->Front->Item; Temp=Q->Front; Q->Front=Temp->Link; free(temp); if (Q->Front==NULL) Q->Rear=NULL; 21

Example main program #include <stdio.h> #include <stdlib.h> #include "QueueInterface.h" int main(void) { int i,j; Queue Q; InitializeQueue(&Q); for(i=1; i<10; ++i){ Insert(i, &Q); while (!Empty(&Q)){ Remove(&Q, &j); printf("item %d has been removed.\n", j); return 0; 22

Comparing Linked and Sequential Queue Representations The sequential queue representation is appropriate when there is a bound on the number of queue elements at any time. The linked representation is appropriate when we do not know how large the queue will grow. 23

Using Queues Queues of jobs are used a lot in operating systems and networks (e.g., a printer queue). Queues are also used in simulation. Queuing theory is a branch of mathematics that studies the behaviour of systems with queues. 24

Readings T. A. Standish. Data Structures, Algorithms and Software Principles in C. Chapter 7. 25