Operating systems keep several queues at once, for example one per priority level of processes. As with multiple stacks (Unit 4), these queues can share one array instead of each having its own.
Segments
Intuition
A post office has one long counter divided into three windows: stamps, parcels and money orders. Each window has its own line within its own part of the hall.
An array mqueue[0..m-1] is divided into k k k equal segments of n = m / k n = m/k n = m / k cells; queue i i i (numbered from 0) uses cells i ⋅ n i \cdot n i ⋅ n to ( i + 1 ) ⋅ n − 1 (i + 1) \cdot n - 1 ( i + 1 ) ⋅ n − 1 . Each queue has its own indices:
front[i] = one position before the first element of queue i i i ;
rear[i] = position of the last element;
queue i i i is empty when front[i] == rear[i] (initially both are i ⋅ n − 1 i \cdot n - 1 i ⋅ n − 1 );
queue i i i is full when rear[i] reaches the last cell of its segment.
Add x to queue i: if rear[i] is at the end of the segment, report “full”; otherwise increase rear[i] once and store x there.
Delete from queue i: if front[i] == rear[i], report “empty”; otherwise increase front[i] and return mqueue[front[i]]. When the queue becomes empty, both indices can be reset to the start of the segment, so that the segment is reused.
Example 5.5 Three queues in a 12-cell array
Goal. Share 12 cells among 3 queues (4 cells each), add to and delete from them, and fill queue 0.
Program.
C · multi_queue.c Copy #include <stdio.h>
#define M 12 /* size of the shared array */
#define K 3 /* number of queues */
int mqueue[M];
int front[K]; /* one position BEFORE the first element of queue i */
int rear[K]; /* position of the last element of queue i */
int limit[K]; /* last cell of the segment of queue i */
void init( void )
{
int n = M / K; /* cells per queue */
for ( int i = 0 ; i < K; i ++ ) {
front[i] = rear[i] = i * n - 1 ; /* empty: front == rear */
limit[i] = (i + 1 ) * n - 1 ;
}
}
int addmq( int i, int x) /* add x to queue i */
{
if (rear[i] == limit[i])
return 0 ; /* segment of queue i is full */
mqueue[ ++ rear[i]] = x; /* increment ONCE, then store */
return 1 ;
}
int delmq( int i, int * x) /* delete from queue i */
{
if (front[i] == rear[i])
return 0 ; /* queue i is empty */
* x = mqueue[ ++ front[i]]; /* front moves FORWARD */
if (front[i] == rear[i]) /* queue i became empty: */
front[i] = rear[i] = i * (M / K) - 1 ; /* reset to the start of the segment */
return 1 ;
}
void show( void )
{
for ( int i = 0 ; i < K; i ++ ) {
printf( "queue %d (cells %d..%d):" , i, i * (M / K), limit[i]);
for ( int k = front[i] + 1 ; k <= rear[i]; k ++ )
printf( " %d" , mqueue[k]);
printf( " \n " );
}
}
int main( void )
{
int x;
init();
addmq( 0 , 1 ); addmq( 0 , 2 ); addmq( 0 , 3 );
addmq( 1 , 10 );
addmq( 2 , 20 ); addmq( 2 , 21 );
show();
printf( "add 4 to queue 0: %s \n " , addmq( 0 , 4 ) ? "ok" : "full" );
printf( "add 5 to queue 0: %s \n " , addmq( 0 , 5 ) ? "ok" : "full" );
delmq( 0 , & x);
printf( "deleted %d from queue 0 \n " , x);
delmq( 1 , & x);
printf( "deleted %d from queue 1 \n " , x);
printf( "delete from queue 1: %s \n " , delmq( 1 , & x) ? "ok" : "empty" );
show();
return 0 ;
}
Run it.
Output Copy queue 0 (cells 0..3): 1 2 3
queue 1 (cells 4..7): 10
queue 2 (cells 8..11): 20 21
add 4 to queue 0: ok
add 5 to queue 0: full
deleted 1 from queue 0
deleted 10 from queue 1
delete from queue 1: empty
queue 0 (cells 0..3): 2 3 4
queue 1 (cells 4..7):
queue 2 (cells 8..11): 20 21
Result. Each queue stays in its own segment; queue 0 reports “full” after 4 items while the other segments still have room, and queue 1 is reset when it becomes empty.
Correction · Programs 5.4 and 5.5 (multiple queues)
Study material says: In Program 5.4 (§5.4), addmq does ++rear[i]; and later rear[i] = rear[i]+1; before storing, and declares int i,x; inside the function body although i and x are its parameters. In Program 5.5, delmq prints “Queue is empty” without else, then reads mqueue[front[i]] and sets front[i] = front[i]-1.
Correct version: Increase rear[i] exactly once, after the “full” check: if (rear[i] == limit[i]) full; else mqueue[++rear[i]] = x;. In deletion, use else after the “empty” message, move front[i] forward (front[i] = front[i] + 1) and then read mqueue[front[i]]. Declare the parameters in the function header: int addmq(int i, int x).
Why: Incrementing rear[i] twice skips one cell for every addition (and the full test compares the first increment with the wrong value). A queue deletes from its front and the front moves towards the rear; decreasing front[i] moves it backwards into the previous queue’s segment. Without else the deletion runs even on an empty queue. Declaring i and x again inside the body hides the parameters, so the function works with uninitialised local variables.
Key idea
Several queues can share one array by giving each its own segment and its own front and rear indices; a queue can then be full while other segments still have space.
TEE Exam note
For “implement multiple queues in an array” draw the array divided into segments with front[i] and rear[i] marked, give the initial values, and write the add and delete algorithms with their full and empty tests. Check Your Progress 2, Q2: “It is not possible to implement multiple queues in an array” is False .
Common mistake
Moving front backwards on deletion. In every queue, both front and rear move in the same direction, towards the end of the segment (or around it, in a circular queue).
Key takeaways
k k k queues in m m m cells: segment i i i = cells i ⋅ n i \cdot n i ⋅ n to ( i + 1 ) n − 1 (i + 1)n - 1 ( i + 1 ) n − 1 with n = m / k n = m/k n = m / k .
front[i] is one before the first element, rear[i] the last; empty when they are equal.
Add: check full, rear[i]++ once, store. Delete: check empty, front[i]++, read.
Fixed segments may be full while the array still has free space.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice 15 cells are shared equally by 3 queues (numbered 0–2). Which cells belong to queue 2?
A 5 to 9 B 11 to 15 C 10 to 14 D 0 to 4
Check answer
Show answer Answer: C. 10 to 14
Each queue gets 15 / 3 = 5 cells: queue 0 uses 0–4, queue 1 uses 5–9, queue 2 uses 10–14.
Question 2 · multiple choice In the scheme above, front[i] is one position before the first element. When is queue i i i empty?
A When front[i] == rear[i] B When rear[i] == limit[i] C When front[i] > rear[i] + 1 D When front[i] == 0
Check answer
Show answer Answer: A. When front[i] == rear[i]
If front[i] is one before the first element and rear[i] is the last, they are equal only when there are no elements. rear[i] == limit[i] is the full test.
Question 3 · multiple choice Which direction does front[i] move when an item is deleted from queue i i i ?
A Backwards (decreases) B It does not move; rear[i] decreases instead C It jumps to the start of the segment D Forwards (increases), towards rear[i]
Check answer
Show answer Answer: D. Forwards (increases), towards rear[i]
The front of a queue follows the items towards the rear. Decreasing it, as Program 5.5 does, moves into another queue’s cells.
Question 4 · multiple choice What does this program print?
C · q5-multi.c Copy #include <stdio.h>
int main( void )
{
int mq[ 8 ], front[ 2 ] = { - 1 , 3 }, rear[ 2 ] = { - 1 , 3 };
mq[ ++ rear[ 0 ]] = 5 ;
mq[ ++ rear[ 1 ]] = 7 ;
mq[ ++ rear[ 1 ]] = 8 ;
int x = mq[ ++ front[ 1 ]];
printf( "%d rear0=%d rear1=%d front1=%d \n " , x, rear[ 0 ], rear[ 1 ], front[ 1 ]);
return 0 ;
} A 8 rear0=0 rear1=5 front1=4 B 7 rear0=0 rear1=5 front1=4 C 5 rear0=0 rear1=5 front1=3 D 7 rear0=1 rear1=5 front1=4
Check answer
Show answer Answer: B. 7 rear0=0 rear1=5 front1=4
Queue 1 holds 7 (cell 4) and 8 (cell 5); deleting moves front[1] from 3 to 4 and returns 7, its first item. Queue 0 holds one item, so rear[0] is 0.
Question 5 · multiple choice “It is not possible to implement multiple queues in an array.” This statement is:
A True, because a queue needs two ends B True, unless the queues are circular C False, but only two queues can share an array D False; any number of queues can share an array using segments
Check answer
Show answer Answer: D. False; any number of queues can share an array using segments
Each queue gets its own segment with its own front and rear (Check Your Progress 2, Q2).
Question 6 · short answer
Write the algorithms to add an element to and delete an element from queue i i i in a multiqueue array.
Show worked answer Let queue i i i occupy cells from start[i] to limit[i], with front[i] = rear[i] = start[i] - 1 initially. ADD(i, x): (1) if rear[i] == limit[i], print “queue i is full” and stop; (2) rear[i] = rear[i] + 1; (3) mqueue[rear[i]] = x. DELETE(i): (1) if front[i] == rear[i], print “queue i is empty” and stop; (2) front[i] = front[i] + 1; (3) x = mqueue[front[i]]; (4) if front[i] == rear[i], reset front[i] = rear[i] = start[i] - 1; (5) return x. Both are O ( 1 ) O(1) O ( 1 ) .