CS 2412 Data Structures. Chapter 3 Queues



Similar documents
Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Linked Lists Linked Lists, Queues, and Stacks

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

Stacks. Linear data structures

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

DATA STRUCTURE - QUEUE

CS 2412 Data Structures. Chapter 2 Stacks and recursion

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

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

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

Programming with Data Structures

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

Module 2 Stacks and Queues: Abstract Data Types

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

Introduction to Data Structures and Algorithms

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

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

Basic Data Structures and Algorithms

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

PES Institute of Technology-BSC QUESTION BANK

CSE 211: Data Structures Lecture Notes VII

STACKS,QUEUES, AND LINKED LISTS

Lecture Notes on Stacks & Queues

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

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

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

CmpSci 187: Programming with Data Structures Spring 2015

10CS35: Data Structures Using C

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

Universidad Carlos III de Madrid

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.

Algorithms and Data Structures

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

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

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

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

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

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

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

Chapter 3: Restricted Structures Page 1

7.1 Our Current Model

Data Structures and Algorithms Stacks and Queues

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

Data Structures and Algorithms

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

Data Structures and Data Manipulation

Introduction to Data Structures

Analysis of a Search Algorithm

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Sequential Data Structures

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!

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

Cpt S 223. School of EECS, WSU

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

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

Data Structures and Algorithms V Otávio Braga

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

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

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

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

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

1 Abstract Data Types Information Hiding

Abstract Data Types. Chapter 2

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

DATA STRUCTURES USING C

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

C++ INTERVIEW QUESTIONS

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

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

Lecture 12 Doubly Linked Lists (with Recursion)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

14 Stacks, Queues, And Linked Lists

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

ADTs,, Arrays, Linked Lists

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Binary Heap Algorithms

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Illustration 1: Diagram of program function and data flow

Queue Implementations

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

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.

DATA STRUCTURE - STACK

STACK Data Structure:

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

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

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

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

Data Structures Using C++

C++FA 5.1 PRACTICE MID-TERM EXAM

Data Structures UNIT III. Model Question Answer

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Chapter 5 Instructor's Manual

Intel EP80579 Software for Security Applications on Intel QuickAssist Technology Cryptographic API Reference

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

Transcription:

CS 2412 Data Structures Chapter 3 Queues

3.1 Definitions A queue is an ordered collection of data in which all additions to the collection are made at one end (called rare or tail) and all deletions from the collection are made at the other end (called front or head). A stack is FILO, while a queue is FIFO (first-in first-out). Data Structure 2014 R. Wei 2

Applications: CPU waiting list. Network router packets sequence. Simulations Data Structure 2014 R. Wei 3

Main operations: Creation Clear (damage) Append (add element) Serve (remove element) Data Structure 2014 R. Wei 4

Specification: void CteateQueue(Queue *q); precondition: None. postcondition: The queue q has been initialized to be empty. (Also can use Queue* CreateQueue(void); ) void ClearQueue(Queue *q); precondition: The queue q has been created. postcondition: All entries have been removed from q and it is now empty. Data Structure 2014 R. Wei 5

Boolean QueueEmpty(Queue *q); precondition: The queue has been created. postcondition: The function returns true or false according as queue q is empty or not. Boolean QueueFull(Queue *q); precondition: The queue has been created. postcondition: The function returns true or false according as queue q is full or not. Data Structure 2014 R. Wei 6

void Append(QueueEntry x, Queue *q); precondition: The queue has been create and is not full. postcondition: The entry x has been stored in the queue as last entry. void Serve(QueueEntry *x, Queue *q); precondition: The queue has been create and is not empty. postcondition: The first entry has been removed and returned as the value of x. Data Structure 2014 R. Wei 7

Some other operations which are not usual operations of a queue. int QueueSize(Queue *q); precondition: The queue has been created. postcondition: The function returns the number of entries in the queue. void QueueFront(QueueEntry *x, Queue *q); precondition: The queue has been created and it is not empty. postcondition: The value of first entry of q is copied to x. The queue remains unchanged. Data Structure 2014 R. Wei 8

3.2 Implementations Using an array to implement a queue has some difficulties. If after each Serve every item moves forward, then time consume is big. If the item not moves, the space will be wasted. A circular array can be used to solve that problem. Data Structure 2014 R. Wei 9

Circular array implementation To let an array be circular in C, we can use: if(i>=max-1) i=0; else i++; or i=(i+1)% MAX The difficulty is how to know the queue is empty or full if we use a circular array. Data Structure 2014 R. Wei 10

We can use a variable count to track of the numbers of entries in a queue to avoid the confusion of full or empty. #define MAXQUEUE 10 typedef char QueueEntry; typedef struct queue int count; int front; int rear; QueueEntry entry[maxqureue]; } Queue; Data Structure 2014 R. Wei 11

