0% found this document useful (0 votes)
3 views

linear queue one dimensional array

This document presents an implementation of a linear queue using a one-dimensional array in C. It includes code for enqueueing, dequeueing, and displaying queue elements, along with a main function to interact with the user. The program allows users to perform operations until they choose to exit.

Uploaded by

Kedar Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

linear queue one dimensional array

This document presents an implementation of a linear queue using a one-dimensional array in C. It includes code for enqueueing, dequeueing, and displaying queue elements, along with a main function to interact with the user. The program allows users to perform operations until they choose to exit.

Uploaded by

Kedar Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No.

Name – Siddhesh Shivaji Kumbhar Roll No – 100

Implementation of Linear Queue through One Dimensional


Array
Code -
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

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

void enqueue(int value) {


if (rear == MAX_SIZE - 1) {
printf("Queue is full\n");
return;
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("%d enqueued to the queue.\n", value);
}

int dequeue() {
if (front == -1) {
printf("Queue is empty\n");
1
Assignment No.4

return -1;
}
int value = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front++;
}
return value;
}

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

int main() {
int choice, value;

while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
2
Assignment No.4

switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1) {
printf("%d dequeued from the queue.\n", value);
}
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

3
Assignment No.4

OUTPUT –

You might also like