0% found this document useful (0 votes)
6 views2 pages

DSA Lab 8

The document describes a lab assignment to implement a circular queue in C, including functions for enqueue, dequeue, and display. It provides the code for managing the queue, handling cases for full and empty states. The main function demonstrates adding and removing elements from the queue while displaying the current state of the queue.

Uploaded by

Ñákúl Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

DSA Lab 8

The document describes a lab assignment to implement a circular queue in C, including functions for enqueue, dequeue, and display. It provides the code for managing the queue, handling cases for full and empty states. The main function demonstrates adding and removing elements from the queue while displaying the current state of the queue.

Uploaded by

Ñákúl Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab Assignment 8

1. Write a program to implement circular queue and perform enqueue and dequeue

#include <stdio.h>

int queue[5];

int front = -1, rear = -1;

int SIZE=5;

void enqueue(int value) {

if ((front == 0 && rear == SIZE - 1) || (rear + 1) % SIZE == front) {

printf("Queue is full\n");

return; }

if (front == -1) {

front = rear = 0;

} else {

rear = (rear + 1) % SIZE; }

queue[rear] = value;

printf("Element added: %d\n", value); }

void dequeue() {

if (front == -1) {

printf("Queue is empty!\n");

return; }

printf("Element deleted: %d\n", queue[front]);

if (front == rear) {

front = rear = -1;

} else {

front = (front + 1) % SIZE; }}

void display() {

if (front == -1) {

printf("Queue is empty!\n");

return; }

printf("Elements in queue: \n");


int i = front;

while (1) {

printf("%d\n", queue[i]);

if (i == rear)

break;

i = (i + 1) % SIZE; }}

void main() {

enqueue(10);

enqueue(20);

enqueue(30);

enqueue(40);

enqueue(50);

display();

dequeue();

dequeue();

display();

enqueue(60);

enqueue(70);

display(); }

Output:

You might also like