COP3530 Assignment 10 - Indexes

by joseph_kanawall2400

HTML

<div id="output"/>

CSS

div {
	font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;
}

JavaScript

// Class Definitions
function Node(id, content, last)
{
	this.id = id;
	this.next = null;
	this.last = last;
	this.content = content;

	this.toString = function()
	{
		return content;
	}
	
	this.clone = function()
	{
		var newNode = new Node(this.id, this.content, this.last);
		newNode.next = this.next;
		return newNode;
	}
}

function List()
{
	this.head = null;
	this.length = 0;

	this.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;
		}
		
		// Add index operator
		this[this.length] = node;
		
		// Update list properties
		this.head = node;
		this.length++;
	}

	this.goto_begin = function()
	{
		// Reset head to beginning of list
		while (this.previous()) {}
	}

	this.goto_end = function()
	{
		// Traverse list until next is null
		while (this.next()) {}
	}

	this.previous = function()
	{
		if (this.head.last != null)
		{
			this.head = this.head.last;
			return true;
		}
		else
		{
			return false;
		}
	}

	this.next = function()
	{
		if (this.head.next != null)
		{
			this.head = this.head.next;
			return true;
		}
		else
		{
			return false;
		}
	}

	this.clear = function()
	{
		this.head = null;
		this.length = 0;
		
		// Remove all node properties
		for(var prop in this)
		{
			if(this[prop] instanceof Node) {
				delete this[prop]
			}
		}
	}
	
	this.indexOf = function(value)
	{
		if(this.length != 0)
		{
			for(var prop in this)
			{
				if(this[prop] instanceof Node && this[prop].content == value) {
					return parseInt(prop);
				}
			}
		}
		return null;
	}
	
	this.clone = function()
	{
		var newList = new List();
		this.goto_begin();
		do
		{
			newList.append(this.head.content);
		}
		while (this.next());
		this.goto_begin();
		return newList;
	}
	
	this.swap = function(node1,...