Contoh Program Stack Dengan Array Di Java

April 25, 2014
contoh program stack tapi pake array.... bisa di liat dan dipelajari...






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 stackcoba {
    static Scanner zen = new Scanner(System.in);
    static int max;
    static Object arrstack[];
    static String pushh;
    static int i;
    static int j;
    static int help[];
    static String topp;
    static int top=-1;
   
   
   
    static void push(){
     Stack baru = new Stack();
     arrstack=new String [max];
        for ( i = 0; i < max; i++) {
           System.out.print("Push -> ");  
           arrstack[i] = zen.next();
            show();
        } 
    }
   
    public int find(){
        return (top+1);
    }
   
    static void show(){
        for (int k = 0; k < max; k++) {
            System.out.print("["+arrstack[k]+"],");  
        }
        System.out.println();
    }
   
    static void show2(){
        for (int k = max-1; k >= 0; k--) {
           System.out.print("["+arrstack[k]+"],"); 
        }
    }
   
    static void pop (){
       // arrstack=new String[max];
        System.out.println("To stop input ''stop''");
        for ( j = max-1; j >= 0; j--) {
            System.out.print("Pop -> ");
          topp=zen.next();
          
          if (topp.contains((CharSequence) arrstack[j])) {
             arrstack[j]=null;
            
         show();
      
          }else{
              show();
              break; 
          }
        }
    }
   
    public static void main(String[] args) {
        System.out.print("Masukkan Nilai Maksimum Stack : ");
        max=zen.nextInt();
        stackcoba z = new stackcoba();
                   z.push();
                   z.pop();
    
    }
}

Share this

Related Posts

Previous
Next Post »