Skip to content

Instantly share code, notes, and snippets.

@Ajay-Raj-S
Created September 18, 2019 18:22
Show Gist options
  • Save Ajay-Raj-S/0bf9a3656f5bcb4552b013d213b7b472 to your computer and use it in GitHub Desktop.
Save Ajay-Raj-S/0bf9a3656f5bcb4552b013d213b7b472 to your computer and use it in GitHub Desktop.
Queue using Array in C program.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int front = 0;
int last = 0;
int queue[MAX];
void enqueue(int val) {
queue[last++] = val;
printf("Value added to the queue\n");
}
int dequeue() {
int temp = queue[front];
front++;
return temp;
}
int checkFull() {
if(last == MAX-1) {
printf("Queue is Full!\n");
return 0;
}
return 1;
}
int checkEmpty() {
if(front == last) {
printf("Queue is Empty!\n");
return 0;
}
return 1;
}
void display() {
int i=front;
int j=last;
for(;i<j;i++) {
printf("-> %d\n", queue[i]);
}
}
int main() {
int choice = 0;
int value = 0;
while(1) {
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n >>> ");
scanf("%d", &choice);
switch(choice) {
case 1:
if(checkFull()) {
printf("Enter the Number to Enqueue >> ");
scanf("%d", &value);
enqueue(value);
}
break;
case 2:
if(checkEmpty()) {
printf("Dequeued element is %d\n", dequeue());
}
break;
case 3:
if(front != last) {
display();
}
break;
case 4:
exit(0);
break;
default:
printf("Wrong Choice!\n");
exit(0);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment