function LinkList() {
var count = 0;
var head = null;
function Node() {
var Data = null;
var Next = null;
}
this.Count = function () {
return count;
}
this.Add = function (data) {
var nd = new Node();
nd.Data = data;
if (head) {
nd.Next = head;
}
head = nd;
count++;
}
this.Traverse = function () {
var result = "";
var current = head;
while (current) {
result = result + " --> " + current.Data;
current = current.Next;
}
return result;
}
}
var mylist = new LinkList();
function AddToList() {
var txtCtrl = document.getElementById('datatxt');
var resCtrl = document.getElementById('result');
if (txtCtrl && txtCtrl.value.length > 0) {
mylist.Add(txtCtrl.value);
if (resCtrl) {
resCtrl.innerHTML = "" + txtCtrl.value + " added to list.";
}
txtCtrl.value = '';
}
}
function PrintList() {
var resCtrl = document.getElementById('result');
if (resCtrl) {
if (mylist.Count() > 0) {
resCtrl.innerHTML = mylist.Traverse();
}
else {
resCtrl.innerHTML = "List is empty.";
}
}
var txtCtrl = document.getElementById('datatxt');
if (txtCtrl) {
txtCtrl.value = '';
}
}
function GetCount() {
var resCtrl = document.getElementById('result');
if (resCtrl) {
resCtrl.innerHTML = "List has " + mylist.Count() + " items.";
}
var txtCtrl = document.getElementById('datatxt');
if (txtCtrl) {
txtCtrl.value = '';
}
}
No comments:
Post a Comment