0% found this document useful (0 votes)
3 views3 pages

Writeup Exp No 07 Queue Implementation Using Array

Uploaded by

bvpsih0052
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Writeup Exp No 07 Queue Implementation Using Array

Uploaded by

bvpsih0052
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No :- 07

Experiment: Queue Implementation Using Array

Aim

To implement a queue using array and perform enqueue and dequeue operations using a
simple example.

Problem Statement

Write a C program to implement a queue using array and perform operations on the
following example:
Insert elements: 10, 20, 30, 40
Delete one element from the queue.

Theory

A queue is a linear data structure that follows FIFO (First In First Out) principle. This means
the element inserted first is removed first. A common real-life example is a queue at a bus
stop or ticket counter.

In queue, insertion is done from the rear end and deletion is done from the front end. Two
main variables are used: front (points to first element) and rear (points to last element).

Initially, front = -1 and rear = -1. When the first element is inserted, both front and rear
become 0.

Operations in Queue:
1. Enqueue: Insert element at rear.
2. Dequeue: Remove element from front.

Conditions:
Queue Overflow: When rear reaches MAX-1.
Queue Underflow: When queue is empty (front == -1 or front > rear).

Advantages of Array Queue:


- Simple implementation
- Fast access using index

Limitations:
- Fixed size
- Wastage of space after deletions
- This can be improved using circular queue
Applications of Queue:
- CPU scheduling
- Printer queue
- Breadth First Search in graphs
- Handling requests in servers

Algorithm
Enqueue:
1. Check overflow
2. If first element set front=0
3. Increment rear
4. Insert element

Dequeue:
1. Check underflow
2. Remove element
3. Increment front

C Program Code
#include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

void enqueue(int value) {


if (rear == MAX - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1)
front = 0;
queue[++rear] = value;
printf("%d inserted into queue\n", value);
}

void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("%d removed from queue\n", queue[front++]);
}

void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return;
}
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
display();
dequeue();
display();
return 0;
}

Conclusion
Queue is an important data structure that follows FIFO principle. Array implementation is
simple and efficient but has size limitations. The experiment clearly demonstrates enqueue
and dequeue operations.

You might also like