-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
100 lines (98 loc) · 2.51 KB
/
stack.cpp
File metadata and controls
100 lines (98 loc) · 2.51 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
// stack.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
#include "stdafx.h"
#define ST 10000
int stack[ST] = {0};
void push(int);
void empty();
int top();
void pop();
void size();
int main()
{
string inputS;
int pushVal;
cout << "push, empty, top, pop, size \n";
while (1) {
cin >> inputS;
if (inputS == "push")
{
cin >> pushVal;
push(pushVal);
}
else if (inputS == "empty")
{
empty();
}
else if (inputS == "top")
{
cout << top() <<"\n";
}
else if (inputS == "pop")
{
pop();
}
else if (inputS == "size")
{
size();
}
else if (inputS == "esc")
{
return 0;
}
}
}
void push(int pushVal)
{
for (int i = 0; i < ST; i++)
{
if (*(stack + i) == 0)
{
*(stack + i) = pushVal;
break;
}
}
}
void empty()
{
if (stack[0] == NULL)
{
cout << "1\n";
}
else
{
cout << "0\n";
}
}
int top()
{
if (*stack== 0)
{
return -1;
}
else
return stack[0];
}
void pop()
{
for (int i = 0; i < ST; i++)
{
if (*(stack + i) == 0)
{
cout << *(stack + i - 1) << "\n";
*(stack + i - 1) = NULL;
break;
}
}
}
void size()
{
for (int i = 0; i < ST; i++)
{
if (*(stack + i) == 0)
{
cout << i << "\n";
break;
}
}
}