-
Notifications
You must be signed in to change notification settings - Fork 39
/
Stack.java
72 lines (62 loc) · 1.42 KB
/
Stack.java
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
package Minggu9_Stack.Praktikum1;
public class Stack {
int size;
int top;
int[] data;
Stack (int size){
this.size = size;
data = new int [size];
top = -1;
}
boolean IsEmpety(){
if (top == -1){
return true;
}else {
return false;
}
}
boolean IsFull(){
if (top == size-1){
return true;
}else {
return false;
}
}
void push (int dt){
if (!IsFull()){
top++;
data[top] = dt;
}else {
System.out.println("Isi stack penuh");
}
}
void pop (){
if (!IsEmpety()){
int x = data[top];
top--;
System.out.println("Data yang keluar: " + x);
}else {
System.out.println("Stack masih kosong");
}
}
void peek(){
System.out.println("ELemen teratas: " + data[top]);
}
void print(){
System.out.println("Isi stack: ");
for (int i = top; i >=0 ; i--) {
System.out.println(data[i] + " ");
}
System.out.println("");
}
void clear(){
if (!IsEmpety()){
for (int i = top; i >=0 ; i--) {
top--;
}
System.out.println("Stack sudah dikosongkan");
}else {
System.out.println("Gagal! Stack masih kosong");
}
}
}