-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
72 lines (66 loc) · 918 Bytes
/
Stack.cpp
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
#include "Stack.h"
#include <malloc.h>
struct Node {
ElementType Element;
PtrToNode Next;
};
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
Stack CreateStack(void)
{
Stack S = (Stack)malloc(sizeof(struct Node));
if (S != NULL)
S->Next = NULL;
return S;
}
void DisposeStack(Stack S);
void MakeEmpty(Stack S)
{
S = S->Next;
PtrToNode temp;
while (S)
{
temp = S->Next;
free(S);
S = temp;
}
}
void Push(ElementType X, Stack S)
{
PtrToNode temp = (PtrToNode)malloc(sizeof(struct Node));
if (temp != NULL)
{
temp->Element = X;
temp->Next = S->Next;
S->Next = temp;
}
}
ElementType Top(Stack S)
{
if (!IsEmpty(S))
{
return S->Next->Element;
}
return 0;
}
void Pop(Stack S)
{
if (!IsEmpty(S))
{
PtrToNode temp = S->Next;
S->Next = temp->Next;
free(temp);
}
}
void DisposeStack(Stack S)
{
PtrToNode temp = S;
while (S!=NULL)
{
temp = S->Next;
free(S);
S = temp;
}
}