-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMINSTACK.c
86 lines (81 loc) · 1.34 KB
/
MINSTACK.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stack{
int top1;
int top2;
int* a;
int* b;
}stack;
void push(int a[],int b[],int val,int *t1,int *t2){
if(*t1==-1 && *t2==-1){
a[++(*t1)]=val;
b[++(*t2)]=val;
}
else{
int k=b[(*t1)];
a[++(*t1)]=val;
if(k>val){
b[++(*t2)]=k;
//printf("%d",b[(*t)]);
}
else{
b[++(*t2)]=val;
}
}
}
void pop(int *t1,int *t2){
if(*t1==-1 && *t2==-1){
return;
}
else{
*t2=*t2-1;
*t1=*t1-1;
}
}
void show(int b[],int* t){
if(*t==-1){
printf("Empty \n");
}
else{
printf("%d \n",b[*t]);
}
}
//void showtop(int)
int main(){
int test;
char c1='A';
char c2="R";
int num=0;
scanf("%d",&test);
while(test--){
num=num+1;
stack *s;
s=(stack*)malloc(sizeof(stack));
s->a = (int*)malloc(sizeof(int)*1000000);
s->b=(int*)malloc(sizeof(int)*10000000);
s->top1=-1;
s->top2=-1;
printf("Case %d:\n",num);
int n;
scanf("%d", &n);
while (n--)
{
char c;
scanf("%s", c);
if (c == c1)
{
int val;
scanf("%d", &val);
push(s->a,s->b,val,&(s->top1),&(s->top2));
}
else if (c== c2)
{
pop(&(s->top1),&(s->top2));
}
else
{
show(s->b,&(s->top2));
}
}
}}