Silahkan Melihat-lihat... Semoga Bermanfaat...

13 Juli 2009

BAB VIII (Sort)

?html>
?head>
?script language="JavaScript">

function Urutkan (form)
{
DataKosong = false;
DataInputan = form.Data.value;
inputData = DataInputan.split(",");
for (var i = 0; i ? inputData.length; i++)
{
inputData[i] = parseInt (inputData[i], 10);
if (isNaN(inputData[i]))
{
DataKosong = true;
break;
}
}

inputData = bubbleSort(inputData, 0, inputData.length - 1);
if (DataKosong)
{
alert("Silakan Entri Dulu Data, Pisahkan dengan tanda Koma");
form.Data.focus();
}
else
form.Hasil.value = DataString(inputData, 0);
}

function DataString (ArrayData, Angka)
{
if ((ArrayData.length - 1) >= Angka)
return (ArrayData[Angka] + "," + DataString(ArrayData,(Angka +

1)));
else
return "";
}

function bubbleSort (ArrayData, Mulai, MulaiBaru)
{
for (var i = MulaiBaru - 1; i >= Mulai; i--)
{
for (var j = Mulai; j ?= i; j++)
{
if (ArrayData[j+1] ? ArrayData[j])
{
var DataTemporer = ArrayData[j];
ArrayData[j] = ArrayData[j+1];
ArrayData[j+1] = DataTemporer;
}
}
}

return ArrayData;
}
?/script>
?/head>

?body>
?center>
?form>
Masukan Deretan Angka, Pisahkan dengan Koma :
?hr>
?input type=text name=Data size=30 value="5,6,8,5,12,4,8,4,20">
?br>
?br>
?input type=button value="Urutkan"

OnClick="Urutkan(this.form)">
?br>
?br>
?br>
Hasil Pengurutan :
?hr>
?input type=text name=Hasil size=30>
?/form>
?/center>

?/body>
?/html>

BAB VII (Graph)

?html>
?head>?title>Dijkstra Algorithms?/title>

?/head>
?body>
?script language = "javascript">

var NilaiAcuan = 10000;
var TakTerdefinisi = -1;
var NamaVerteks = new Array('A','B','C','D','E','F');
var Matriks = new Array(6);

function Bobot(a,b)
{
return Matriks[a][b];
}

function Dijkstra(JumlahVerteks,Awal,d)
{
var Posisi = new Array(JumlahVerteks);
var i;
var Kunjungan = new Array(JumlahVerteks);
var pred = new Array(JumlahVerteks);

for(i=0; i?JumlahVerteks; i++)
{
Posisi[i] = NilaiAcuan;
pred[i] = TakTerdefinisi;
Kunjungan[i] = false;
}
Posisi[Awal]=0;

var Verteks;
for (Verteks =0; Verteks ? JumlahVerteks; Verteks++)
{
var JarakTerpendek = NilaiAcuan, closest = -1;
for (i=0; i ? JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
if (Posisi[i] ?= JarakTerpendek)
{
JarakTerpendek = Posisi[i]; closest = i;
}
}
}

Kunjungan[closest] = true

for (i=0; i ? JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
var w = Bobot(closest, i);
if (Posisi[closest]+w ? Posisi[i])
{
Posisi[i] = Posisi[closest] + w;
pred[i] = closest;
}
}
}
}


i=d
if (Posisi[i] ? NilaiAcuan)
{
var Lintasan = NamaVerteks[i];
var Verteks = i;
while (Verteks>0)
{
Verteks = pred[Verteks];
if (Verteks>=0) Lintasan = NamaVerteks[Verteks] + '->' + Lintasan;
}
alert("Jarak :" +Posisi[i]+ '('+Lintasan+')');
} else
{
alert("Tidak ada Jalur");
}
}


var x = '~';
document.write('?pre>');
document.write(Matriks[0]=new Array(0,2,3,x,x,x));
document.write('?br>');
document.write(Matriks[1]=new Array(2,0,3,6,x,x));
document.write('?br>');
document.write(Matriks[2]=new Array(3,3,0,3,5,x));
document.write('?br>');
document.write(Matriks[3]=new Array(x,6,3,0,1,3));
document.write('?br>');
document.write(Matriks[4]=new Array(x,x,5,1,0,1));
document.write('?br>');
document.write(Matriks[5]=new Array(x,x,x,3,1,0));
document.write('?br>');
document.write('?/pre>');


document.write('?pre>A-2-B-6--D--3-F ?br>');
document.write('\\ | /| / ?br>');
document.write(' 3 3 3 1 1 ?br>');
document.write(' \\ | / | / ?br>');
document.write(' \\|/ |/ ?br>');
document.write(' C--5-E ?br> ?/pre>');



var Dari=0; // A
var Ke =5; // F
Dijkstra(Matriks.length,Dari,Ke);
//-->

?/script>

?/body>
?/htmL>

BAB VI (LinkedList)

SingleLinkedList

?html>
?head>
?title>Struktur Data Linked List?/title>
?/head>
?body>
?script language = "JavaScript">
?!--

function LinkedList()
{
this._panjang = 0;
this._kepala = null;
}

LinkedList.prototype =
{
constructor: LinkedList,

//Membuat Metode untuk Menambah Data Kedalam List
Tambah: function (data)
{
//Membuat Node List
var node = {data: data, next: null}, NodeTanda;

//Periksa Isi List
if (this._kepala === null)
{
this._kepala = node;
}
else
{
NodeTanda = this._kepala;
while (NodeTanda.next)
{
NodeTanda = NodeTanda.next;
}
NodeTanda.next = node;
}
this._panjang++
},


//Membuat metode Untuk Menunjuk Item Data
Item: function (index)
{
if (index > -1 && index ? this._panjang)
{
var NodeTanda = this._kepala,
i = 0;
while (i++ ? index)
{
NodeTanda = NodeTanda.next;
}
return NodeTanda.data;
}
else
{
return null;
}
},


//Membuat metode Untuk Membuang Elemen List
Buang: function (index)
{
if (index > -1 && index ? this._panjang)
{
var NodeTanda = this._kepala, previous, i = 0;
if (index === 0)
{
this._kepala = NodeTanda.next;
}
else
{
while (i++ ? index)
{
previous = NodeTanda;
NodeTanda = NodeTanda.next;
}

previous.next =NodeTanda.next;
}
this._panjang--;
return NodeTanda.data;
}
else
{
return null;
}
},


//Membuat metode Untuk Mengetahui Jumlah Elemen List
Ukuran: function()
{
return this._panjang;
},


//Membuat metode Untuk Membentuk Array
toArray: function()
{
var result = [],
NodeTanda = this._kepala;

while (NodeTanda)
{
result.push (NodeTanda.data);
NodeTanda = NodeTanda.next;
}
return result;
},


//Membuat metode Untuk Konversi ke String
toString: function()
{
return this.toArray().toString();
}

}
var list = new LinkedList ();


list.Tambah("Teknik Informatika");
list.Tambah("Teknik Komputer");
list.Tambah("Komputerisasi Akuntansi");
list.Tambah("komputer Networking");


document.write("Isi Linked List ?br>");
document.write("--------------- ?br>");
for (i=0; i?4; i++)
{
document.write(list.Item(i) + "?br>");
}
document.write("--------------- ?br>");

alert("Isi Linked List Awal =" +list.Item(0));
alert("Item 1 Pada Linked List dibuang, Yaitu =" +list.Item(1));
list.Buang(1);



document.write("Isi Linked List Terakhir ?br>");
document.write("---------------- ?br>");
for (i=0; i?4; i++)
{
document.write(list.Item(i) + "?br>");
}
document.write("---------------- ?br>");

//-->
?/script>
?/body>
?/html>

DoubleLinkedList

?html>
?head>?title>Struktur Data Linked List?/title>?/head>
?body>
?script language = "javascript">
?!--

function DoubleLinkedList()
{
this._panjang = 0;
this._kepala = null;
this._ekor = null;
}

DoubleLinkedList.prototype =
{
constructor:DoubleLinkedList,


Tambah: function (data)
{
var node = { data: data, next: null, prev: null };
if (this._panjang == 0)
{
this._kepala = node;
this._ekor = node;
}
else
{
this._ekor.next = node;
node.prev = this._ekor;
this._ekor = node;
}
this._panjang++;
},


ItemList: function (index)
{
if (index > -1 && index ? this._panjang)
{
var NodeTanda = this._kepala,
i = 0;
while (i++ ? index)
{
NodeTanda = NodeTanda.next;
}
return NodeTanda.data;
}
else
{
return null;
}
},

Buang: function (index)
{
if (index > -1 && index ? this._panjang)
{
var NodeTanda = this._kepala, i = 0;
if (index === 0)
{
this._kepala = NodeTanda.next;

if (this._kepala)
{
this._ekor = null;
}
else
{
this._kepala.prev = null;
}
}
else if (index === this._panjang -1)
{
NodeTanda = this._ekor;
this._ekor = NodeTanda.prev;
this._ekor.next = null;
}
else

{
while (i++ ? index)
{
NodeTanda = NodeTanda.next;
}

NodeTanda.prev.next =NodeTanda.next;
}
this._panjang--;
return NodeTanda.data;
}
else
{
return null;
}
},




toString: function()
{
return this.toArray().toString();
}
}
var list = new DoubleLinkedList();

list.Tambah ("Teknik Informatika");
list.Tambah ("Teknik Komputer");
list.Tambah ("Komputerisasi Akuntansi");
list.Tambah ("Computer Networking");

document.write ("Isi LinkedList ?br>");
document.write ("-------------- ?br>");
for (i=0; i?4; i++)
{
document.write(list.ItemList(i) +"?br>");
}
document.write ("-------------- ?br>");

alert("Isi Linked List Awal =" +list.ItemList(0));
alert("Item 1 pada Linked List dibuang, yaitu = "+list.ItemList(1));
list.Buang(1);

document.write ("Isi LinkedList Terakhir ?br>");
document.write ("-------------- ?br>");
for (i=0; i?4; i++)
{
document.write(list.ItemList(i) +"?br>");
}
document.write ("-------------- ?br>");

//-->
?/script>
?/body>
?/html>

BAB V (Queue)

QUEUE STATIK

?html>
?head>
?title>Program Queue dengan Shift dan Unshift javascript?/title>
?script language = "javascript">

function DataQueue()
{
var queue = [];
queue.unshift(1);
queue.unshift(2);

var i = queue.pop();
document.write(i + "?br>");
queue.unshift(3);
queue.unshift(4);
var i = queue.pop();
document.write(i + "?br>");
}

?/script>
?/head>

?body>
?form name="FormStrukturData">
?input name="btnQueue" value="Queue" type="button"
onClick = "DataQueue();">
?/form>
?/body>
?/html>

QUEUE DINAMIK

?html>
?head>
?title>Struktur Data Queue?/title>
?script language = "javascript">

var queue = new Array();

function masuk_antrian(data)
{
queue.unshift(data);
}

function keluar_antrian()
{
var yang_keluar_antrian = queue.pop();
if (queue.length == 0)
return "Antrian Sudah Kosong";
else
return yang_keluar_antrian;
}

function dalam_antrian(list)
{
list.options.length = 0;
for (var i=0; i?queue.length; i++)
{
var data = new Option(queue[i]);
list.options[list.options.length] = data;
}
}

?/script>
?/head>
?body>
?form>
?input type=text name=textsimpan>
?input type=button value="Masuk Antrian"
onClick='masuk_antrian(textsimpan.value);
textsimpan.value="";
dalam_antrian(visualisasi);'>

?select name="visualisasi" size=12>
?option>Isi Antrian...
?/select>
?input type=text name=textambil size=20>
?input type=button value="Keluar Antrian"
onClick="textambil.value=keluar_antrian();
dalam_antrian(visualisasi);">
?/form>
?/body>
?/html>

BAB IV (Stack)

STACK
?html>
?head>
?title>Implementasi Push dan POP pada Stack?/title>
?/head>

?body>
?script type="text/javascript">
var stack = new Array;
stack.push("A");
stack.push("B");
stack.push("C");
document.write("Stack:"+stack.toString()+ "?br>");

