STACK UTILITY JAVA

STACK UTILITY JAVA
STACK UTILITY JAVA

Latihan1:

import java.util.Stack;
class Main {
  public static void main(String[] args) {
    Stack<Integer> stack = new Stack<Integer>();

    for (int i = 0; i < 10; i++) {
      stack.push(i);
      System.out.print(i + " ");
    }
    System.out.println("");

    int position = stack.search(3);
    System.out.println("Search result position: " + position);

    System.out.println("Stack top: " + stack.peek());

    while (!stack.empty()) {
      System.out.print(stack.pop() + " ");
    }
  }
}


Latihan2:

import java.util.Stack;
class StackExample
{
  public static void main(String args[])
  {
    Stack s = new Stack();
    System.out.print(s.push(55));
    System.out.print(s.push(66));
    System.out.println(s.push(77));
System.out.println("");
 
    System.out.println("Puncak: " + s.peek());
    s.push("88");
    System.out.println("Puncak: " + s.peek());
    //s.pop();
 
    s.push(".");
    int count = s.search(77);
 
    System.out.println(count);
 
    while (count != -1 && count > 1)
    {
      s.pop();
      count--;
    }
    System.out.println(s.pop());
    System.out.println(s.empty());
    System.out.println(s.pop());
    System.out.println(s.pop());
    System.out.println(s.pop());
  }
}






Latihan3:

import java.io.*;
import java.util.Stack;
class StackExample
{
  public static void main(String args[]) throws IOException
  {
    Stack<Integer> s = new Stack<Integer>();

    int input;
    int i, data ; 
 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       
    System.out.print("Masukkan Jumlah Data : "); data = Integer.parseInt(in.readLine());
    int jumlah[] = new int[data];
 
    for (i=0; i<jumlah.length; i++)
    {
    System.out.print("Masukkan angka : "); input = Integer.parseInt(in.readLine());
    s.push(input);
   
     
    System.out.println(" ");
    while (!s.empty())
    {   
    System.out.println(s.pop());
    }
   
    System.out.println("Isi Stack Sekarang : "+s);
    }
}




Latihan4:

import java.io.*;
import java.util.Stack;
class StackExample
{
public static void main(String args[]) throws IOException
{
Stack s = new Stack();
String kata;
int i;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Masukkan tulisan : "); kata = in.readLine();
for (i=0; i<kata.length(); i++)
{
s.push(kata.charAt(i));
}
System.out.println(" ");
System.out.print("Setelah dibalik menjadi : ");
while (!s.empty())
{
System.out.print(s.pop());
}
System.out.println(" ");
}
}



No comments:

Post a Comment

Iklan Atas Artikel

Meta Propeller atas artikel

cara mudah turial kode Java blog website grafika komputer jaringan komputer dan Arduino

Iklan Tengah Artikel 2

Iklan Bawah Artikel