-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackt.c
103 lines (95 loc) · 2.86 KB
/
stackt.c
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
97
98
99
100
101
102
103
/* File queues.c */
/* implementasi dari file queues.h */
/* HEAD dan TAIL adalah alamat elemen pertama dan terakhir */
/* Kapasitas Queue=MaxEl bush elemen, dan indeks dibuat "sirkuler" */
#include "stackt.h"
#include <stdio.h>
/* Definisi ABSTRACT DATA TYPE POINT *
Stack S;
/******************* Prototype ****************/
/*** Konstruktor/Kreator ***/
void CreateEmpty(Stack *S){
Top(*S) = Niil;
}
/* T.S. sembarang; */
/* F.S. Membuat sebuah stack S yang kosonq berkapasitas MaxEl */
/* jadi indeksnya antara 1.. MaxEl+l karena O tidak dipakai */
/* Ciri stack kosong : TOP bernilai Nil */
/*********** Predikat Untuk test keadaan KOLEKSI **/
boolean IsEmpty (Stack S){
return Top(S) == Niil;
}
/* Mengirim true jika Stack kosong: lihat definisi di atas */
boolean IsFull(Stack S){
//printf("Top %d Max %d", Top(S),MaxEl);
return Top(S) == MaxEl;
}
/* Mengirim true jika tabel penampung nilai elemen stack penuh */
/*********** Menambahkan sebuah elemen ke Stack **********/
void Push (Stack *S, info X){
if(!IsFull(*S)){
Top(*S) = Top(*S)+1;
InfoTop(*S) = X;
} else {
printf("Sorry Cant Push Cause Stack is Full!");
}
}
/* Menambahkan X sebagai elemen Stack S. */
/* T.S. S mungkin kosong, tabel penampung elemen stack TTDAK penuh */
/* F.S. X menjadi TOP yang baru,TOP bertambah 1 */
/*********** Menqhapus sebuah elemen Stack **********/
void Pop (Stack *S, info *X){
if(!IsEmpty(*S)){
*X = InfoTop(*S);
Top(*S) = Top(*S)-1;
}else{
printf("Cant Pop Cause your Stack is Empty!");
}
}
/* Menghapus X dari Stack S. */
/* T.S. S tidak mungkin kosong */
/* F.S. X adalah nilai elemen TOP yang lama, TOP berkurang l */
/********* Print Stack From Bottom to Top *************/
void printStack(Stack S){
Stack temp;
CreateEmpty(&temp);
info x, f;
int i = 1;
while(!IsEmpty(S)){
Pop(&S, &x);
Push(&temp, x);
}
while(!IsEmpty(temp)){
x = InfoTop(temp);
printf("%d. %s\n", i, x.Nama);
// printf("No. KTP : %s\n", x.ktp);
// printf("Nama : %s\n", x.nama);
// printf("Kategori : %s\n", x.kategori);
// printf("Info Kategori : %s\n", x.info_kategori);
Push(&S, x);
Pop(&temp, &f);
i++;
}
}
int nElem(Stack S){
Stack temp;
CreateEmpty(&temp);
info x, f;
int i = 1;
while(!IsEmpty(S)){
Pop(&S, &x);
Push(&temp, x);
}
while(!IsEmpty(temp)){
x = InfoTop(temp);
//printf("%d. %s\n", i, x.nama);
// printf("No. KTP : %s\n", x.ktp);
// printf("Nama : %s\n", x.nama);
// printf("Kategori : %s\n", x.kategori);
// printf("Info Kategori : %s\n", x.info_kategori);
Push(&S, x);
Pop(&temp, &f);
i++;
}
return i-1;
}