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

13 Juli 2009

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>


1 komentar: