2. In computer science, a queue is a linear data structure where the components are put into one end
and removed from the other end according to the "first-in, first-out" (FIFO) principle. This data
structure can be utilized for controlling an action sequence or storing data. C is a computer language
with queue capability incorporated in the form of arrays or linked lists.
Characteristics:
• A queue is a type of linear data structure that can be constructed with an array or a linked list.
• Elements are relocated to the back of the queue while being removed from the front.
• Enqueue (add an element to the back) and dequeue (remove an element from the front) are two
queue operations.
• When elements are added and removed often, a queue might be built as a circular queue to
prevent wasting memory.
3. A Queue is defined as a linear data structure that is open at both ends and
the operations are performed in First In First Out (FIFO) order.
4.
5. Using Array:
To implement a queue in C using an array, first define the queue's maximum size and declare an
array of that size. The front and back integers were respectively set to 1. The front variable
represents the front element of the queue, and the back variable represents the back element.
Before pushing the new element to the final position of the queue, we need to increase the back
variable by 1. The queue is now full and no other additional elements can be added when the back
position is reaching the queue's maximum capacity. We add an element to the front of the queue
and increase the front variable by one only to remove an element from the queue. If the front and
rear positions are equal and no more components can be deleted, hence we can say that the
queue is empty.
Below is an instance of a queue written in C that makes use of an array:
6. #define MAX_SIZE 100
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
void enqueue(int element) {
if (rear == MAX_SIZE - 1) {
printf("Queue is full");
return;
}
if (front == -1) {
front = 0;
}
7. rear++;
queue[rear] = element;
}
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty");
return -1;
}
int element = queue[front];
9. The difference between stacks and queues is in removing. In a stack we remove the item the most
recently added; in a queue, we remove the item the least recently added.
Implement Queue using array
10.
11.
12. CIRCULAR QUEUE:
Why was the concept of the circular queue introduced?
There was one limitation in the array implementation of Queue. If the rear reaches to the end
position of the Queue then there might be possibility that some vacant spaces are left in the
beginning which cannot be utilized. So, to overcome such limitations, the concept of the circular
queue was introduced.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34. Implementation of circular queue using linked list:
As we know that linked list is a linear data structure that stores two parts, i.e., data
part and the address part where address part contains the address of the next
node. Here, linked list is used to implement the circular queue; therefore, the linked
list follows the properties of the Queue. When we are implementing the circular
queue using linked list then both the enqueue and dequeue operations
take O(1) time.
35.
36.
37.
38.
39.
40. Priority Queue:
A priority queue is a special type of queue in which each element is associated
with a priority value. And, elements are served on the basis of their priority. That
is, higher priority elements are served first.
However, if elements with the same priority occur, they are served according to
their order in the queue.
Assigning Priority Value:
Generally, the value of the element itself is considered for assigning the priority.
For example,
The element with the highest value is considered the highest priority element.
However, in other cases, we can assume the element with the lowest value as the
highest priority element.
We can also set priorities according to our needs.
41.
42. Difference between Priority Queue and Normal Queue:
In a queue, the first-in-first-out rule is implemented whereas, in a priority queue, the
values are removed on the basis of priority. The element with the highest priority is
removed first.
Implementation of Priority Queue
Priority queue can be implemented using an array, a linked list, a heap data structure, or a
binary search tree. Among these data structures, heap data structure provides an efficient
implementation of priority queues.
Hence, we will be using the heap data structure to implement the priority queue in this
tutorial. A max-heap is implemented in the following operations. If you want to learn more
about it, please visit max-heap and min-heap.
43.
44. Priority Queue Operations:
Basic operations of a priority queue are inserting, removing, and peeking elements.
1. Inserting an Element into the Priority Queue
Inserting an element into a priority queue (max-heap) is done by the following
steps.
• Insert the new element at the end of the tree.