Objectives of this lecture
What is Queue?
Create_queue
– make queue logically accessible
Destroy_queue
– make queue logically inaccessible
Empty_queue
– checks if a queue is empty
Full_queue
– checks if a queue is full
enqueue - add item at the rear of the queue
dequeue
– remove item from the front of the queue.
Implementation of Queue using Array
|
|
|
|
|
|
A |
B |
|
|
|
|
|
B |
|
|
|
|
A |
B |
C |
D |
E |
|
|
|
C |
D |
E |
|
G |
|
C |
D |
E |
F |
G |
H |
C |
D |
E |
F |
rear=front
(same as empty queue).
This problem can
be solved if we have another integer field, queue_count to keep track of the
number of elements in the queue.
However, a more
common solution is to leave one cell free and let front point to it. With this approach, the empty and full queue
are as follows:
Empty Queue
|
|
|
|
|
|
Full Queue
G |
|
C |
D |
E |
F |
With this
understanding, we can implement queue as follws:
The user_inteface:
#define
MAX_Q 100
typedef
char ITEM_TYPE;
typedef
enum { FALSE, TRUE} BOOLEAN;
typedef
struct { ITEM_TYPE item[MAX_Q];
int front; /* always point to cell prior to front */
int rear; /* always point
to the rear item */
} Q_TYPE;
void create_queue(Q_TYPE *queue);
void destroy_queue(Q_TYPE *queue);
BOOLEAN empty_queue(Q_TYPE *queue);
BOOLEAN full_queue(Q_TYPE *queue);
void enqueue(Q_TYPE *queue, ITEM_TYPE
new_item);
void dequeue(Q_TYPE *queue, ITEM_TYPE
*front_item);
Implementation details
#include "queue.h"
/* initializes queue by setting rear and
front to 0 */
void create_queue(Q_TYPE *queue)
{ queue->front=0;
queue->rear=0;
}
/* resets queue by resetting rear and front
to zero */
void destroy_queue(Q_TYPE *queue)
{ queue->front = 0;
queue->rear = 0;
}
/* returns true if front and rear are the
same */
BOOLEAN empty_queue(Q_TYPE *queue)
{ if (queue->rear == queue->front)
return TRUE;
else
return FALSE;
}
/* returns true if rear+1 is same as front */
BOOLEAN full_queue(Q_TYPE *queue)
{ if ((queue->rear+1)%MAX_Q
==queue->front)
return TRUE;
else
return FALSE;
}
/* adds item to the rear of the queue */
void enqueue(Q_TYPE *queue, ITEM_TYPE
new_item)
{
queue->rear++;
/* we need to take modulo MAX_Q in case
rear is now bigger than MAX_Q */
queue->rear = queue->rear%MAX_Q;
queue->item[queue->rear]=new_item;
}
/*
removes item from the front of the queue */
void dequeue(Q_TYPE *queue, ITEM_TYPE
*front_item)
{ /* we need to add 1 to fromt to reach the
front item */
queue->front++;
/* we need to take modulo MAX_Q in case
front is now bigger than
MAX_Q */
queue->front = queue->front%MAX_Q;
*front_item =
queue->item[queue->front];
}
Example:
#include
<stdio.h>
#include
"queue.c"
void
input_queue(Q_TYPE *queue);
void
output_queue (Q_TYPE *queue);
main()
{ Q_TYPE queue;
create_queue(&queue);
input_queue(&queue);
output_queue(&queue);
destroy_queue(&queue);
return 0;
}
void
input_queue(Q_TYPE *queue)
{ ITEM_TYPE in_value;
printf("\nEnter a string of
caharacters: ");
while (((in_value=getchar()) != '\n')
&&
(full_queue(queue)==FALSE))
enqueue(queue,in_value);
}
void
output_queue (Q_TYPE *queue)
{ ITEM_TYPE out_value;
printf("\nThe string in order is:
");
while ( empty_queue(queue)== FALSE)
{
dequeue(queue,&out_value);
putchar(out_value);
}
}