-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_of_stack.c
More file actions
103 lines (100 loc) · 2.31 KB
/
array_of_stack.c
File metadata and controls
103 lines (100 loc) · 2.31 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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct Stack{
int *items;
int capacity;
int top;
}Stack;
typedef struct{
char name[10];
int key;
}hash_;
void initialize(Stack* stack,int capacity){
stack->capacity=capacity;
stack->top=-1;
stack->items=(int*)malloc(capacity*sizeof(int));
}
void push(Stack* stack,int value){
if(stack->top==stack->capacity-1){
printf("stack overflow\n");
}
else{
stack->top++;
stack->items[stack->top]=value;
printf("%d\n",stack->items[stack->top]);
}
}
void pop(Stack* stack){
if(stack->top==-1){
printf("stack underflow\n");
}
else{
printf("%d\n",stack->items[stack->top]);
stack->top--;
}
}
void top(Stack* stack){
if(stack->top==-1){
printf("stack underflow\n");
}
else{
printf("%d\n",stack->items[stack->top]);
}
}
void free_(Stack* stack){
free(stack->items);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int s,q,c;
scanf("%d %d %d",&s,&q,&c);
Stack stack[s];
hash_ hashmap[s];
for(int i=0;i<s;i++){
scanf("%s",hashmap[i].name);
hashmap[i].key=i;
}
for(int i=0;i<s;i++){
initialize(&stack[i],c);
}
while(q--){
char query[10];
char nameof[10];
scanf("%s",query);
if(strcmp(query,"push")==0){
scanf("%s",nameof);
for(int i=0;i<s;i++){
if(strcmp(nameof,hashmap[i].name)==0){
int value;
scanf("%d",&value);
push(&stack[i],value);
}
}
}
if(strcmp(query,"pop")==0){
scanf("%s",nameof);
for(int i=0;i<s;i++){
if(strcmp(nameof,hashmap[i].name)==0){
pop(&stack[i]);
}
}
}
if(strcmp(query,"top")==0){
scanf("%s",nameof);
for(int i=0;i<s;i++){
if(strcmp(nameof,hashmap[i].name)==0){
top(&stack[i]);
}
}
}
}
for(int i=0;i<s;i++){
free_(&stack[i]);
}
}
return 0;
}