Prototypes: void CreateQueue(Queue *); void Append(QueueEntry,Queue *); void Serve(QueueEntry *,Queue *); Boolean QueueEmpty(Queue *); Boolean QueueFull(Queue *); Data Structure 2014 R. Wei 12

void CreateQueue(Queue *q) q->count = 0; q->front = 0; q->rear = -1; } void Append(QueueEntry x,queue *q) if(queuefull(q)) Error("Cannot append an entry to a full queue."); else q->count++; q->rear = (q->rear +1)%MAXQUEUE; q->entry[q->rear)]=x; } } Data Structure 2014 R. Wei 13

void Serve(QueueEntry *x,queue *q) if(queueempty(q)) Error("Can t serve from a an empty queue."); else q->cont--; *x = q->entry[q->front]; q->front=(q->front+1)%maxqueue; } } Data Structure 2014 R. Wei 14

Boolean QueueEmpty(Queue *q) return q->count <=0; } Boolean QueueFull(Queue *q) return q->cout >=MAXQUEUE; } int QueueSize(Queue *q) return q->count; } Data Structure 2014 R. Wei 15

3.3 Computer simulation Simulation is the use of one system to imitate the behavior of another system, when it is too expensive or dangerous to experiment with the real system. A computer simulation uses the steps of a program to imitate the behavior of a system under study. We study a simple but useful computer simulation which concentrates on queues as its basic data structure. These simulations imitate the behavior of systems in which there are queues of objects waiting to be served by various processes. Data Structure 2014 R. Wei 16

Example Consider a small but busy airport with only one runway: In each unit of time, one plane can land or one plane can take off, but not both. Planes arrive ready to land or to take off at random times. It is better to keep a plane waiting on the ground than in the air, so the airport allows a plane to take off only if there are no planes waiting to land. Data Structure 2014 R. Wei 17

We will use two queues: takeoff and landing in the simulation. A plane is appended to a queue at random with certain probability in a unit time. The simulation runs for many units of time. It starts for curtime from 1 to a variable endtime. We will use a function RandomNumber() to output a random number between 0 and INT MAX with given average, which will denote the number of planes at a unit of time. Data Structure 2014 R. Wei 18

Outline of the simulation program: Settings: Queue landing, takeoff; Queue *pl = &landing; Queue *pt = &takeoff; Plane plane; //abstrat of a plane int curtime; //current time unit int endtime; //total number of time units to run Initialization: CreateQueue(pl); CreateQueue(pt); Data Structure 2014 R. Wei 19

Simulate queue landing at a unit of time: for(i = 1; i <= RandomNumber();i++) NewPlane(&plane); //new planes arrive if (QueueFull(pl) Refuse(plane); else Append(plane,pl); } Simulation of queue takeoff at a unit of time is similar. Data Structure 2014 R. Wei 20

Simulation of the control: if(!queueempty(pl)) Serve(&plane,pl); Land(plane); } else if(!queueempty(pt)) Serve(&plane,pt); } else Idle(); Data Structure 2014 R. Wei 21

Now we consider some details. First, the queues we used are queue of planes. #define MAXQUEUE 5 //use a small value for testing typedef enum action ARRIVE, DEPART} Action; typedef struct plane int id; //id number of plane int tm; //time of arrival in queue } Plane; typedef Plane QueueEntry; typedef struct queue int count; //number of planes in the queue int front; int rear; QueueEntry entry[maxqueue]; } Queue; Data Structure 2014 R. Wei 22

The purpose of the simulation is to obtain some data about the airport. So we need to record the number of planes, the waiting time, the numbers of departure and arriving planes, etc. The simulation also wants to know the different data in different settings of the probability of arriving planes and departing planes. Data Structure 2014 R. Wei 23

/*NewPlane: make a new record for a plan, update nplanes. Pre: None. Post: Makes a new plane and update nplanes.*/ void NewPlane(Plane *p,int *nplanes,int curtime,action kind) (*nplanes)++; p->id=*nplanes; p->tm=curtime; switch(kind) case ARRIVE: printf(" Plane %3d ready to land.\n",*nplanes); break; case DEPART: printf(" Plane %3d ready to take off.\n",*nplanes); break; } } Data Structure 2014 R. Wei 24

/*Refuse: processes a plan when the queueis full. Pre: None. Post: processes a plane requesting runway, but queue is full*/ void Refuse(Plane p, int *nrefuse,action kind) switch(kind) case ARRIVE: printf(" Plane %3d directed to another airport.\n",p.id); break; case DEPART: printf(" Plane %3d told to try later.\n".p.id); break; } (*nrefuse)++; } Data Structure 2014 R. Wei 25

/*Land: process a plane that is actually landing. Pre: None. Post: Precesses a plane p that is landing.*/ void Land(Plane p,int curtime,int *nland,int *landwait) int wait; wait = curtime - p.tm; printf("%3d: Plane %3d landed; in queue %d units. \n",curtime,p.id,wait); (*nland)++; *landwait+= wait; } The function Fly is similar. Data Structure 2014 R. Wei 26

