Sei sulla pagina 1di 3

import javax.swing.

JOptionPane;
public class StackCar {

String s[];
int ukuran,top;

public StackCar(int size){


s=new String[size];
ukuran=size;
top=-1;
}

public void push (String x) {

if(isFull()){
JOptionPane.showMessageDialog(null, "ERROR : Stack Full");
}
else{
top++;
s[top]=x;
}
}

public String pop(){


if(isEmpty()){
JOptionPane.showMessageDialog(null, "ERROR : Stack Empty");
return "";
}else{
String hasil;
hasil=s[top--];
return hasil;
}
}

public boolean isEmpty(){


if(top==-1)
return true;
else
return false;
}

public boolean isFull(){


if(top==ukuran-1)
return true;
else
return false;
}
}

========================================================================

import javax.swing.JOptionPane;
public class StackCar2 {

public static void main(String[] args) {


String menu="MENU STACK int 10 elemen \n"+
"1. Push data \n"+
"2. Pop data \n"+
"3. Pop sampai habis \n"+
"4. Keluar \n"+
"pilihan anda :";

String input,x = "";


int pilih;

StackCar jerry = new StackCar(10);

do {
input=JOptionPane.showInputDialog(menu);
pilih=Integer.valueOf(input);

String s[] = new String [10];

int y = -1;

for (int i = y; i >= 0; i--) {


jerry.push( s[i]);
}

switch(pilih){
case 1:
String masuk=JOptionPane.showInputDialog("Masukan Data : ");
jerry.push(masuk);

while (jerry.isEmpty() != true) {

String ludji = jerry.pop();


x += " " +""+ ludji;
y++;
s[y] = ludji;
}
for (int i = y; i >= 0; i--) {
jerry.push( s[i]);
}

JOptionPane.showMessageDialog(null, "Push Data "+x);


x = "";
break;
case 2:
x = jerry.pop();
JOptionPane.showMessageDialog(null, "Pop data "+x);
break;
case 3:
while(!jerry.isEmpty()){
x=(String) jerry.pop();
JOptionPane.showMessageDialog(null, "Pop data "+x);
}
break;
case 4:
JOptionPane.showMessageDialog(null, "TERIMA KASIH");
break;
default :
JOptionPane.showMessageDialog(null,"ERROR : Pilih 1-4 saja");
break;
}
}
while(pilih!=4);
}
}

Potrebbero piacerti anche