-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArrayUseStruct.c
More file actions
85 lines (67 loc) · 1.43 KB
/
StackArrayUseStruct.c
File metadata and controls
85 lines (67 loc) · 1.43 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
#include <stdio.h>
#include <stdlib.h>
#define MINSTACKSIZE 5
typedef struct StackRecord* Stack;
Stack CreateStack(int size);
void Push(int x,Stack S);
int Top (Stack S);
void Pop(Stack S);
int IsEmpty(Stack S);
int IsFull(Stack S);
void MakeNull(Stack S);
void DisposeStack(Stack S);
struct StackRecord {
int Top;
int Capacity;
int *Array;
};
int main (){
}
Stack CreateStack(int size){
Stack S;
if( size < MINSTACKSIZE )
printf("Stack size is too small");
S = (Stack) malloc( sizeof( struct StackRecord ) );
if( S == NULL )
printf("Out of space!!!");
S->Array = (int*) malloc( sizeof(int) * size );
if( S->Array == NULL )
printf("Out of space!!!");
S->Top = -1;
S->Capacity = size ;
return S ;
}
void Push(int x,Stack S){
if(IsFull( S ))
printf("Full stack");
else
S->Array[++S->Top] = x;
}
int Top (Stack S){
if(IsEmpty(S))
printf("Empty stack");
else
return S->Array[S->Top];
return -1;
}
void Pop(Stack S){
if(IsEmpty(S))
printf("Empty stack");
else
S->Top--;
}
int IsEmpty(Stack S){
return S->Top == -1 ;
}
int IsFull(Stack S){
return S->Top == S->Capacity-1;
}
void MakeNull(Stack S){
S->Top = -1;
}
void DisposeStack(Stack S) {
if (S != NULL) {
free(S->Array);
free(S);
}
}