var YangDiAmbil = stack.pop();
document.write("Data yang diambil: "+YangDiAmbil);
document.write("?br>");
document.write("Isi Stack Terakhir: " +stack.toString());
?/script>
?/body>
?/html>

MENAMBAHKAN FUNCTION
?html>
?head>
?title>Struktur Data Stack?/title>
?script language="javascript">

var stack = new Array();

function input_ke_stack(data)
{
stack.push(data);
}

function ambil_dari_stack()
{
var data_dari_stack = stack.pop();
if (data_dari_stack == undefined)
return "Stack Sudah Kosong";
else
return data_dari_stack;
}

function tampilkan_stack(list)
{
list.options.length = 0;
for (var i=0; i?stack.length; i++)
{
var data = new Option(stack[i]);
list.options[list.options.length] = data;
}
}
?/script>
?/head>
?body>
?form>
?input type=text name=textsimpan>
?input type=button value="Masukan Ke Stack"
onClick='input_ke_stack(textsimpan.value);
textsimpan.value="";
tampilkan_stack(mediaList);'>

?select name="mediaList" size=12>
?option>Tampilan Data...
?/select>
?input type=text name=textambil size=20>
?input type=button value="Ambil Dari Stack"
onClick='textambil.value=ambil_dari_stack();
tampilkan_stack(mediaList);'>

?/form>
?/body>
?/html>


BAB III (Record)

RECORD
?html>
?head>
?title>Membuat Rekaman Objek?/title>
?/head>
?script language="javascript">
?!--

var Record = new Array();
function Mahasiswa(NPM,Nama,Prodi,Jenjang)
{
//properti
this.npm = NPM;
this.nama = Nama;
this.prodi = Prodi;
this.jenjang = Jenjang;

//metode
this.LihatRecordMahasiswa = LihatRecordMahasiswa;
}

function LihatRecordMahasiswa()
{
document.write("?td>"+this.npm+"?/td>");
document.write("?td>"+this.nama+"?/td>");
document.write("?td>"+this.prodi+"?/td>");
document.write("?td>"+this.jenjang+"?/td>");
}
//mengisi data
Record[0] = new Mahasiswa("43E57006085085", "Tesa", "TI", "S1");
Record[1] = new Mahasiswa("43E57006085084", "Taufik", "TI", "S1");
Record[2] = new Mahasiswa("43E57006085083", "Wasa", "TI", "S1");
Record[3] = new Mahasiswa("43E57006085082", "Wawan", "TI", "S1");

//menampilkan data
document.write("?table border='1'>" +
"?th>NPM?/th> ?th>Nama?/th> ?th>Prodi?/th> ?th>Jenjang?/th>");
for (i=0; i?Record.length; i++)
{
document.write("?tr>");
Record[i].LihatRecordMahasiswa();
document.write("?/tr>");
}

document.write("?/table>");

//-->

?/script>
?/body>
?/html>

PROGRAM DATA PEGAWAI
?html>
?head>
?title>Manipulasi Data Pegawai?/title>
?script language = "javascript" type="text/javascript">
?!--

var DBPegawai = new Array();
function Pegawai(Fields_NIP,Fields_Nama,Fields_Gaji_Per_Jam,Fields_Jam_Kerja,Fields_Gaji_Total)
{
this.NIP = Fields_NIP;
this.Nama = Fields_Nama;
this.Gaji_Jam = Fields_Gaji_Per_Jam;
this.Jam_Kerja = Fields_Jam_Kerja;
this.Gaji_Total = Fields_Gaji_Total;
}
function inputpegawai()
{
var Nama,Gaji_Per_Jam,Jam_Kerja,Gaji;
while (NIP = window.prompt("Ketikan NIP (Nomor Induk Pegawai) \n\n Click 'cancel' untuk Berhenti !!."))
{
Nama = window.prompt("Nama Pegawai");
Gaji_Per_Jam = parseInt(window.prompt("Gaji Per Jam"));
Jam_Kerja = parseInt(window.prompt("Jumlah Jam Kerja"));
Gaji = Gaji_Per_Jam * Jam_Kerja;
DBPegawai[DBPegawai.length] = new Pegawai(NIP,Nama,Gaji_Per_Jam,Jam_Kerja,Gaji);
}
ShowPegawai();
}

