Contoh Program Stack Pada Pemrograman Java

April 25, 2014
ini tugas ke 3 Struktur data, Stack inputan bisa di liat..

 


/
import java.util.Scanner;
import java.util.Stack;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Zen
 */
public class stackcoba2 {
    static Stack zen = new Stack();
    static Scanner in = new Scanner(System.in);
    static String a;
    static String arr;
    static int max_st;
    static int i=0;
    static int top =-1;
    static void pushh(Stack zen, String a){
        zen.push((a).toString());
        System.out.println("Stack: " + zen);
    }
   public int find(){
        return (int) (top=top+1);
    }
   
   
    static void popp(Stack zen, String a ){
        a = (String) zen.pop();
        System.out.println("stack: " + zen);
    }
   
    public static void main(String[] args) {
        System.out.print("Masukkan Max_Stack : "); 
    max_st=in.nextInt();
        for (int i = 0; i < max_st; i++) {
            System.out.print("PUSH -> ");
            a=in.next();
            pushh(zen, a);   
        }
        System.out.println("POP = y / anykeys=stop");
      while(!a.equals("n")){
            System.out.print("POP ->  ");
               a=in.next();
               if (a.equals("y") && !zen.isEmpty()) {
                  popp(zen, a);
                 
            }else{
                   System.out.println("apa stack kosong?  "+zen.isEmpty());
                   break;
               }
         
            System.out.println("apa stack kosong?  "+zen.isEmpty());
             if (zen.isEmpty()) {
                       System.out.println("stop");
                       break;
                 }
        }
    }
}

Share this

Related Posts

Previous
Next Post »