Survey							
                            
		                
		                * Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Data Structures and Algorithms
Linear Data Structure – Queue
1. Introduction of Queue
2. Operations on Queue
a. remove operation (dequeue)
b. insert operation (enqueue)
c. isEmpty operation
3. Representing Queue in C
4. Implementation of Operations
5. Priority Queue
a. Ascending Priority Queue
b. Descending Priority Queue
Introduction of Queue
Definition [ Queue ]
A queu is an ordered collection of items from which item may be deleted at one end called the
front end of the queue and into which item may be inserted at the other end called the rear of
the queue.
The fist element inserted into a queue is the first element to be removed. For this reason a
queue is sometimes called FIFO (First In First Out).
Operations on Queue
There are three operations which can be applied to the queue.
•
•
•
Insert (enqueue) operation: Enqueue means adding a new item to the rear of the queue.
Remove (dequeue) operation: Dequeue refers to removing the front item from queue and
returning it.
isEmpty operation: The isEmpty operation checks whether the queue is empty or not. If it
is empty, the function returns true otherwise false.
Theoretically, one characteristic of a queue is that it does not have a specific capacity.
Regardless of how many elements are already contained, a new element can always be added. It
can also be empty, at which point removing an element will be impossible until a new element
has been added again.
A practical implementation of a queue of course does have some capacity limit, that depends on
the concrete situation it is used in. For a data structure the executing computer will eventually run
out of memory, thus limiting the queue size. Queue overflow results from trying to add an
element onto a full queue and queue underflow happens when trying to remove an element from
an empty queue.
Representing Queue in C
The following C code represents the Queue.
#define SIZE 10
typedef struct
{
int front, rear;
int items[SIZE];
}Queue;
Page 1 of 2
Prepared by: Manoj Shakya
Data Structures and Algorithms
void initQueue(Queue &q){ // initialize a queue
q->front = SIZE - 1;
q->rear = SIZE – 1;
}
Implementation of Operations
Implementing various operations including remove and insert.
int isEmpty(Queue *q){
if(q->front == q->rear)
return 1;
return 0;
}
int remove(Queue *q){
if(isEmpty(q) == 1){ // queue is empty
printf("Queue Underflow.");
exit(1);
}
else{
if(q->front == SIZE -1)
q->front = 0;
else
q->front++;
}
return ( q->items[q->front] );
}
void insert(Queue *q, int data){
if(q->front - q->rear == 1){
printf("Queue Overflow");
exit(1);
}
else{
if(q->rear == SIZE-1)
q->rear = 0;
else
q->rear++;
}
q->items[q->rear] = data;
}
Priority Queue
Definition [ Priority Queue ]
The priority queue is a data structure in which the intrinsic ordering of the elements does
determine the results of the basic operations.
There are two types of priority queue.
• Ascending Priority Queue
• Descending Priority Queue
An ascending priority queue is a collection of items into which items can be inserted arbitrarily
and from which only the smallest item can be removed.
A descending priority queue is similar but allows deletion of only the largest item.
Page 2 of 2
Prepared by: Manoj Shakya