Question
In Javascript we are going to create a Doubly Linked List . (A Singly Linked List is another example of a type of List they
In Javascript we are going to create a Doubly Linked List. (A Singly Linked List is another example of a type of List they do the same thing). If you need a little primer on creating objects and classes in Javascript see http://www.w3schools.com/js/js_object_definition.asp if you know your object oriented code this will be relatively straightforward. Your List will be made up of Nodes. Each node will have the following properties;
Node Properties
id A simple id for the node itself content A value (String) next A pointer to the next node in the List (null for the last node). last A pointer to the previous node in the List
List Properties
head Pointer to the first node in the list length Number of nodes in the list
Here is a simple Javascript code that represents the concept . It should be a good demonstration for you to use in writing your code. https://jsfiddle.net/reaglin/q46obht6/ This code should help you get started, but you should build your List and Node objects from scratch.
Next you will populate your list with 5 nodes with content A, B, C, D, E. You will also create a print function for the list that will print the nodes in order. You will need to create some functions to support this functionality.
You will now need to write an interface to allow the user to add nodes to the end of the list. There should be a single text box to enter the Node content, and a button Add to List. When the user adds a node to the list, then you should add the node with the content to the end of the list and print the new list.
If you have questions you MUST post to the bulletin board. All students who are in progress or working on this will be granted extensions if they are asking questions on the BB.
Information
Note that I use the _ to differentiate between passed variables to a function and properties of the function. This I pass _content to the function and then set the property of the function equal to the passed value this.content = _content. The reason for the underscore is simply a coding convention, you should always employ consistent conventions to make reading your code easy when you have to come back to it later.
To define a Node object in JavaScript with the properties required by the assignment the following code will create the object
function Node(_content) { // Implementation of a doubly linked list (last and next pointers) this.content= _content; // The value stored this.last = null; // A pointer to the previous link this.next = null; // a pointer to the next link return this; // returns the created node } |
To define a List object in Javascript with the properties required by the assignment the following code will create the object
function List(_content) { // We will define the list with the first link defined this.length = 1; // Technically the list does NOT need a length property this.head = new Node(_content); // Creates the first Node this.last = this.head; // When created - head and last are the same. Note - technically a List does not need a pointer to the last node return this; // Return a pointer to the newly created List } |
To Instantiate (Create) an Node Object named aNode with content _content.
var aNode = new Node("Node A"); // Creates Node with Content
|
To Instantiate (Create) an List Object named aList (this List will be created containing one node with content _content)
var aList = new List("Node A"); // Creates List with first Node |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started