safriansah

stack

Jul 30th, 2018
5,841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package buku;
  2. import java.util.Scanner;
  3. public class Buku {
  4.     int id,tahun;
  5.     String judul;
  6.     Buku next;
  7.     public static Scanner in=new Scanner(System.in);
  8.     public static Scanner str=new Scanner(System.in);
  9.     public void input(){
  10.         System.out.print("Masukkan id buku      : ");
  11.         id=in.nextInt();
  12.         System.out.print("Masukkan judul        : ");
  13.         judul=str.nextLine();
  14.         System.out.print("Masukkan tahun terbit : ");
  15.         tahun=in.nextInt();
  16.         next=null;
  17.     }
  18.     public void view(){
  19.         System.out.println("id buku      : "+id);
  20.         System.out.println("judul        : "+judul);
  21.         System.out.println("tahun terbit : "+tahun);
  22.     }
  23.     public static void main(String[] args) {
  24.         int menu=0;
  25.         linked st=new linked();
  26.         while(menu!=4){
  27.             System.out.print("1.push\n2.pop\n3.view\n4.exit\n : ");
  28.             menu=in.nextInt();
  29.             if(menu==1){
  30.                 Buku baru=new Buku();
  31.                 baru.input();
  32.                 st.push(baru);
  33.             }
  34.             else if(menu==2) st.pop();
  35.             else if(menu==3) st.view();
  36.             else if(menu==4) System.out.println("keluar . . .");
  37.             else System.out.println("salah . . .");
  38.             System.out.println(" ");
  39.         }
  40.     }    
  41. }
  42. class linked{
  43.     Buku top;
  44.     public linked(){
  45.         top=null;
  46.     }
  47.     public void push(Buku a){
  48.         if(top==null) top=a;
  49.         else{
  50.             a.next=top;
  51.             top=a;
  52.         }
  53.     }
  54.     public void pop(){
  55.         if(top==null) System.out.println("kosong");
  56.         else{
  57.             System.out.println("Popping Data . . .");
  58.             top.view();
  59.             top=top.next;
  60.         }
  61.     }
  62.     public void view(){
  63.         if(top==null) System.out.println("kosong");
  64.         else{
  65.             Buku ptr=top;
  66.             while(ptr!=null){
  67.                 System.out.println("- - - - -");
  68.                 ptr.view();
  69.                 ptr=ptr.next;
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment