CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
Computer Engineering Data Structure Queue.pptx
1. Queue:
A queue, also called FIFO system, is a linear list in which deletions can
take place only at one end of the list, the Front of the list and
insertion can take place only at the other end Rear.
2. Implementation of Queue by using array:
front = -1
rear = -1
A
Insert (A)
front = 0
rear = 0
A B
Insert (B)
front = 0
rear = 1
A B C
Insert (C)
front = 0
rear = 2
B C
Delete
front = 1
rear = 2
C
Delete
front = 2
rear = 2
3. QINSERT(QUEUE, N, FRONT, REAR, ELE)
1. If REAR = N-1 then
Write : OVERFLOW, and Return
2. If FRONT = -1 then
Set FRONT = 0 and REAR = 0
Else
Set REAR = REAR+1
3. Set QUEUE[REAR] = ELE
4. Return
int add(struct queue *q, int ele)
{
if (q->rear == Max-1)
return(0);
if (q->front == -1)
{
q->front = 0;
q->rear = 0;
}
else
q->rear++;
q->item[q->rear]=ele;
return (1);
}
struct queue
{
int item[Max];
int front,rear;
};
4. QDELETE(QUEUE, N, FRONT, REAR, ELE)
1. If FRONT = -1 or FRONT > REAR and then
Write : UNDERFLOW, and Return
2. Set ELE = QUEUE[FRONT]
3. Set FRONT = FRONT+1
4. Return
int del(struct queue *q, int *ele)
{
if (q->front ==-1 || q->front > q->rear)
return (0);
*ele=q->item[q->front];
q->front++;
return(1);
}
5. With this implementation there is one drawback. After Max
insertions, the last element will occupy the last position of the array.
This occurs even though the queue itself may not contain any
elements.
Suppose we want to insert an element into a queue when there are
some empty locations at the beginning of the array. One way is to
simply move the entire queue to the beginning of the array. This
procedure may be very expensive.
Another way is by assuming that the array is circular, that is, That
QUEUE[0] comes after QUEUE[Max-1] in the array.
0 1 Max-1
6. CQINSERT(QUEUE, N, FRONT, REAR, ITEM)
1. If FRONT = 0 and REAR =N-1 or if FRONT=REAR +1 then
Write : OVERFLOW, and Return
2. If FRONT = -1 then
Set FRONT = 0 and REAR = 0
Else if REAR = N-1, then
Set REAR = 0
Else
Set REAR = REAR+1
3. Set QUEUE[REAR] = ITEM
4. Return
7. CQDELETE(QUEUE, N, FRONT, REAR, ITEM)
1. If FRONT = -1 then
Write : UNDERFLOW, and Return
2. Set ITEM = QUEUE[FRONT]
3. If FRONT = REAR then
Set FRONT = -1 and REAR = -1
Else if FRONT = N-1, then
Set FRONT = 0
Else
Set FRONT = FRONT+1
4. Return