- By Sonali Soni
Introduction to Queue.
Operations on Queue.
Representation of Queue in memory.
Application of Queue.
Queue is an ordered group of homogeneous
items of elements.
A queue has two ends - front end & rear
end.
Insertion can take place at rear end &
deletion can take place at front end.
Queue is also known as First In First
Out(FIFO) data structure.
Insertion & Deletion operations are known
as Enqueue & Dequeue respectively.
Front Rear
The following operations are performed on
queue:-
1. CreateQueue(q)- To create q as an empty queue.
2. Enqueue(q, i)- To insert an element i in the
queue.
3. Dequeue(q)- To access & remove an element of
the queue.
4. Peek(q)- To access the first element of the
queue without removing it.
5. IsFull(q)- to check whether the queue is full.
6. IsEmpty(q)- To check whether the queue is
empty.
Queue can be represented in memory using linear
array or linear linked list.
A queue represented using a linked list is called a
linked queue.
The overflow condition for array implementation is
front is equal to zero & rear is equal to (size of
array)-1.
The underflow condition for array implementation is
front is equal to -1.
typedef struct nodetype
{
int info;
struct nodetype *next;
}node;
typedef struct
{
node *front;
node *rear;
}queue;
We define two datatypes- node &
queue.
The datatype queue, a structure
whose first element front holds the
address of first element of queue &
second element rear holds the
address of last element of the
queue.
front rear
5 10 20 0
CREATING AN EMPTY QUEUE
void CreateQueue(queue *pq)
{
pq->front=pq->rear=NULL;
}
The front and rear end of the queue are
initialized to NULL.
typedef enum {true,false}boolean;
boolean IsEmpty(queue *pq)
{
if(pq->front==NULL)
return true;
else
return false;
}
void Enqueue(queue *pq, int item)
{
node *ptr;
ptr=(node *)malloc(sizeof(node));
ptr->info=item;
ptr->next=NULL;
if(pq->rear==NULL)
pq->front=pq->rear=ptr;
else
{
(pq->rear)->next=ptr;
pq->rear=ptr;
}
}
int Dequeue(queue *pq)
{
int temp;
node *ptr;
temp=(pq->front)->info;
ptr=pq->front;
if(pq->front==pq->rear)
pq->front=pq->rear=NULL;
else
pq->front=(pq->front)->next;
free(ptr);
return temp;
}
Queue is used to access files from a
disk system.
In a multiprogramming environment,
queue is used for CPU scheduling or
job scheduling of operating system.
In ticket reservation system, queue
can be used for issuing tickets to the
customers.
Queue

Queue

  • 1.
  • 2.
    Introduction to Queue. Operationson Queue. Representation of Queue in memory. Application of Queue.
  • 3.
    Queue is anordered group of homogeneous items of elements. A queue has two ends - front end & rear end. Insertion can take place at rear end & deletion can take place at front end.
  • 4.
    Queue is alsoknown as First In First Out(FIFO) data structure. Insertion & Deletion operations are known as Enqueue & Dequeue respectively. Front Rear
  • 5.
    The following operationsare performed on queue:- 1. CreateQueue(q)- To create q as an empty queue. 2. Enqueue(q, i)- To insert an element i in the queue. 3. Dequeue(q)- To access & remove an element of the queue. 4. Peek(q)- To access the first element of the queue without removing it. 5. IsFull(q)- to check whether the queue is full. 6. IsEmpty(q)- To check whether the queue is empty.
  • 6.
    Queue can berepresented in memory using linear array or linear linked list. A queue represented using a linked list is called a linked queue. The overflow condition for array implementation is front is equal to zero & rear is equal to (size of array)-1. The underflow condition for array implementation is front is equal to -1.
  • 7.
    typedef struct nodetype { intinfo; struct nodetype *next; }node; typedef struct { node *front; node *rear; }queue;
  • 8.
    We define twodatatypes- node & queue. The datatype queue, a structure whose first element front holds the address of first element of queue & second element rear holds the address of last element of the queue.
  • 9.
  • 10.
    CREATING AN EMPTYQUEUE void CreateQueue(queue *pq) { pq->front=pq->rear=NULL; } The front and rear end of the queue are initialized to NULL.
  • 11.
    typedef enum {true,false}boolean; booleanIsEmpty(queue *pq) { if(pq->front==NULL) return true; else return false; }
  • 12.
    void Enqueue(queue *pq,int item) { node *ptr; ptr=(node *)malloc(sizeof(node)); ptr->info=item; ptr->next=NULL; if(pq->rear==NULL) pq->front=pq->rear=ptr; else { (pq->rear)->next=ptr; pq->rear=ptr; } }
  • 13.
    int Dequeue(queue *pq) { inttemp; node *ptr; temp=(pq->front)->info; ptr=pq->front; if(pq->front==pq->rear) pq->front=pq->rear=NULL; else pq->front=(pq->front)->next; free(ptr); return temp; }
  • 14.
    Queue is usedto access files from a disk system. In a multiprogramming environment, queue is used for CPU scheduling or job scheduling of operating system. In ticket reservation system, queue can be used for issuing tickets to the customers.