-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
113 lines (92 loc) · 1.73 KB
/
main.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
//
// main.c
// BaekJun Algorithm 44(10828, 스택)
// https://www.acmicpc.net/problem/10828
// Created by hyungwook on 2021/03/27.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int max;
int ptr;
int *stk;
} IntStack;
int Initialize(IntStack *s, int max)
{
s->max = max;
s->ptr = 0;
s->stk = malloc(sizeof(int) * max);
return 0;
}
int Push(IntStack *s, int x)
{
if(s->max <= s->ptr)
return -1;
s->stk[s->ptr] = x;
s->ptr++;
return 0;
}
int Pop(IntStack *s)
{
int x;
if(s->ptr == 0)
return -1;
s->ptr--;
x = s->stk[s->ptr];
return x;
}
int Size(IntStack *s)
{
return s->ptr;
}
int Isempty(IntStack *s)
{
if(s->ptr == 0)
return 1;
else
return 0;
}
int Top(IntStack *s)
{
int x;
if(s->ptr == 0)
return -1;
x = s->stk[s->ptr - 1];
return x;
}
int main(int argc, const char * argv[]) {
// insert code here...
IntStack s;
int n;
int x;
char command[10];
Initialize(&s, 10000);
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", command);
if(strcmp(command, "push") == 0)
{
scanf("%d", &x);
Push(&s, x);
}
else if(strcmp(command, "pop") == 0)
{
printf("%d\n", Pop(&s));
}
else if(strcmp(command, "size") == 0)
{
printf("%d\n", Size(&s));
}
else if(strcmp(command, "empty") == 0)
{
printf("%d\n", Isempty(&s));
}
else if(strcmp(command, "top") == 0)
{
printf("%d\n", Top(&s));
}
}
return 0;
}