|
| 1 | +Queue |
| 2 | +==== |
| 3 | +A queue can be defined as an ordered list which enables insert operations to be performed at one end called REAR and delete operations to be performed at another end called FRONT. Queue is referred to be as First In First Out list. |
| 4 | +For example, people waiting in line for a rail ticket form a queue. |
| 5 | + |
| 6 | +     |
| 7 | +> A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops. |
| 8 | +
|
| 9 | +Queue Representation |
| 10 | +--- |
| 11 | + |
| 12 | +     |
| 13 | + |
| 14 | +Applications Of Queue |
| 15 | +--- |
| 16 | +There are various applications of queues few of them are discussed below. |
| 17 | +- Queues are widely used as waiting lists for a single shared resource like printer, disk, CPU. |
| 18 | +- Queues are used in asynchronous transfer of data (where data is not being transferred at the same rate between two processes) for eg. pipes, file IO, sockets. |
| 19 | +- Queues are used as buffers in most of the applications like MP3 media player, CD player, etc. |
| 20 | +- Queue are used to maintain the play list in media players in order to add and remove the songs from the play-list. |
| 21 | +- Queues are used in operating systems for handling interrupts. |
| 22 | + |
| 23 | +Basic Operations Of Queue |
| 24 | +--- |
| 25 | +A queue is an object (an abstract data structure - ADT) that allows the following operations: |
| 26 | +- Enqueue: Add an element to the end of the queue |
| 27 | +- Dequeue: Remove an element from the front of the queue |
| 28 | +- IsEmpty: Check if the queue is empty |
| 29 | +- IsFull: Check if the queue is full |
| 30 | +- Peek: Get the value of the front of the queue without removing it |
| 31 | + |
| 32 | +Algorithm and Code |
| 33 | +--- |
| 34 | +> ### peek() |
| 35 | +This function helps to see the data at the front of the queue. The algorithm of peek() function is : |
| 36 | + |
| 37 | +#### Algorithm |
| 38 | +``` |
| 39 | +begin procedure peek |
| 40 | +return queue[front] |
| 41 | +end procedure |
| 42 | +``` |
| 43 | + |
| 44 | +Implementation of peek() function in C programming language : |
| 45 | + |
| 46 | +#### Example |
| 47 | +``` |
| 48 | +int peek() { |
| 49 | + return queue[front]; |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +> ### isfull() |
| 54 | +
|
| 55 | +Algorithm of isfull() function : |
| 56 | +#### Algorithm |
| 57 | +``` |
| 58 | +begin procedure isfull |
| 59 | +
|
| 60 | + if rear equals to MAXSIZE |
| 61 | + return true |
| 62 | + else |
| 63 | + return false |
| 64 | + endif |
| 65 | + |
| 66 | +end procedure |
| 67 | +``` |
| 68 | +Implementation of isfull() function in C programming language : |
| 69 | +#### Example |
| 70 | +``` |
| 71 | +bool isfull() { |
| 72 | + if(rear == MAXSIZE - 1) |
| 73 | + return true; |
| 74 | + else |
| 75 | + return false; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +> ### isempty() |
| 80 | +If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty. |
| 81 | +Algorithm of isempty() function : |
| 82 | + |
| 83 | +#### Algorithm |
| 84 | +``` |
| 85 | +begin procedure isempty |
| 86 | +
|
| 87 | + if front is less than MIN OR front is greater than rear |
| 88 | + return true |
| 89 | + else |
| 90 | + return false |
| 91 | + endif |
| 92 | + |
| 93 | +end procedure |
| 94 | +``` |
| 95 | + |
| 96 | +Implementation of isempty() function in C programming language : |
| 97 | + |
| 98 | +#### Example |
| 99 | +``` |
| 100 | +bool isempty() { |
| 101 | + if(front < 0 || front > rear) |
| 102 | + return true; |
| 103 | + else |
| 104 | + return false; |
| 105 | +} |
| 106 | +``` |
| 107 | +> ### enqueue() |
| 108 | +The following steps should be taken to enqueue (insert) data into a queue : |
| 109 | +- Step 1 − Check if the queue is full. |
| 110 | +- Step 2 − If the queue is full, produce overflow error and exit. |
| 111 | +- Step 3 − If the queue is not full, increment rear pointer to point the next empty space. |
| 112 | +- Step 4 − Add data element to the queue location, where the rear is pointing. |
| 113 | +- Step 5 − return success. |
| 114 | + |
| 115 | +Algorithm of enqueue() function : |
| 116 | + |
| 117 | +#### Algorithm |
| 118 | +``` |
| 119 | +procedure enqueue(data) |
| 120 | + |
| 121 | + if queue is full |
| 122 | + return overflow |
| 123 | + endif |
| 124 | + |
| 125 | + rear ← rear + 1 |
| 126 | + queue[rear] ← data |
| 127 | + return true |
| 128 | + |
| 129 | +end procedure |
| 130 | +``` |
| 131 | + |
| 132 | +Implementation of enqueue() function in C programming language : |
| 133 | + |
| 134 | +#### Example |
| 135 | +``` |
| 136 | +int enqueue(int data) |
| 137 | + if(isfull()) |
| 138 | + return 0; |
| 139 | + |
| 140 | + rear = rear + 1; |
| 141 | + queue[rear] = data; |
| 142 | + |
| 143 | + return 1; |
| 144 | +end procedure |
| 145 | +``` |
| 146 | + |
| 147 | +> ### dequeue() |
| 148 | +The following steps are taken to perform dequeue operation : |
| 149 | +- Step 1 − Check if the queue is empty. |
| 150 | +- Step 2 − If the queue is empty, produce underflow error and exit. |
| 151 | +- Step 3 − If the queue is not empty, access the data where front is pointing. |
| 152 | +- Step 4 − Increment front pointer to point to the next available data element. |
| 153 | +- Step 5 − Return success. |
| 154 | + |
| 155 | +Algorithm of dequeue() function : |
| 156 | +#### Algorithm |
| 157 | +``` |
| 158 | +procedure dequeue |
| 159 | + |
| 160 | + if queue is empty |
| 161 | + return underflow |
| 162 | + end if |
| 163 | +
|
| 164 | + data = queue[front] |
| 165 | + front ← front + 1 |
| 166 | + return true |
| 167 | +
|
| 168 | +end procedure |
| 169 | +``` |
| 170 | +Implementation of dequeue() in C programming language : |
| 171 | +#### Example |
| 172 | +``` |
| 173 | +int dequeue() { |
| 174 | + if(isempty()) |
| 175 | + return 0; |
| 176 | +
|
| 177 | + int data = queue[front]; |
| 178 | + front = front + 1; |
| 179 | +
|
| 180 | + return data; |
| 181 | +} |
| 182 | +``` |
| 183 | +--- |
| 184 | +For a complete Queue program in C programming language. Please check out above code files. |
0 commit comments