Repository navigation
Expand file tree
/
Copy path3_QueueMax.c
More file actions
102 lines (93 loc) · 1.56 KB
/
Copy path3_QueueMax.c
File metadata and controls
102 lines (93 loc) · 1.56 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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1;
int rear = -1;
int isFull() {
return rear == MAX - 1;
}
int isEmpty() {
return front == -1 || front > rear;
}
void enqueue(int item) {
if (isFull()) {
printf("Queue overflow\n");
return;
}
if (front == -1 && rear == -1) {
front = 0;
}
queue[++rear] = item;
printf("Enqueued: %d\n", item);
}
void dequeue() {
if (isEmpty()) {
printf("Queue underflow\n");
return;
}
int item = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front+=1;
}
printf("Dequeued: %d\n", item);
}
void display() {
if (isEmpty()) {
printf("Queue is Empty\n");
return;
}
printf("Queue: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear)
break;
i = i + 1;
}
printf("\n");
}
void maxValue() {
int i = front;
int index, max=queue[front];
while (1) {
if (queue[i]>max) {
index = i;
max = queue[i];
}
if (i == rear) {
break;
}
i++;
}
printf("Max Value: %d\nIndex: %d\n", max, index);
}
int main() {
int choice, item;
while (1) {
printf("\n1: Enqueue\n2: Dequeue\n3: Max\n4: Display\n0: Exit\n");
printf("Enter choice[1,2,3,4,0]: ");
scanf("%d", &choice);
if (choice == 0) {
break;
} else if (choice == 1) {
printf("Enter value to enqueue: ");
scanf("%d", &item);
enqueue(item);
display();
} else if (choice == 2) {
dequeue();
display();
} else if (choice == 3) {
maxValue();
display();
} else if (choice == 4) {
display();
} else {
printf("ERROR: Invalid choice\n");
}
}
return 0;
}