-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
79 lines (75 loc) · 1.69 KB
/
stack.cpp
File metadata and controls
79 lines (75 loc) · 1.69 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 "stack.h"
void createStack(stack &S){
/*I.S. sembarang
F.S. terbentuk stack dengan Top= 0 */
Top(S) = 0;
};
bool isEmpty(stack S){
/*Mengembalikan nilai true jika stackkosong*/
if(Top(S) == 0){
return true;
} else {
return false;
}
};
bool isFull(stack S){
/*Mengembalikan nilai true jika stackpenuh*/
if(Top(S) == 10){
return true;
} else {
return false;
}
};
void push(stack &S,infotype x){
/*I.S. mungkin kosong
F.S. menambahkan elemen pada stack dengannilai x, Top = Top + 1*/
if(isFull(S) == false){
Top(S)++;
info(S)[Top(S)] = x;
}
};
int pop(stack &S){
/*Mengembalikan nilai infotype yang ada pada indeksTop, Top = Top - 1*/
int x;
x = info(S)[Top(S)];
Top(S)--;
return x;
};
void printInfo(stack S){
/* I.S. stack mungkin kosong
F.S. Jika stack tidak kosong, maka menampilkansemua info yang ada pada stack */
for(int i = Top(S); i>=1; i--){
cout<<info(S)[i]<<" ";
}
cout<<endl;
};
void ascending(stack &S){
/*membuat stack terurut ascending*/
int pass = 2;
int i,temp;
while( pass <= Top(S)){
i = pass;
temp = info(S)[pass];
while(i>0 && temp <info(S)[i-1]){
info(S)[i] = info(S)[i-1];
i--;
}
info(S)[i] = temp;
pass++;
}
}
void descending(stack &S){
/*membuat stack terurut descending*/
int pass = 2;
int i,temp;
while( pass <= Top(S)){
i = pass;
temp = info(S)[pass];
while(i>0 && temp >info(S)[i-1]){
info(S)[i] = info(S)[i-1];
i--;
}
info(S)[i] = temp;
pass++;
}
}