Circular Queue Implementation Using Arrays in C, Study Guides, Projects, Research of Data Structures and Algorithms

A comprehensive guide to implementing a circular queue using an array in c. It includes the theory behind the circular queue data structure, explaining the first in, first out (fifo) principle. The document details the algorithm for enqueue, dequeue, and display operations, along with c code implementation. It also covers condition checking for queue overflow and underflow, ensuring safe and efficient queue operations. The learning outcomes and course outcomes are clearly defined, making it a valuable resource for understanding and implementing circular queues. This document enhances problem-solving skills by implementing a menu-driven interface that allows dynamic interaction with the queue at runtime. It also teaches how to display and maintain the queue contents after each operation to provide real-time feedback to the user.

Typology: Study Guides, Projects, Research

2024/2025

Uploaded on 08/13/2025

aditya-kumar-barai
aditya-kumar-barai 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Experiment No. 5: Circular Queue Using Array
Aim: Build a Program for Circular Queue using an array (Menu driven program)
Tools: Turbo C++, Launch DOSBox, MS Word
Theory: This program implements a queue data structure using an array in C. A queue follows the First In,
First Out (FIFO) principle, meaning the first element inserted is the first to be removed. The array holds the
queue elements, while two integer variables, front and rear, track the positions from which elements are
dequeued and enqueued respectively. The program offers core operations such as enqueue (to add elements),
dequeue (to remove elements), and display (to show the current elements in the queue), all managed through
a menu-driven interface for ease of use. The enqueue function inserts an element at the rear of the queue,
with checks in place to prevent overflow when the queue is full. The dequeue function removes an element
from the front, ensuring the queue is not empty to avoid underflow. The display function prints the queue
contents from front to rear.
Algorithm:
1. Start.
2. Declare an integer array queue of size 5 and two integer variables front and rear initialized to -1.
3. Repeat the following steps until the user chooses to exit:
a. Display the menu with options: Enqueue, Dequeue, Display, Exit.
b. Read the user’s choice.
c. If the choice is Enqueue:
i. Prompt the user to enter a value.
ii. If rear is equal to the size of the queue (5), then check if front - 1 >= 0.
- If yes, decrement front and insert the value at queue[front].
- Otherwise, display "Queue is Full".
iii. Else if front == rear, increment front by 1, increment rear by 2, and insert the value at
queue[front].
iv. Otherwise, insert the value at queue[rear] and increment rear by 1.
d. If the choice is Dequeue:
i. If front equals rear, display "No Element to dequeue".
ii. Otherwise, increment front by 1 (effectively removing the front element).
e. If the choice is Display:
i. Print elements from queue[front] to queue[rear - 1] in order.
f. If the choice is Exit:
i. Terminate the program.
4. Stop.
Program:
#include <stdio.h>
#define size 5
int queue[size], front = -1, rear = -1;
pf3
pf4
pf5

Partial preview of the text

Download Circular Queue Implementation Using Arrays in C and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Experiment No. 5: Circular Queue Using Array

Aim: Build a Program for Circular Queue using an array (Menu driven program) Tools: Turbo C++, Launch DOSBox, MS Word Theory: This program implements a queue data structure using an array in C. A queue follows the First In, First Out (FIFO) principle, meaning the first element inserted is the first to be removed. The array holds the queue elements, while two integer variables, front and rear, track the positions from which elements are dequeued and enqueued respectively. The program offers core operations such as enqueue (to add elements), dequeue (to remove elements), and display (to show the current elements in the queue), all managed through a menu-driven interface for ease of use. The enqueue function inserts an element at the rear of the queue, with checks in place to prevent overflow when the queue is full. The dequeue function removes an element from the front, ensuring the queue is not empty to avoid underflow. The display function prints the queue contents from front to rear. Algorithm:

  1. Start.
  2. Declare an integer array queue of size 5 and two integer variables front and rear initialized to - 1.
  3. Repeat the following steps until the user chooses to exit: a. Display the menu with options: Enqueue, Dequeue, Display, Exit. b. Read the user’s choice. c. If the choice is Enqueue: i. Prompt the user to enter a value. ii. If rear is equal to the size of the queue (5), then check if front - 1 >= 0.
    • If yes, decrement front and insert the value at queue[front].
    • Otherwise, display "Queue is Full". iii. Else if front == rear, increment front by 1, increment rear by 2, and insert the value at queue[front]. iv. Otherwise, insert the value at queue[rear] and increment rear by 1. d. If the choice is Dequeue: i. If front equals rear, display "No Element to dequeue". ii. Otherwise, increment front by 1 (effectively removing the front element). e. If the choice is Display: i. Print elements from queue[front] to queue[rear - 1] in order. f. If the choice is Exit: i. Terminate the program.
  4. Stop. Program: #include <stdio.h> #define size 5 int queue[size], front = - 1, rear = - 1;

void enqueue(int num) { if (rear == size) { if (front - 1 >= 0) { --front; queue[front] = num; return; } else { printf("\nQueue is Full."); return; } } if (front == rear) { front++; rear += 2; queue[front] = num; } else { queue[rear] = num; rear++; } }

case 1: printf("Enter number to enqueue: "); scanf("%d", &num); enqueue(num); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("Invalid choice."); } } return 0; } Output:

Learning Outcome:  To understand and implement the Queue data structure using an array and manage its operations (enqueue, dequeue, display) following the First In, First Out (FIFO) principle.

For Faculty Use Correction Parameters Formative Assessment [40%] Timely completion of Practical [ 40%] Attendance / Learning Attitude [20%] Marks Obtained