-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.c
More file actions
79 lines (67 loc) · 1.39 KB
/
Stack.c
File metadata and controls
79 lines (67 loc) · 1.39 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
#include <stdio.h>
#include <stdlib.h>
typedef struct node* Pos;
typedef Pos Stack;
int IsEmpty( Stack S );
Stack CreateStack();
void MakeEmpty( Stack S );
void Pop( Stack S );
int Top( Stack S );
void Push( int X, Stack S );
void DisposeStack( Stack S );
struct node{
int Element;
Pos Next;
};
int main() {
return 0;
}
int IsEmpty( Stack S ){
return S->Next == NULL;
}
Stack CreateStack(){
Stack S= (Stack)malloc(sizeof(struct node));
if( S == NULL )
printf("Out of space!");
S->Next = NULL;
MakeEmpty( S );
return S;
}
void MakeEmpty( Stack S ){
if( S == NULL )
printf("Out of space!");
else
while( !IsEmpty( S ))
Pop(S);
}
void Pop( Stack S ){
Pos firstCell;
if( IsEmpty( S ) )
printf("Empty stack" );
else{
firstCell = S->Next;
S->Next = S->Next->Next;
free(firstCell);
}
}
int Top( Stack S ){
if( !IsEmpty( S ) )
return S->Next->Element;
printf("Empty stack");
return 0;
}
void Push( int X, Stack S ){
Pos temp;
temp = ( Stack )malloc( sizeof( struct node ) );
if( temp == NULL)
printf("Out of space!");
else{
temp->Element = X;
temp->Next = S->Next;
S->Next = temp;
}
}
void DisposeStack( Stack S ){
MakeEmpty( S );
free( S );
}