[go: up one dir, main page]

0% found this document useful (0 votes)
7 views7 pages

Ravi 4

The document provides a C program that implements a Linear Queue Abstract Data Type (ADT) using an array. It includes functionalities for insertion, deletion, display, and checking the front element of the queue. The program handles queue overflow and underflow conditions and demonstrates its usage with example operations.

Uploaded by

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

Ravi 4

The document provides a C program that implements a Linear Queue Abstract Data Type (ADT) using an array. It includes functionalities for insertion, deletion, display, and checking the front element of the queue. The program handles queue overflow and underflow conditions and demonstrates its usage with example operations.

Uploaded by

yashtambe3610
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical Number:-4

Aim:- Implement Linear Queue ADT using array.

#include<stdio.h>

#define n 5

int main()

int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;

printf("Queue using Array");

printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");

while(ch)

printf("\nEnter the Choice:");

scanf("%d",&ch);

switch(ch)

case 1:

if(rear==x)

printf("\n Queue is Full");

else

printf("\n Enter no %d:",j++);

scanf("%d",&queue[rear++]);

break;
case 2:

if(front==rear)

printf("\n Queue is empty");

else

printf("\n Deleted Element is %d",queue[front++]);

x++;

break;

case 3:

printf("\nQueue Elements are:\n ");

if(front==rear)

printf("\n Queue is Empty");

else

for(i=front; i<rear; i++)

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

printf("\n");

break;

case 4:

exit(0);
default:

printf("Wrong Choice: please see the options");

return 0;

OUTPUT:
#include <stdio.h>

#define MAX_SIZE 10

// Queue and its front and rear

int queue[MAX_SIZE];

int front = -1, rear = -1;

// Function to enqueue an element to the queue

void enqueue(int value) {

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

printf("Queue Overflow!\n");

return;

} else if (front == -1) {

front = rear = 0;

queue[rear] = value;

} else if (rear == MAX_SIZE - 1 && front != 0) {

rear = 0;

queue[rear] = value;

} else {

rear++;

queue[rear] = value;

}
// Function to dequeue an element from the queue

int dequeue() {

int data;

if (front == -1) {

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

return -1;

data = queue[front];

if (front == rear) {

front = -1;

rear = -1;

} else if (front == MAX_SIZE - 1) {

front = 0;

} else {

front++;

return data;

// Function to get the front element of the queue

int peek() {

if (front == -1) {

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

return -1;
}

return queue[front];

// Function to display the queue

void display() {

if (front == -1) {

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);

printf("Front element: %d\n", peek());

display();

printf("Dequeued element: %d\n", dequeue());

display();

return 0;
}

Output:

You might also like