function ShowPegawai()
{
document.write("?table border='1'>" +
"?tr>" +
"?th>NIP?/th> ?th>Nama?/th> ?th>Gaji Per Jam?/th> ?th>Jam Kerja?/th> ?th>Gaji Total?/th>" +
"?/tr>");

for (i=0; i?DBPegawai.length; i++)
{
document.write("?tr>" +
"?td>" +DBPegawai[i].NIP+ "?/td>" +
"?td>" +DBPegawai[i].Nama+ "?/td>" +
"?td align='right'>" +DBPegawai[i].Gaji_Jam+ "?/td>" +
"?td align='right'>" +DBPegawai[i].Jam_Kerja+ "?/td>" +
"?td align='right'>" +DBPegawai[i].Gaji_Total+ "?/td>" +
"?/tr>");
}
document.write("?/table>");
}
//-->
?/script>
?/head>
?body onload = "inputpegawai();">
?/body>
?/html>

BAB II (Array)

ARRAY (1)
?html>
?head>
?title>Belajar Array?/title>
?/head>
?body>
?script language = "JavaScript">
?!--

var data_ku = new Array ("Saya","Sedang","Senang");

for (i=0; i?3; i++)
{
document.write(data_ku[i] + "?br>");
}

alert ("Selamat Belajar Struktur Data");
alert ("Semoga Memperoleh Ilmu yang Bermanfaat");

//-->
?/script>
?/body>
?/html>

ARRAY (2)
?html>
?head>
?title>Parameter Array?/title>
?body>
?script language = "JavaScript">
?!--

var tempat = []
var data = prompt("Masukan Jumlah Maksimum Array: ");

for (i=0; i?=data; i++)
{
tempat[i] = prompt("Masukan Bilangan: ");
}
for (j=0; j?=data; j++)
{
document.write("Elemen Array("+j+") = "+tempat[j]+"?br>");
}

//-->
?/script>
?/body>
?/html>

DERET FIBONACCI
?html>
?head>
?script>
?!--

function fibonacci (n)
{
var i;
var array_fib = new Array ();

array_fib.push(0);
array_fib.push(1);

for (i=0; i?n; i++)
{
array_fib.push(array_fib[0] + array_fib[1]);
array_fib.shift();
}
return array_fib[0];
}

for (i=0; i?=20; i++)
document.write("Fibonacci("+i+") = "+fibonacci(i)+"?br>");
//-->
?/script>
?/head>
?body>
?/body>
?/html>

BAB I (Type Data)

PROGRAM MENAMBAHKAN BILANGAN
?html>
?head>
?title>Membuat Fungsi Matematika Dengan JavaScript?/title>
?/head>
?body>
?script language = "JavaScript">
?!--
function jumlah (x,y)
{
var hasil = x + y;
return(hasil);
}
var a = parseInt(prompt("Ketikan Bilangan A: "));
var b = parseInt(prompt("Ketikan Bilangan B: "));
var z = jumlah(a,b);

document.write(z);
document.write("?br>?hr>");
document.write("Tipe Datanya Adalah = "+typeof(z));

//-->
?/script>
?/body>
?/html>

PROGRAM KOVERSI ANTAR SISTEM BILANGAN
?html>
?head>
?title>Konversi Antar Sistem Bilangan?/title>
?/head>
?body>
?script language = "JavaScript">
?!--

var bil = 25;
document.write("Bilangan Desimal: "+bil+"?br>");
document.write("?hr>");

document.write("Biner: "+bil.toString(2)+"?br>");
document.write("Oktal: "+bil.toString(8)+"?br>");
document.write("Heksa: "+bil.toString(16)+"?br>");

//-->
?/script>
?/body>
?/html>

PROGRAM MENAMBAHKAN 2 BILANGAN
?html>
?head>
?title>Type Bilangan?/title>
?/head>
?body>
?script language = "JavaScript">
?!--

var a = 15;
var b = 14;

tambah = a + b;
kali = a * b;

document.write("Penambahan ("+a+" + "+b+") = "+tambah+"?br>");
document.write("Perkalian ("+a+" x "+b+") = "+kali+"?br>");

//-->
?/script>
?/body>
?/html>