QUEUES – Concepts and Programming in C
SEEM 3460
1
Queue: a First-In-First-Out (FIFO) list
A
rear front
B A
C rear B front A
D rear C B front A
rear D C front B
rear front
Inserting and deleting elements in a queue
Application: • job scheduling SEEM 3460
2
Specification of the structure queue structure Queue is objects: a finite ordered list with zero or more elements. functions: for all entry Queue, item element, rear, front positive integer max_ queue_ size positive integer void CreateQ() ::= create an empty queue whose maximum size is max_queue_size Boolean IsFullQ(entry) ::= if(number of elements in entry == max_queue_size) return TRUE else return FALSE void AddQ(rear, item) ::= if (IsFullQ(entry)) { queue_full and return } else insert item at rear and update rear
SEEM 3460
3
Boolean IsEmptyQ(entry) ::= if (front == rear) return TRUE else return FALSE element DeleteQ(entry) ::= if (IsEmptyQ(entry)) return else { remove and return the item at front. update front. }
SEEM 3460
4
Specification of the structure queue front: refers to the position just before the first element rear: refers to the position of the last element rear
front 21
10
Initial value: front = -1 rear = -1
SEEM 3460
5
Example of a queue structure front: refers to the position just before the first element rear: refers to the position of the last element Q: array for storing the current elements in the queue front rear Q[0] Q[1] Q[2] Q[3] -1 -1 0 J1 -1 J2 1 J1 -1 J2 J3 2 J1 -1 J2 J3 2 0 J3 2 1
Comments queue is empty Job 1 is added Job 2 is added Job 3 is added Job 1 is deleted Job 2 is deleted
Insertion and deletion from a sequential queue SEEM 3460
6
(Revision) Parameter Design in Functions The following parameter design cannot work as desired #include void incrementby1 (int i); int main() { int x; x = 10; incrementby1 (x); printf("x=%d\n",x); } void incrementby1 (int x) { x = x+1; }
SEEM 3460
7
(Revision) Parameter Design in Functions The following parameter design is correct #include void incrementby1 (int *i); int main() { int x; x = 10; incrementby1 (&x); printf("x=%d\n",x); } void incrementby1 (int *x) { *x = *x+1; }
SEEM 3460
8
Function Design for Adding an Element to a queue void addq(int *rear, element item) { /* add an item to the queue */ if (*rear == MAX_QUEUE_SIZE-1) { queue_full( ); return; } *rear = *rear + 1; entry[*rear] = item; }
SEEM 3460
9
Function Design for Deleting an Element from a queue element deleteq(int *front, int rear) { /* remove element at the front of the queue */ if ( *front == rear) return queue_empty( ); /* return an error key */ *front = *front + 1; return (entry[*front]); }
SEEM 3460
10
A sample complete C program for queue /* queue.c */ #include #include #define MAXSIZE 4 int entry[MAXSIZE]; void createqueue(int*, int*); /* same as void createqueue(int *front, int *read);
*/
void addqueue(int*, int); int deletequeue(int*, int); void createqueue(int *front, int *rear) { *rear = -1; *front = -1; } /* front refers to the position just before the first element rear refers to the position of the last element */ SEEM 3460
11
A sample complete C program for queue (con’t) void addqueue(int *rear, int item) { if (*rear == (MAXSIZE-1)) { printf("The storage is full\n"); return; } *rear = *rear + 1; entry[*rear] = item; } int deletequeue(int *front, int rear) { if (*front == rear) { printf("The storage is empty\n"); return; } *front = *front + 1; return (entry[*front]); }
SEEM 3460
12
A sample complete C program for queue (con’t) int main () { int front, rear; int randnum, i, j; /* using current time to initalize a random seed for the function rand() */
srand((unsigned)time(NULL)); randnum = rand()%MAXSIZE; /* generate a random number between 0 to MAXSIZE-1 */ printf("randnum=%d\n",randnum); createqueue(&front, &rear); for (i=0; i ./queue random=2 The queue: 0 1 The element removed from the queue is: 0 cuse93:> ./queue Random=0 The queue has no element cuse93:>
SEEM 3460
… compile ...execute
14
Header Files • Sometimes, we can put the global constant, global variables, and function prototype declarations in a separate file called header file. • It allows convenient sharing of data structures or functions in the future development /* queueh.h */ #define MAXSIZE 100 int entry[MAXSIZE]; void createqueue(int*, int*); void addqueue(int*, int); int deletequeue(int*, int);
SEEM 3460
15
The C program for queue with the header file /* queueh.c */ #include #include #include “queueh.h” void createqueue(int *front, int *rear) { *rear = -1; *front = -1; } /* front refers to the position just before the first element rear refers to the position of the last element */ : :
The remaining C codes are the same as before
SEEM 3460
16
Compilation and Execution cuse93:> gcc –o queueh queueh.c cuse93:> ./queueh
SEEM 3460
… compile ...execute
17
Problems for the Simple Queue Data Structure • There are problems for the previous design of the queue data structure: • There may still be a lot of available space when the rear index of the queue reaches near the end of the array • Leading to impractical usage of the queue front
rear 21 15
SEEM 3460
18
Implementation 2: regard an array as a circular queue front: one position counter-clockwise from the first element rear: current end EMPTY QUEUE [3]
[2]
[2]
[3] J2
[1]
[4]
[0]
[5]
J3
[1] J1
[4]
[0]
front = 0 rear = 0
[5]
front = 0 rear = 3
Figure : Empty and nonempty circular queues SEEM 3460
19
one space is left when queue is full FULL QUEUE
[2]
[3] J2
[1]
FULL QUEUE
[2]
[3] J8
J3
J9
J4 [4][1] J7
J1
J6
J5 [0]
[4]
[5]
[0]
front =0 rear = 5
J5 [5]
front =4 rear =3
Figure : Full circular queues and then we remove the item SEEM 3460
20
Add to a circular queue void addq(int front, int *rear, element item) { /* add an item to the queue */ if (front = = ((*rear+1)%MAX_QUEUE_SIZE)) return queue_full(); *rear = (*rear +1) % MAX_QUEUE_SIZE; entry[*rear] = item; }
SEEM 3460
21
Delete from a circular queue element deleteq(int* front, int rear) { /* remove front element from the queue and return it */ if (*front = = rear) return queue_empty( ); /* queue_empty returns an error key */ *front = (*front+1) % MAX_QUEUE_SIZE; return entry[*front]; }
SEEM 3460
22