In data structures, a queue is a linear data structure that follows the "First-In, First-Out" (FIFO) principle. This means that the first element added to the queue is the first one to be removed. Think of it like a queue of people waiting in line, where the person who joins the line first is the one who gets served first.

Key characteristics of a queue include:
Enqueue: The process of adding an element to the back or end of the queue is called enqueue.
Dequeue: The process of removing an element from the front or head of the queue is called dequeue.
Front: The front of the queue is where elements are dequeued from.
Rear: The rear of the queue is where elements are enqueued to.
Empty: A queue is considered empty when it contains no elements.
Size: It represents the number of elements currently in the queue.
Operation on queue
Algorithm
1 − START
2 – Check if the queue is full.
3 − If the queue is full, produce overflow error and exit.
4 − If the queue is not full, increment rear pointer to point the next empty space.
5 − Add data element to the queue location, where the rear is pointing.
6 − return success.
7 – END
In this algorithm, queue is an array that stores the elements of the queue. rear is a pointer to the rear element of the queue. The enqueue operation first checks if the queue is full. If it is, the operation prints an error message and returns. Otherwise, the operation increments the rear pointer and stores the new item at the rear index of the array.
Dequeue: The dequeue operation removes an item from the queue. The algorithm for dequeue is as follows:
Algorithm
1 – START
2 − Check if the queue is empty.
3 − If the queue is empty, produce underflow error and exit.
4 − If the queue is not empty, access the data where front is pointing.
5 − Increment front pointer to point to the next available data element.
6 − Return success.
7 – END
In this algorithm, queue is an array that stores the elements of the queue. front is a pointer to the front element of the queue. The dequeue operation first checks if the queue is empty. If it is, the operation prints an error message and returns -1. Otherwise, the operation increments the front pointer and returns the element at the front index of the array.
Program
#include <stdio.h>
#define MAX 100
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue(int item) {
if (rear == MAX - 1) {
printf("Queue overflow\n");
return;
}
rear++;
queue[rear] = item;
}
int dequeue() {
if (front == rear) {
printf("Queue underflow\n");
return -1;
}
front++;
return queue[front];
}
int peek() {
if (front == rear) {
printf("Queue underflow\n");
return -1;
}
return queue[front];
}
void printQueue() {
if (front == rear) {
printf("Queue is empty\n");
return;
}
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
int main() {
int choice, item;
do {
printf("\n\nQueue operations\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("4. Print\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the item to enqueue: ");
scanf("%d", &item);
enqueue(item);
break;
case 2:
item = dequeue();
if (item != -1) {
printf("Dequeued item: %d\n", item);
}
break;
case 3:
item = peek();
if (item != -1) {
printf("Front item: %d\n", item);
}
break;
case 4:
printQueue();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
}
} while (1);
}
No comments:
Post a Comment