-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem38.c
More file actions
130 lines (114 loc) Β· 2.97 KB
/
problem38.c
File metadata and controls
130 lines (114 loc) Β· 2.97 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STACK_SIZE 100005
#define MAX_LABEL_LENGTH 21
#define HASH_SIZE 10007
typedef struct Node {
char label[MAX_LABEL_LENGTH];
int count;
struct Node* next;
} Node;
Node* hashTable[HASH_SIZE];
int hash(const char* str) {
int h = 0;
while (*str) {
h = (h * 31 + *str++) % HASH_SIZE;
}
return h;
}
int getCount(const char* label) {
int h = hash(label);
Node* curr = hashTable[h];
while (curr) {
if (strcmp(curr->label, label) == 0) {
return curr->count;
}
curr = curr->next;
}
return 0;
}
void incrementCount(const char* label) {
int h = hash(label);
Node* curr = hashTable[h];
while (curr) {
if (strcmp(curr->label, label) == 0) {
curr->count++;
return;
}
curr = curr->next;
}
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->label, label);
newNode->count = 1;
newNode->next = hashTable[h];
hashTable[h] = newNode;
}
void decrementCount(const char* label) {
int h = hash(label);
Node* curr = hashTable[h];
Node* prev = NULL;
while (curr) {
if (strcmp(curr->label, label) == 0) {
curr->count--;
if (curr->count == 0) {
if (prev) prev->next = curr->next;
else hashTable[h] = curr->next;
free(curr);
}
return;
}
prev = curr;
curr = curr->next;
}
}
char stack[MAX_STACK_SIZE][MAX_LABEL_LENGTH];
int top = -1;
typedef struct {
char type[10];
char value[MAX_LABEL_LENGTH];
} Command;
Command commands[MAX_STACK_SIZE];
int output[MAX_STACK_SIZE];
int outputCount = 0;
int main() {
int n;
printf("Enter number of operations: ");
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++) {
char line[40];
fgets(line, sizeof(line), stdin);
line[strcspn(line, "\n")] = 0;
if (strncmp(line, "PUSH", 4) == 0) {
strcpy(commands[i].type, "PUSH");
sscanf(line + 5, "%s", commands[i].value);
} else {
strcpy(commands[i].type, line);
}
}
for (int i = 0; i < n; i++) {
if (strcmp(commands[i].type, "PUSH") == 0) {
strcpy(stack[++top], commands[i].value);
incrementCount(commands[i].value);
} else if (strcmp(commands[i].type, "POP") == 0) {
if (top >= 0) {
decrementCount(stack[top--]);
}
} else if (strcmp(commands[i].type, "COUNT") == 0) {
if (top == -1) {
output[outputCount++] = -1;
} else {
output[outputCount++] = getCount(stack[top]);
}
}
}
printf("\nOutput:\n");
for (int i = 0; i < outputCount; i++) {
if (output[i] == -1)
printf("EMPTY\n");
else
printf("%d\n", output[i]);
}
return 0;
}