1. Class Main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tumpukan;
/**
*
* @author Hermawankusuma */
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Stack tumpuk = new Stack(5);
tumpuk.push(20);
tumpuk.push(21);
tumpuk.push(12);
tumpuk.push(29);
tumpuk.push(11);
tumpuk.tampilkan();
double a = tumpuk.pop();
System.out.println("yang barusan di pop: "+ a );
tumpuk.tampilkan();
tumpuk.push(3);
tumpuk.tampilkan();
}
}
2. Class Stack
package tumpukan;
/**
*
* @author Hermawankusuma */
public class Stack {
int size;
int top;
double[] data;
public Stack(int size)
{
this.size = size;
top = -1;
data = new double[size];
}
public boolean kosong()
{
boolean ks;
if(top == -1)
{
ks = true;
}
else
{
ks = false;
}
return ks;
}
public boolean penuh()
{
if (top == size-1)
{
return true;
}
else {
return false;
}
}
public double nilaiTop()
{
return data[top];
}
public double pop()
{
double a = data[top];
top--;
return a;
}
public void push(double dt)
{
if (!penuh())
{
top++;
data[top] = dt;
}
else {
System.out.println("sudah penuh");
}
}
public void tampilkan()
{
System.out.println("isi Stack: ");
for (int i = top; i >=0; i--)
{
System.out.println(data[i]+" ");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tumpukan;
/**
*
* @author Hermawankusuma */
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Stack tumpuk = new Stack(5);
tumpuk.push(20);
tumpuk.push(21);
tumpuk.push(12);
tumpuk.push(29);
tumpuk.push(11);
tumpuk.tampilkan();
double a = tumpuk.pop();
System.out.println("yang barusan di pop: "+ a );
tumpuk.tampilkan();
tumpuk.push(3);
tumpuk.tampilkan();
}
}
2. Class Stack
package tumpukan;
/**
*
* @author Hermawankusuma */
public class Stack {
int size;
int top;
double[] data;
public Stack(int size)
{
this.size = size;
top = -1;
data = new double[size];
}
public boolean kosong()
{
boolean ks;
if(top == -1)
{
ks = true;
}
else
{
ks = false;
}
return ks;
}
public boolean penuh()
{
if (top == size-1)
{
return true;
}
else {
return false;
}
}
public double nilaiTop()
{
return data[top];
}
public double pop()
{
double a = data[top];
top--;
return a;
}
public void push(double dt)
{
if (!penuh())
{
top++;
data[top] = dt;
}
else {
System.out.println("sudah penuh");
}
}
public void tampilkan()
{
System.out.println("isi Stack: ");
for (int i = top; i >=0; i--)
{
System.out.println(data[i]+" ");
}
}
}
Tidak ada komentar:
Posting Komentar