-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.c
120 lines (105 loc) · 2.71 KB
/
test1.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
// Structure to represent a queue
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
// Function to create an empty queue
void createQueue(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
// Function to check if the queue is full
int isFull(struct Queue *q) {
if (q->rear == MAX_SIZE - 1)
return 1;
else
return 0;
}
// Function to check if the queue is empty
int isEmpty(struct Queue *q) {
if (q->front == -1)
return 1;
else
return 0;
}
// Function to add an element to the queue
void enqueue(struct Queue *q, int value) {
if (isFull(q)) {
printf("Queue is full. Cannot enqueue element.\n");
} else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// Function to remove an element from the queue
int dequeue(struct Queue *q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty. Cannot dequeue element.\n");
return -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = -1;
q->rear = -1;
}
return item;
}
}
// Function to count duplicates in the queue
int countDuplicates(struct Queue *q) {
int count = 0;
int visited[MAX_SIZE] = {0}; // Array to keep track of visited elements
// Iterate through the queue
for (int i = q->front; i <= q->rear; i++) {
visited[q->items[i]]++;
if (visited[q->items[i]] > 1) {
count++;
}
}
return count;
}
// Function to find the maximum element in the queue
int findMax(struct Queue *q) {
int max = q->items[q->front];
for (int i = q->front + 1; i <= q->rear; i++) {
if (q->items[i] > max) {
max = q->items[i];
}
}
return max;
}
// Function to count the occurrences of the maximum element in the queue
int countMaxOccurrences(struct Queue *q) {
int max = findMax(q);
int count = 0;
for (int i = q->front; i <= q->rear; i++) {
if (q->items[i] == max) {
count++;
}
}
return count;
}
int main() {
struct Queue q;
createQueue(&q);
int size, element;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &element);
enqueue(&q, element);
}
int duplicates = countDuplicates(&q);
int maxOccurrences = countMaxOccurrences(&q);
printf("Number of duplicates in the queueis: %d\n", duplicates);
printf("Number of occurrences of the maximum element: %d\n", maxOccurrences);}