-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation_of_queue_using_array.c
More file actions
38 lines (36 loc) · 1.29 KB
/
implementation_of_queue_using_array.c
File metadata and controls
38 lines (36 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
int front=-1,rear=-1;
int enqueue(int x,int size, int queue[]);
int dequeue();
int display(int queue[]);
int peek(int queue[]);
int main(){
int size; printf("Enter the size of the array: "); scanf("%d", &size); int queue[size]; int choice=1; while(choice!=0){
printf("What do you want to perform?\n1. Enqueue\n2. Dequeue\n3. Display\n4. Peek"); scanf("%d", &choice);
switch(choice){
case 1: printf("Enter the element, that you want to insert: "); int x; scanf("%d", &x); enqueue(x, size, queue);break;
case 2: dequeue(); break;
case 3: display(queue); break;
case 4: peek(queue); break;
default: printf("Invalid Choice: "); break;
}
printf("Do you want to continue? If yes press 1, either press 0: "); scanf("%d", &choice);}
}
int enqueue(int x, int size, int queue[]){
if(rear==size-1) printf("Overflow");
else if(front==-1 && rear==-1) {front=rear=0; queue[rear]=x;}
else {rear++; queue[rear]=x;}
}
int dequeue(){
if(front==-1 && rear==-1) printf("Underflow");
else if(front==rear) front=rear=-1;
else front++;
}
int display(int queue[]){
if(front==-1 && rear==-1) printf("Queue is empty");
else{ int i=front; for(i; i<=rear; i++) printf("%d ", queue[i]);}
}
int peek(int queue[]){
if(front==-1 && rear==-1) printf("Queue is empty");
else printf("%d", queue[front]);
}