/* Idle: undate variables for idle runway. Pre: None. Post: Update variables for a time unit when the runway is idle. */ void Idel(int curtime,int *idletime) printf("%3d: Runway is idel.\n",curtime); (*idletime)++; } Data Structure 2014 R. Wei 27

Two functions: Start and Conclude are used to initialize and conclude statistical data for the simulation. /*Start: print messages and initialize the parameters. Pre:None. Post: Asks user for responses and initializes all variables specified as parameters. Uses: UserSaysYes.*/ void Start(int *endtime,double *expectarrive, double *expectdepart) Data Structure 2014 R. Wei 28

/*Conclude: write out statistics and conclude simulation. Pre: None. Post: Writes out all the statistics and concludes the simulation.*/ void Conclude(int nplanes, int nland, int ntakeoff, int nrefuse, int landwait, int takeoffwait, int idletime,int endtime, Queue *pt, Queue *pl) We omitted the details of these two functions. The details of creating pseudo-random numbers are omitted. Data Structure 2014 R. Wei 29

Implementation using dynamic memory // Queue ADT Type Defintions typedef struct node void* dataptr; struct node* next; } QUEUE_NODE; typedef struct QUEUE_NODE* front; QUEUE_NODE* rear; int count; } QUEUE; Data Structure 2014 R. Wei 30

QUEUE* createqueue (void) // Local Definitions QUEUE* queue; // Statements queue = (QUEUE*) malloc (sizeof (QUEUE)); if (queue) queue->front = NULL; queue->rear = NULL; queue->count = 0; } // if return queue; } // createqueue Data Structure 2014 R. Wei 31

bool enqueue (QUEUE* queue, void* itemptr) QUEUE_NODE* newptr; if (!(newptr = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE)))) return false; newptr->dataptr = itemptr; newptr->next = NULL; if (queue->count == 0) queue->front = newptr; else queue->rear->next = newptr; (queue->count)++; queue->rear = newptr; return true; } // enqueue Data Structure 2014 R. Wei 32

bool dequeue (QUEUE* queue, void** itemptr) QUEUE_NODE* deleteloc; if (!queue->count) return false; *itemptr = queue->front->dataptr; deleteloc = queue->front; if (queue->count == 1) queue->rear = queue->front = NULL; else queue->front = queue->front->next; (queue->count)--; free (deleteloc); return true; } // dequeue Data Structure 2014 R. Wei 33

bool queuefront (QUEUE* queue, void** itemptr) // Statements if (!queue->count) return false; else *itemptr = queue->front->dataptr; return true; } // else } // queuefront Data Structure 2014 R. Wei 34

bool queuerear (QUEUE* queue, void** itemptr) // Statements if (!queue->count) return true; else *itemptr = queue->rear->dataptr; return false; } // else } // queuerear Data Structure 2014 R. Wei 35

bool emptyqueue (QUEUE* queue) // Statements return (queue->count == 0); } // emptyqueue Data Structure 2014 R. Wei 36

bool fullqueue (QUEUE* queue) // Local Definitions * QUEUE_NODE* temp; // Statements temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear))); if (temp) free (temp); return true; } // if // Heap full return false; } // fullqueue Data Structure 2014 R. Wei 37

int queuecount(queue* queue) // Statements return queue->count; } // queuecount Data Structure 2014 R. Wei 38

QUEUE* destroyqueue (QUEUE* queue) QUEUE_NODE* deleteptr; if (queue) while (queue->front!= NULL) free (queue->front->dataptr); deleteptr = queue->front; queue->front = queue->front->next; free (deleteptr); } // while free (queue); } // if return NULL; } // destroyqueue Data Structure 2014 R. Wei 39

Application: Polynomial arithmetic Using a reverse Polish calculator to do polynomial arithmetics. basic idea is using a stack. The basic idea of reverse Polish calculator is to input operands first and input operation. For example: if we want to calculate (a + b) (c + d), then the inputs are: a b + c d +. If we want to calculate a (b + c), then the inputs are: b c + a. To do the calculations, push the operands to a stack. If an operation is input, then pop out two operands and do the operation and then push the result to the stack. When = is input, pop out the result. Data Structure 2014 R. Wei 40

The outline of the program is quite simple: mainly use ReadCommand to get the inputs and use DoCommand to do the calculation. To represent polynomials, we use a queues. For a polynomial 3x 5 2x 3 + x 2 + 4, we can use a queue of size 4 to represent it. Each item of the queue represent a term. Data Structure 2014 R. Wei 41

typedef struct term double coef; int exp; } Term; The term 2x 3 can be stored as: Term *newterm; newterm->coef = -2.0; newterm->exp = 3; Data Structure 2014 R. Wei 42