public class cItem {
private String nama;
cItem next, prev;
cItem(String n){
nama=n;
System.out.println("Item "+n+" dibuat...");
}
public String getNama(){
return nama;
}
}
//stack berupa double linked list
cItem top, tail;
int jumlah;
cStack(){
top=tail=null;
jumlah=0;
System.out.println("Object stack dibuat...");
}
public void push(cItem baru){
if(top==null){
top=tail=baru;
}
else{
//penambahan di depan
baru.next = top;
top.prev = baru;
top = baru;
}
System.out.println("Push sukses...");
}
public cItem pop(){
if(top==null){
System.out.println("Stack kosong!");
return null;
}
else if(top.next==null){
//stack sisa 1 item
cItem t = top;
top=tail=null;
t.next = null;
t.prev = null;
System.out.println("Pop sukses...");
return t;
}
else{
//stack sisa > 1 item
cItem t = top;
top = top.next;
top.prev = null;
t.next = null;
t.prev = null;
System.out.println("Pop sukses...");
return t;
}
}
public void print(){
System.out.print("Isi Stack: ");
for (cItem t = top; t != null; t=t.next) {
System.out.print(t.getNama()+" ");
}
System.out.println("");
}
}
import java.util.Scanner;
public class appStack {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
cStack myStack = new cStack();
int pilih=0;
do{
System.out.println("\nMENU STACK");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Print");
System.out.println("4. Exit");
System.out.print("Pilih = ");
pilih = s.nextInt();
switch(pilih){
case 1:
System.out.print("Masukkan Nama = ");
String nm = s.next();
cItem t = new cItem(nm);
myStack.push(t);
break;
case 2:
t = myStack.pop();
if(t==null){
System.out.println("Tidak ada data!");
}
else{
System.out.println(t.getNama()+" keluar...");
}
break;
case 3:
myStack.print();
break;
case 4:
System.out.println("Terima kasih...");
break;
}
}while(pilih!=4);
}
}
Ayo pakai software kontrol stok "MiniMart", GRATIS SEPENUHNYA & FULL OPEN-SOURCE LHOO .....
ReplyDeleteSilahkan download software "MiniMart" di sini :
https://helloyud.blogspot.com/2016/12/aplikasi-pertokoan-minimart.html