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

Queue Program

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)
7 views3 pages

Queue Program

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/ 3

C Program: Queue Implementation

#include<stdio.h>
#include<stdlib.h>
#define max 20

struct queue {
int arr[max];
int front;
int rear;
};

void intialise(struct queue*val) {


val->front = -1;
val->rear = -1;
}

int isempty(struct queue*val) {


if (val->front == -1 && val->rear == -1) {
return 1;
}
else {
return 0;
}
}

int isfull(struct queue*val) {


if (val->rear == max - 1) {

return 1;
}
else {

return 0;
}
}

void enqueue(struct queue*val, int value) {


if (isfull(val)) {
printf("Queue is overflowed");
return;
}
if (isempty(val)) {
val->front = 0;
val->rear = 0;
} else {
val->rear++;
}
val->arr[val->rear] = value;
}

int dequeue(struct queue*val) {


if (isempty(val)) {
printf("Queue is underflowed\n");
return -1;
}
int ans = val->arr[val->front];
if (val->front == val->rear) {
val->front = -1;
val->rear = -1;
} else {
val->front++;
}
return ans;
}

int peek(struct queue*val) {


if (isempty(val)) {
printf("Queue is empty\n");
return -1;
}
return val->arr[val->front];
}

void display(struct queue*val) {


if (isempty(val)) {
printf("Queue is empty\n");
return;
}
for (int i = val->front; i <= val->rear; i++) {
printf("%d ", val->arr[i]);
}
printf("\n");
}

int main() {
struct queue val;
intialise(&val);
int choice, value;

printf("------INDEX-------\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Is Empty\n");
printf("4. Is Full\n");
printf("5. Peek value\n");
printf("6. Display\n");
printf("Enter the choice number: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the value: ");
scanf("%d", &value);
enqueue(&val, value);
break;

case 2:
value = dequeue(&val);
if (value != -1)
printf("Dequeued element is: %d\n", value);
break;

case 3:
if (isempty(&val))
printf("Queue is empty\n");
else
printf("Queue is not empty\n");
break;

case 4:
if (isfull(&val))
printf("Queue is full\n");
else
printf("Queue is not full\n");
break;

case 5:
value = peek(&val);
if (value != -1)
printf("Peek element is: %d\n", value);
break;

case 6:
display(&val);
break;

default:
printf("Invalid choice!\n");
}
return 0;
}

You might also like