그 외 공부/Algorithm
# 3_Stack
ssangeun
2017. 11. 5. 17:32
# The source code
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 |
#include<stdio.h>
#include<stdlib.h>
#define MAX_SZ 10
typedef int boolean;
int true = 1;
int false = 0;
int stack[MAX_SZ];
int top = - 1;
boolean isEmpty()
{
if (top == -1)
{
return true;
}
return false;
}
boolean isFull()
{
if (top == MAX_SZ - 1)
{
return true;
}
return false;
}
int pop()
{
if (isEmpty() == true)
{
printf("fail to pop \n");
return -999;
}
else
{
int v = stack[top];
top--;
return v;
}
}
int push(int v)
{
if (isFull() == true)
{
printf("fail to push \n");
return -999;
}
else
{
top++;
stack[top] = v;
return 1;
}
}
void printStack()
{
for (int i = 0; i <= top; i++)
{
printf("%d ", stack[i]);
}
}
void main()
{
push(1);
push(2);
push(3);
push(4);
push(5);
printf("Stack after pushing 1, 2, 3, 4, 5 : ");
printStack();
printf("\n");
pop();
pop();
pop();
printf("Stack after popping 3 times : ");
printStack();
} |
cs |
# The result