🌳
B.TECH I YEAR β€’ II SEMESTER β€’ R25

Data Structures - Lab Manual

Data Structures Laboratory β€’ C Programming Solution

πŸ“˜ R25 Regulation πŸŽ“ I Year πŸ“š II Semester πŸ’» C Programming πŸ§ͺ Lab Program
Selected Program

C Program to implement Queue (its operations) using Array

← All Lab Programs

C Program to implement Queue (its operations) using Array

Queue using Array
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5

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

// Function to add an element to the queue
void enqueue(int element) 
{
    if (rear == MAX_SIZE - 1) 
	{
        printf("Queue Overflow\n");
    } 
	else 
	{
		if(front == -1) 
			front = 0;
		queue[++rear] = element;
    }
}

// Function to remove an element from the queue
int dequeue() 
{
	int item;
    if (front == -1) 
	{
		printf("Queue Underflow\n");
        return -1; // Indicating underflow
    } 
	else 
	{
        int item = queue[front++];
        if (front > rear)   // Queue is now empty
		{ 
            front = -1;
            rear = -1;
        } 
        return item;
    }
}

// Function to display all the items from Queue
void display()
{
    int i;
    if (front == -1) 
	{
		printf("Queue is Empty\n");
    } 
    else
    {
        for(i=front;i<=rear;i++)
            printf("%d\t",queue[i]);
    }
}

// Main function
int main() {
    int ch,data;
    do{
        printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
        printf("\nEnter your choice:   ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: printf("Enter data to insert:  ");
                    scanf("%d",&data);
                    enqueue(data);
                    break;
            case 2: printf("Deleted: %d\n", dequeue());
                    break;
            case 3: display();
                    break;
            case 4: break;
            default: printf("your choice is wrong!..");
        }
    }while(ch!=4);
    return 0;
}

OUTPUT
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  10
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  20
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  30
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
10	20	30	
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  40
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  50
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
10	20	30	40	50	
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   1
Enter data to insert:  60
Queue Overflow

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Deleted: 10

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Deleted: 20

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Deleted: 30

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
40	50	
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Deleted: 40

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Deleted: 50

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   2
Queue Underflow
Deleted: -1

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   3
Queue is Empty

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:   4

πŸ’» Run This C Program

C Program to implement Queue (its operations) using Array
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
← Back to Data Structures Lab Programs πŸ“š All Lab Manuals