BECAUSE WE LOVE EVOLVING SYSTEM
Double Linklist.Java
--------------------------------------------------------------------------------------------------------------
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
//<T> agar class bisa digunakan untuk tipe data lain
//biasa di sebut generics
public class DoublyLinkedList<T> {
//class Node untuk menyimpan array datanya
private class Node<T> {
//ada data , next dan previous == kayak head tail dan datanya
private T data;
private Node<T> next;
private Node<T> previous;
//constructore dgn argumen operan data, next dan prev
public Node(T data, Node<T> next, Node<T> previous) {
this.data = data;
this.next = next;
this.previous = previous;
}
//getter dari atribut data di Node
public T getData() {
return data;
}
//getter untuk atribut next
public Node<T> getNext() {
return next;
}
public Node<T> getPrevious() {
return previous;
}
//mutator next
public void setNext(Node<T> next) {
this.next = next;
}
//mutator prev
public void setPrevious(Node<T> previous) {
this.previous = previous;
}
}
//atribut doublylinkedlist
private Node<T> head; //head
private Node<T> tail; //tail
private int size;
private ImageIcon icon;
public DoublyLinkedList() {
head = null;
tail = null;
size = 0;
icon = new ImageIcon("doh3.jpg");
}
//mengembalikan semua string data yang ada di dlinkedlist
public String toString() {
String str = "[";
Node<T> curr;
//membaca data ketika node curr dari headnya dan selama tidak kosong,maka node curr di tambahkan
//pada string str, ketika curr node null maka di ganti tanda spasi dan brenti
for (curr = head; curr != null; curr = curr.getNext()) {
str = str + curr.getData();
if (curr.getNext() != null) {
str = str + " ";
}
}
str = str + "]";
return str;
}
//menghapus semua data di list dari range tertentu dengan memanggil metode remove yg memiliki index
public void removeRange(int from, int to) {
if (from < 0 || from >= Size() || to < 0 || to >= Size()) {
return;
}
for (int i = from; i <= to; i++) {
remove(i);
}
}
//menambah data di awal ketika head nya null maka tail adalah node baru yg di dapat dari
//argumen data ketika tidak null maka menset data lama sebagai tail data baru dan head adalah data baru
public void addFirst(T data) {
Node<T> newNode = new Node<T>(data, head, null);
if (head == null) {
tail = newNode;
}
if (head != null) {
head.setPrevious(newNode);
}
head = newNode;
size++;
}
//menghapus daata pertama dengan menset data sebelumnya sbagai head
//ketika list kosong maka muncul peringatan
public void removeFirst() {
if (size == 0) {
JOptionPane pane = new JOptionPane();
pane.setIcon(icon);
pane.showMessageDialog(null, "Cannot remove from an empty list!", "Invalid removal", JOptionPane.ERROR_MESSAGE);
pane.setMessageType(JOptionPane.ERROR_MESSAGE);
return;
}
Node<T> current = head;
head = head.getNext();
current.setNext(null);
size--;
}
// menghapus semua data dengan menset head=tail=null dan size menjadi 0
public void removeALL(){
head=tail=null;
size=0;
}
//menambah data di akhir jika data null maka seperti menambah pada data pertama
//jika data tidak null maka tail adalah data baru
public void addLast(T data) {
if (tail == null) {
addFirst(data);
return;
}
Node<T> newNode = new Node(data, null, tail);
tail.setNext(newNode);
tail = newNode;
size++;
}
//mendapatkanjumlah data yg ada dalam list
public int Size() {
return (size);
}
//menambahkan data di inde tertentu
//ketika null maka seperti menambah data pertama
//jika index lebih besar dari size atau kurang dari 0 maka error
//jika index sama dengan size maka seperti menambah data di akhir
//jika tidak maka dilakukan pembacaan nilai data sebelum index dengan perulangan
//dan dilakukan pemasukan data
public void add(int index, T data) {
int i;
if (index == 0) {
addFirst(data);
return;
}
if (index > size) {
JOptionPane.showMessageDialog(null, "Cannot add out of bounds!", "Invalid command", JOptionPane.ERROR_MESSAGE);
return;
}
if (index < 0) {
JOptionPane.showMessageDialog(null, "Cannot add out of bounds!", "Invalid command", JOptionPane.ERROR_MESSAGE);
return;
}
if (head == null) {
addFirst(data);
return;
}
if (index == size) {
addLast(data);
return;
}
Node<T> current;
current = head;
for (i = 0; i < index - 1; i++) {
current = current.getNext();
}
Node<T> newnode = new Node<T>(data, current.getNext(), current.getPrevious());
current.setNext(newnode);
size++;
}
//menghapus node di index tertentu
public void remove(int index) {
//jika index <0 atau lebih dari ukuran maka eror
if ((index < 0) || (index >= size)) {
JOptionPane.showMessageDialog(null, "You cannot remove an out-of-bounds value!", "Invalid removal", JOptionPane.ERROR_MESSAGE);
return;
}
Node<T> next2, previous3;
Node<T> NodeToRemove = head;//set node yg mau di remove
//mendapatkan nilai node yang mau di remove
for (int v = 0; v < index; v++) {
NodeToRemove = NodeToRemove.getNext();
}
previous3 = NodeToRemove.getPrevious(); //set prev dnode sebelum node yg akan di remove
next2 = NodeToRemove.getNext(); //set next dr node setelah node yg akan di remove
if (previous3 == null) {//jika prev null
if (next2 == null) {//jika next null maka list kosong
head = null;
tail = null;
} else {
head = next2;//jika next ada maka head di ganti nilai next setelah node yg di remove
}
} else {
previous3.setNext(next2);//jika prev ada maka prev jadi next
}
if (next2 == null) {
if (previous3 == null) {//sama kayak di atas tpi di balik
head = null;
tail = null;
} else {
tail = previous3;
}
} else {
next2.setPrevious(previous3);
}
size--;//size berkurang
}
public T get(int i) {//mendapatkan nilai di index tertentu
if (i < 0 || i >= size) {
return null;
}
if (i == 0) {
//Node<T> thisNode = head;
return (head.getData());
}
if (i == size - 1) {
//Node<T> thisNode = tail;
return (tail.getData());
}
Node<T> specialNode = head;
for (int x = 1; x < i + 1; x++) {
specialNode = specialNode.getNext();
}
return (specialNode.getData());
}
public T front() { //mendapatkan nilai paling depan
if (head == null) {
return null;
}
return (get(0));
}
public T back() { //mendapatkan nilai paling belakang
if (tail == null) {
return null;
}
return (get(size - 1));
}
public void removeLast() {
if (head == null) {
JOptionPane.showMessageDialog(null, "Cannot remove from an empty list!", "Invalid removal", JOptionPane.ERROR_MESSAGE);
return;
}
remove(Size() - 1);
}
public String printAlternate() {//sama kayak subString tapi dengan metode yg berbeda dengan mengubahnya sebagai array
String str = "[";
String[] str2 = new String[size];
for (int v = 0; v < size; v++) {
str2[v] = this.get(v) + " " + this.get(size - (v + 1));
}
String str3 = "";
for (int x = 0; x < size - 2; x++) {
str3 = str2[x].concat(str2[x + 1]);
}
String str4 = str + " " + str3 + " " + "]";
return (str4);
}
}
------------------------------------------------------------------------------------------------------------------
image.Java
~~~~~~~~
import java.awt.Color;
import java.awt.Graphics;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author aginta
*/
public class Gambare {
//atribut dan pembuatan objek untuk stack, warna
private Stack staknya = new Stack();
DoublyLinkedList dLLBaru = new DoublyLinkedList();
private Warnane[] JmlTumpukanWarna = new Warnane[10];
private Warnane tempTumpukan;
private String warninge = "";
private int dataMasuke;
private String dataKeluare;
private int dataSkrge = dataLamane = 0;
private int dataLamane;
private int modeGambar = 2;
MergeSort d = new MergeSort();
String[] baru;
int[] baru2;
//memberikan warna pada kotak yang akan menjadi tempat print out data secara random
//mengambil dari warna
public Warnane buatWarnane(int paramInt) {
int i = 100 + (int) (Math.random() * 154.0D);
int j = 100 + (int) (Math.random() * 154.0D);
int k = 100 + (int) (Math.random() * 154.0D);
Color localColor = new Color(i, j, k);
return new Warnane(paramInt, localColor);
}
//melakukan pengosongan stack dengan mempop semua data di list dan gambar
public void newStack() {
if (staknya.isEmpty()) {
return;
} else {
this.JmlTumpukanWarna = new Warnane[10];
for (int i = 0; i < 10; i++) {
this.JmlTumpukanWarna[i] = null;
}
staknya.popAll(staknya.Size()-1);
this.dataLamane = this.dataSkrge;
this.dataSkrge = 0;
this.warninge=("");
this.modeGambar = 2;
return;
}
}
//memasukkan data dari list ke array sebelum di sort
public void toList() {
dLLBaru.removeALL();
for (int k = 0; k < staknya.Size(); k++) {
dLLBaru.add(k, staknya.peekKe(k));
}
baru2=d.toArrayInt(dLLBaru);
}
//melakukan pengisian stak ketika sort telah dilakukan
//dengan membaca data aray baru dan di masukkan ke Doublylinked list lagi
public void doFillSort() {
int i;
this.JmlTumpukanWarna = new Warnane[10];//
System.out.println(staknya.Size());
newStack();
staknya.popAll(staknya.Size()-1);
System.out.println(staknya.Size());
for (i = 0; i < baru2.length; i++) {
int j=baru2[i];
this.tempTumpukan = buatWarnane(j);
this.JmlTumpukanWarna[i] = this.tempTumpukan;
staknya.push(String.valueOf(j));
}
this.baru2=null;
this.dataLamane = this.dataSkrge;
this.dataSkrge = staknya.Size();
System.out.println(staknya.Size());
}
//metode push ke stak
public void push(boolean benarAda, int data) {
if ((!benarAda) || (data < 0) || (data > 999)) {
this.warninge = ("CAN'T PUSH: masukkan angka dari 0 s.d. " + 999);
} else if (staknya.Size() > 9) {
this.warninge = "CAN'T PUSH: stack full";
} else {
dataMasuke = data;
staknya.push(dataMasuke);
this.tempTumpukan = buatWarnane(dataMasuke);
this.dataLamane = this.dataSkrge;
this.dataSkrge = staknya.Size();
JmlTumpukanWarna[staknya.Size() - 1] = tempTumpukan;
this.modeGambar = 1;
}
}
//mempop data
public String pop() {
if (staknya.isEmpty()) {
this.warninge = "CAN'T POP: stack kosong";
} else {
this.dataKeluare = "";
dataKeluare = String.valueOf(staknya.peek());
this.JmlTumpukanWarna[(staknya.Size() - 1)] = null;
staknya.pop();
warninge = dataKeluare + " di pop";
this.dataLamane = this.dataSkrge;
this.dataSkrge = staknya.Size();
this.modeGambar = 1;
}
return this.dataKeluare;
}
//melihat data pertama
public void peek() {
if (staknya.isEmpty()) {
this.warninge = "CAN'T PEEK:stack is kosong";
} else {
this.dataKeluare = "";
dataKeluare = String.valueOf(staknya.peek());
this.warninge = "Top :" + dataKeluare;
this.modeGambar = 1;
}
}
//metode untuk menghitung dan melakukan penggambaran kotak dan garis serta isinya
public void drawTumpukane(Graphics g, int JmlTumpuk) {
int i = 145;
int j = 310 - (40 + 17 * JmlTumpuk);
if (JmlTumpuk != 10) {
int k;
if (JmlTumpuk < 10) {
k = 12;
} else if (JmlTumpuk < 100) {
k = 6;
} else {
k = 0;
}
g.setColor(Color.black);
g.setColor(Color.black);
g.drawLine(160, 290, 160, 110);
g.drawLine(210, 290, 210, 110);
g.drawLine(160, 290, 210, 290);
if (this.JmlTumpukanWarna[JmlTumpuk] == null) {
g.setColor(Color.lightGray);
g.fillRect(i + 18 + 6, j - 4, 34,16);
} else {
int m = this.JmlTumpukanWarna[JmlTumpuk].getHeight();
g.setColor(this.JmlTumpukanWarna[JmlTumpuk].getColor());
g.fill3DRect(i + 18 + 6, j - 4, 34,16, true);
if (m < 10) {
k = 12;
} else if (m < 100) {
k = 6;
} else {
k = 0;
}
g.setColor(Color.black);
g.drawString(String.valueOf(m), i + 18 + k + 15,
j + 17 - 8);
}
}
if (JmlTumpuk == this.dataSkrge) {
g.setColor(Color.red);
} else {
g.setColor(Color.lightGray);
}
}
public void draw(Graphics g) {
if (this.modeGambar == 1) {
g.setColor(Color.lightGray);
g.fillRect(10, 55, 325,25);
g.setColor(Color.black);
g.drawString(this.warninge, 16,74);
drawTumpukane(g, this.dataLamane);
drawTumpukane(g, this.dataSkrge);
this.modeGambar = 2;
return;
}
g.setColor(Color.lightGray);
g.fillRect(0, 0, 440, 300);
g.setColor(Color.black);
g.drawString(this.warninge, 20, 64);
for (int i = 0; i < 10; i++) {
drawTumpukane(g, i);
}
drawTumpukane(g, 10);
}
}
---------------------------------------------------------------------------------------------------------------
mergesort.java
~~~~~~~~~~~~~~~~~~~~
public class MergeSort {
public String[] toArray(DoublyLinkedList list) {
String[] dataK = new String[list.Size()];
for (int i = 0; i < list.Size(); i++) {
dataK[i] = String.valueOf(list.get(i));
}
return mergeJalan(dataK);
}
public String[] mergeJalan(String[] arr) {
String[] x = new String[arr.length];
x = mergeSort(arr);
return x;
}
public int[] toArrayInt(DoublyLinkedList list) {
int[] dataK = new int[list.Size()];
for (int i = 0; i < list.Size(); i++) {
dataK[i] = Integer.parseInt(String.valueOf(list.get(i)));
}
return sort(dataK);
}
public int[] mergeJalanInt(int[] arr) {
int[] x = new int[arr.length];
// x = mergeSortInt(arr);
return x;
}
public String[] mergeSort(String[] d) {
String[] urut = new String[d.length];
if (d.length <= 1) {
return d;
} else {
int mid = d.length / 2;
String[] left = null;
String[] right = null;
if ((d.length % 2) == 0) {
left = new String[d.length / 2];
right = new String[d.length / 2];
} else {
left = new String[d.length / 2];
right = new String[(d.length / 2) + 1];
}
int x = 0;
int y = 0;
for (; x < mid; x++) {
left[x] = d[x];
}
for (; x < d.length; x++) {
right[y] = d[x];
y++;
}
left = mergeSort(left);
right = mergeSort(right);
urut = merge(left, right);
}
return urut;
}
//melakukan pengurutan dengan membandingkan string satu dengan yang lain
public String[] merge(String[] kiri, String[] kanan) {
String[] urutan = new String[kiri.length + kanan.length];
int indexkiri = 0;
int indexkanan = 0;
int indextengah = 0;
int kompil = 0;
while (indexkiri < kiri.length || indexkanan < kanan.length) {
if (indexkiri == kiri.length) {
urutan[indextengah] = kanan[indexkanan];
indextengah++;
indexkanan++;
} else if (indexkanan == kanan.length) {
urutan[indextengah] = kiri[indexkiri];
indextengah++;
indexkiri++;
} else {
kompil = kiri[indexkiri].compareTo(kanan[indexkanan]);
if (kompil > 0) {
urutan[indextengah] = kanan[indexkanan];
indextengah++;
indexkanan++;
} else if (kompil < 0) {
urutan[indextengah] = kiri[indexkiri];
indextengah++;
indexkiri++;
} else {
urutan[indextengah] = kiri[indexkiri];
indextengah++;
indexkiri++;
}
}
}
return urutan;
}
public int[] sort(int[] input) {
int[] temp = new int[input.length];
mergeSort(input, temp, 0, input.length - 1);
return input;
}
public void mergeSort(int[] fromArray, int[] toArray, int left, int right) {
if (left < right) {
int center = (left + right) / 2;
mergeSort(fromArray, toArray, left, center);
mergeSort(fromArray, toArray, center + 1, right);
merge(fromArray, toArray, left, center + 1, right);
}
}
public void merge(int[] fromArray, int[] toArray, int leftPos, int rightPos, int rightEnd) {
int leftEnd = rightPos - 1;
int tempPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while (leftPos <= leftEnd && rightPos <= rightEnd) {
if (fromArray[leftPos] < fromArray[rightPos]) {
toArray[tempPos++] = fromArray[leftPos++];
} else {
toArray[tempPos++] = fromArray[rightPos++];
}
}
while (leftPos <= leftEnd) {
toArray[tempPos++] = fromArray[leftPos++];
}
while (rightPos <= rightEnd) {
toArray[tempPos++] = fromArray[rightPos++];
}
for (int i = 0; i < numElements; i++, rightEnd--) {
fromArray[rightEnd] = toArray[rightEnd];
}
}
}
--------------------------------------------------------------------------------------------------------------
stack.java
~~~~~~~~~~~~~~~~~~~~~
public class Stack<T> {
private DoublyLinkedList<T> dLL;
public Stack() {
dLL = new DoublyLinkedList<T>();
}
public void pop() {
dLL.remove(0);
}
public void push(T data) {
dLL.addFirst(data);
}
public T peek() {
return dLL.get(0);
}
public T peekKe(int index) {
return dLL.get(index);
}
public void Display(){
for(int i=0;i<Size();i++){
System.out.println(dLL.get(i));
}
}
public boolean isEmpty(){
if(dLL.Size()>0){
return false;
}
return true;
}
public void popAll(int akhir){
dLL.removeALL();
}
public int Size() {
return dLL.Size();
}
}
-------------------------------------------------------------------------------------------------------------
Main.java
~~~~~~~~~~~~~~
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author aginta
*/
public class Utama {
public static void main(String[] args){
JFrame Frametampil =new JFrame("simulasi Stack");
Container konApplet =Frametampil.getContentPane();
framenya ObjApplet =new framenya();
ObjApplet.init();
Frametampil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frametampil.setSize(420, 340);
Frametampil.setLocation(400,200);
konApplet.add(ObjApplet,BorderLayout.CENTER);
Frametampil.setVisible(true);
}
}
------------------------------------------------------------------------------------------------------------
painting.java
~~~~~~~~~~~~~~~
import java.awt.Color;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author aginta
*/
public class Warnane {
private int height;
private Color color;
public Warnane(int tinggi, Color warna) {
this.height = tinggi;
this.color = warna;
}
public Color getColor() {
return this.color;
}
public int getHeight() {
return this.height;
}
}
--------------------------------------------------------------------------------------------------------------
frame.java
~~~~~~~~~~~
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author aginta
*/
public class framenya extends JApplet {
Stack stake;
private Thread runner;
private Gambare thePersonGroup;
private int GPNumber = -1;
private boolean isNumber = false;
Button push, pop, merge, Clear, peek,exit;
TextField nggoInput;
public void init() {
setLayout(new FlowLayout());
Panel panelFull = new Panel();
add(panelFull);
panelFull.setLayout(new FlowLayout());
Panel panelTombol = new Panel();
panelFull.add(panelTombol);
panelTombol.setLayout(new FlowLayout(1));
push = new Button("Push");
push.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isNumber = true;
String str = nggoInput.getText();
try {
GPNumber = Integer.parseInt(str);
} catch (NumberFormatException f) {
GPNumber = 0;
isNumber = false;
}
thePersonGroup.push(isNumber, GPNumber);
repaint();
}
});
panelTombol.add(push);
pop = new Button("Pop");
pop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str = "";
str = thePersonGroup.pop();
nggoInput.setText(str);
repaint();
}
});
panelTombol.add(pop);
merge = new Button("Sort");
merge.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
thePersonGroup.toList();
thePersonGroup.doFillSort();
repaint();
}
});
panelTombol.add(merge);
peek = new Button("Peek");
peek.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thePersonGroup.peek();
repaint();
}
});
panelTombol.add(peek);
Clear = new Button("Clear");
Clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thePersonGroup.newStack();
//merge.enable(true);
repaint();
}
});
panelTombol.add(Clear);
exit = new Button("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panelTombol.add(exit);
nggoInput = new TextField();
panelTombol.setLayout(new FlowLayout(2));
panelTombol.add(new Label("Input angka :"));
panelTombol.add(this.nggoInput);
this.thePersonGroup = new Gambare();
repaint();
}
public void start() {
if (runner == null) {
runner = new Thread();
runner.start();
}
}
public void stop() {
if (this.runner != null) {
this.runner.stop();
this.runner = null;
}
}
public void paint(Graphics g) {
this.thePersonGroup.draw(g);
}
public void update(Graphics g) {
paint(g);
}
public void run() {
label3:
break label3;
}
}
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
TEAM PROJECT * Allright commence. 2010
Aginta Team
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
as JAVA ALLIANCE
***********************************************************************************
back trace to 2011
No comments:
Post a Comment