-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
57 lines (51 loc) · 1006 Bytes
/
stack.h
File metadata and controls
57 lines (51 loc) · 1006 Bytes
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
//j1422 �y�� stack.h
stack_type stack[stack_size];
int sp;
//スタックの初期化
void init_stack()
{
sp = -1;
}
//スタックにデータを格納
void push(stack_type d)
{
if( sp < stack_size-1)//スタックサイズより少なかったら
{
stack[++sp] = d;//+1してから追加
}
else//等しいか多かったら
{
fprintf(stderr, "Error : stack full \n");//スタックがいっぱい
exit(1);
}
}
//スタック内のデータを返す
int stack_stock()
{
//spの初期値は-1なので+1する
return sp+1;
}
//スタック内のデータを取り出す
stack_type pop()
{
if( sp >= 0)//スタック内に値があったら
{
return stack[sp--];//取り出してから-1する
}
else
{
fprintf(stderr, "Error : stack empty \n");//スタックが空
exit(1);
}
}
//スタック内の値を出力
void print_stack()
{
int i;
printf("stack data : ");
for(i=0;i<=sp;i++)//spの値のぶん繰り返す
{
printf("%d ",stack[i]);
}
printf("\n");
}