-
Notifications
You must be signed in to change notification settings - Fork 2
/
MasterStack.cpp
85 lines (70 loc) · 1.52 KB
/
MasterStack.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
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <stdlib.h>
using namespace std;
/* Konstanta */
#define Nil NULL
/* Deklarasi infotype */
typedef int infotype;
/* Stack dengan representasi berkait dengan pointer */
typedef struct tElmtStack * address;
typedef struct tElmtStack {
infotype Info;
address Next;
} ElmtStack;
/* Type stack dengan ciri TOP : */
typedef struct {
address TOP; /* alamat TOP: elemen puncak */
} Stack;
/* Selektor */
#define Top(S) (S).TOP
#define InfoTop(S) (S).TOP->Info
#define Next(P) (P)->Next
#define Info(P) (P)->Info
/* ----- Prototype Manajemen Memori ----- */
void Alokasi (address * P, infotype X) {
/* Kamus Lokal */
/* Algoritma */
(*P) = (address) malloc (sizeof (ElmtStack));
if ((*P) != Nil) {
Info(*P) = X;
Next(*P) = Nil;
}
}
void Dealokasi (address P) {
/* Kamus Lokal */
/* Algoritma */
Next(P)=Nil;
}
/* ********* PROTOTYPE REPRESENTASI LOJIK STACK ***************/
bool IsEmpty (Stack S) {
/* Kamus Lokal */
/* Algoritma */
return (Top(S) == Nil);
}
void CreateEmpty (Stack * S) {
/* Kamus Lokal */
/* Algoritma */
Top(*S) = Nil;
}
/* ----- Operator Dasar Stack ----- */
void Push (Stack * S, infotype X) {
/* Kamus Lokal */
address P;
/* Algoritma */
Alokasi(&P,X);
if (P != Nil) {
Next(P) = Top(*S);
Top(*S) = P;
}
}
void Pop (Stack * S, infotype * X) {
/* Kamus Lokal */
address P;
/* Algoritma */
P = Top(*S);
*X= Info(P);
if (!IsEmpty(*S)) {
Top(*S)=Next(P);
Dealokasi(P);
}
}