COP3530 Assignment 4
by joseph_kanawall2400
HTML
Append New Value:
<input type="textbox" id="value_box" value="" />
<br/>
<input type="button" onClick="AddToList(document.getElementById('value_box').value);" value="Add to List" />
<br/><br/>
<div id="list_log" />
JavaScript
// Node Functions
function Node(id, content, last) {
this.id = id;
this.next = null;
this.last = last;
this.content = content;
return this;
}
Node.prototype.toString = function() {
return "Node <ID:" + this.id + ", Value:'" + this.content + "'>";
}
// List Functions
function List() {
this.head = null;
this.length = 0;
return this;
}
List.prototype.append = function(content) {
// Go to end of list if length is not 0
if (this.head != null) {
this.goto_end();
}
// Create new node
var node = new Node(this.length, content, null);
// Check if new node is first node
if (this.head != null) {
node.last = this.head;
this.head.next = node;
}
// Update list properties
this.head = node;
this.length++;
}
List.prototype.print_head = function() {
var string = this.head.toString();
return string;
}
List.prototype.goto_begin = function() {
// Reset head to beginning of list
while (this.previous()) {}
}
List.prototype.goto_end = function() {
// Traverse list until next is null
while (this.next()) {}
}
List.prototype.previous = function() {
if (this.head.last != null) {
this.head = this.head.last;
return true;
} else {
return false;
}
}
List.prototype.next = function() {
if (this.head.next != null) {
this.head = this.head.next;
return true;
} else {
return false;
}
}
List.prototype.print = function() {
var string = "";
this.goto_begin();
do {
string += this.head.toString() + "<br/>";
}
while (this.next());
return string;
}
// Other Functions
function AddToList(value) {
list.append(value);
document.getElementById("list_log").innerHTML = list.print();
}
// Setup
var list = new List();
list.append("A");
list.append("B");
list.append("C");
list.append("D");
list.append("E");
document.getElementById("list_log").innerHTML =...