-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackar.c
More file actions
96 lines (79 loc) · 1.27 KB
/
stackar.c
File metadata and controls
96 lines (79 loc) · 1.27 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
//数组实现的栈
#include "fatal.h"
#include <stdlib.h>
#define EmptyTOS (-1)
#define MinStackSize ( 5 )
struct StackRecoard
{
int Capacity;
int TopOfStack;
ElementType * Array;
};
int IsEmpty(Stack S)
{
return S->TopOfStack == EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity - 1;
}
Stack CreateStack(int MaxElements)
{
Stack S;
if(MaxElements < MinStackSize)
Error("Stack size is too small");
S =malloc(sizeof(struct StackRecoard));
if (S == NULL)
FatalError("Out of space!!!");
S->Array = malloc(sizeof(ElementType) * MaxElements);
if (S->Array == NULL)
FatalError("Out of Space");
S->Capacity = MaxElements;
MakeEmpty(S);
return S;
}
void DisposeStack(Stack S)
{
if(S != NULL)
{
free(S->Array);
free(S);
}
}
void MakeEmpty(Stack S)
{
if(S != NULL)
{
S->TopOfStack = EmptyTOS;
}
}
void Push(ElementType X, Stack S)
{
if(IsFull(S))
Error("Full Stack");
else
S->Array[++S->TopOfStack] = X;
}
ElementType Top(Stack S)
{
if(!IsEmpty(S))
return S->Array[S->TopOfStack];
Error("empty stack");
return 0;
}
void Pop(Stack S)
{
if(IsEmpty(S))
Error("Empty stack");
else
S->TopOfStack--;
}
ElementType TopAndPop(Stack S)
{
if(!IsEmpty(S))
{
return S->Array[S->TopOfStack--];
}
Error("Empty Stack");
return 0;
}