welcome

HAIIIIIIIII WELCOME IN MY PERSONNAL BLOGSPOT

Senin, 06 Juli 2009

tugas struktur data

Queue (antrian) push and shift
?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.shift();
if (queue.length==0)
return "stack 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>

New LinkedList
Program linked list setelah ditambahkan function Ukuran, toArray dan toString

?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>");
document.write("Isi Linked List Sebelum di Buang = " +list.Ukuran() +
"?br>");
document.write ("?br>")
document.write("Isi Linked List Awal =" +list.toArray());
document.write ("?br>")
document.write("Data String Linked List Awal =" +list.toString());
alert("Isi Linked List Awal =" +list.Item(0));
alert("Item 1 Pada Linked List dibuang, Yaitu =" +list.Item(1));
list.Buang(1);
document.write ("?br>")
document.write ("?br>")
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>");
document.write("Isi Linked List Setelah di Buang = " +list.Ukuran() +
"?br>");
document.write("Isi Linked List Terakhir =" +list.toArray());
document.write ("?br>")
document.write("Data String Linked List Terakhir =" +list.toString());
??script>
??body>
??html>

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+')'+'?br>');
} 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>


Doubly LinkedList
?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>


latihan penggabungan array & linklist
?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>


penggabungan array,linkedlist,short
?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("3");
list.Tambah("2");
list.Tambah("1");
list.Tambah("4");


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

document.write("Isi Linked List Awal = " +list.Ukuran() + "?br>");
document.write ("?br>")


document.write("Isi Linked List to Array =" +list.toArray());
document.write ("?br>")


document.write("Data String Linked List Awal =" +list.toString());



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 (toArray, Angka)
{
if ((toArray.length - 1) >= Angka)
return (toArray[Angka] + "," + DataString(toArray,(Angka +

1)));
else
return "";
}

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

return toArray;
}




?/script>



?body>
?center>
?form>
Masukan Deretan Angka, Pisahkan dengan Koma :
?hr>
?input type=text name=Data size=30 value= >
?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>
?/html>

Tidak ada komentar: