-
Notifications
You must be signed in to change notification settings - Fork 2
/
question2.c
67 lines (58 loc) · 1.2 KB
/
question2.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Implement a queue using circular array
For pushing:
Time complexity:O(1)
For popping:
Time complexity:O(1)
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void push(int arr[], int *front, int *rear, int elm){
*rear = (*rear+1)%MAX; //modulus after incrementing is imp.
if(*rear == *front){
printf("Overflow\n");
if(*rear == 0){
*rear = MAX - 1;
}else{
*rear = *rear-1;
}
return;
}
arr[*rear] = elm;
return;
}
int pop(int arr[], int *front, int *rear){
if(*front == *rear){ //this is where we started from when both were pointing to zero
return -1;
}
*front = (*front + 1)%MAX; //modulus after incrementing is imp.
int data = arr[*front];
return data;
}
int main(){
int arr[MAX];
int front,rear = 0, step, elm;
while(1){
printf("1. PUSH element\n");
printf("2. POP element\n");
printf("3.EXIT \n");
scanf("%d",&step);
switch(step){
case 1: printf("Enter the number to be pushed\n");
scanf("%d",&elm);
push(arr, &front, &rear, elm);
break;
case 2: elm = pop(arr,&front,&rear);
if(elm == -1){
printf("Already empty\n");
}else{
printf("%d was popped\n", elm);
}
break;
case 3: exit(1);
break;
}
}
return 0;
}