Analogies Using Concept of Queues
Analogies Using Concept of Queues
inserted first is the first one to be taken out. The elements in a queue are added at one end called
the REAR and removed from the other end called the FRONT.
Analogies using concept of queues.
People waiting for a bus. The first person standing in the line will be the first one to get
into the bus.
Cars lined at a toll bridge. The first car to reach the bridge will be the first to leave.
Taking a Tickets in the Cinema Theater Queue. The First Person will collect the Ticket
and enter into the Hall.
Implementation of Queues
IMP: the problem with queues is that unless the queue is made fully empty, we cannot use the
queue fully. To overcome this problem circular queues logic is implemented.
---------------------------------------------------------------------------------------------------------
Algorithm :
procedure qInsert(element)
//insert item into the queue represented in Q(l:n)//
if rear = MAX-1 then // if queue is full
call QUEUE_FULL;
Else // queue is not full
rear rear + 1; //forward rear pointer by 1.
Q(rear)element; // store element at rear end
End If;
End ADDQ;
procedure qDelete()
//delete an element from queue
if front=-1 and rear=-1 then //if queue empty
call Queue is Empty;
else if front==rear //if rear and front both are equal
set rear =-1 and front=-1
else //if queue is not empty
front<-front+1; //forward front pointer by 1
End if;
end Dequeue;
import java.util.Scanner;
class Queue1{
int que[],front,rear;
Queue1(){
que=new int[5];
front=rear=0;
}
void qDelete() {
if(front != rear ){
System.out.println("deleting "+que[front]);
front++;
}
else{
System.out.println("Queue is empty");
front=rear=0;
}
}
void disp() {
for(int i=front;i<rear;i++)
System.out.print(que[i]+" ");
System.out.println();
}
}
public class MyQue {
public static void main(String[] args) {
Queue1 q=new Queue1();
q.qInsert(1);
q.qInsert(2);
q.qInsert(3);
q.qInsert(4);
q.qInsert(5);
q.disp();
q.qInsert(6);
q.qDelete();
q.qDelete();
q.disp();
}
}
---------------------------------------------------------------------------------------------------------
Output :
inserting 1
inserting 2
inserting 3
inserting 4
inserting 5
1 2 3 4 5
que is full
deleting 1
deleting 2
3 4 5