Javascript 我正在学习一门数据结构课程,我必须使用JSFIDLE来演示我的作业
我需要创建一个界面,允许用户将节点添加到列表中。这是我到目前为止的代码,不确定我做错了什么,我是新手Javascript 我正在学习一门数据结构课程,我必须使用JSFIDLE来演示我的作业,javascript,html,list,data-structures,Javascript,Html,List,Data Structures,我需要创建一个界面,允许用户将节点添加到列表中。这是我到目前为止的代码,不确定我做错了什么,我是新手 当我试图在JSFIDLE编辑器中运行它时,没有输出?我用代码snip运行它,错误是没有定义id。以下是我的小提琴的链接: HTML 正在添加的节点的名称: var list=null; //用于创建列表的函数 函数createList(){ var值=document.getElementById(“LinkName”).value; var length=document.getElem
当我试图在JSFIDLE编辑器中运行它时,没有输出?我用代码snip运行它,错误是没有定义id。以下是我的小提琴的链接: HTML
正在添加的节点的名称:
var list=null;
//用于创建列表的函数
函数createList(){
var值=document.getElementById(“LinkName”).value;
var length=document.getElementById(“LinkName”).length;
列表=新列表(值、长度);
document.getElementById(“demo”).innerHTML=list.print();
}
//用于向列表中添加新节点的函数
函数addNode(){
var id=document.getElementById(“LinkName”).id;
var值=document.getElementById(“LinkName”).value;
list.addNode(id,value);
document.getElementById(“demo”).innerHTML=list.print();
}
//使用以下属性定义链接对象
功能节点(\u id、\u值、\u last){
this.id=\u id;//节点本身的简单id
this.value=\u value;//存储的值
this.last=\u last;//指向上一个链接的指针
this.next=null;//指向下一个链接的指针
return this;//返回创建的节点
}
Node.prototype.asString=函数(){
返回“节点值:”+this.Value+“
”;
}
//定义列表对象
函数列表(_值,_长度){
//我们将使用定义的第一个链接定义列表
this.length=_length;
this.head=新节点(_id,_value,null);//指向head的指针为null
this.last=this.head;//创建时-head和last相同。
}
List.prototype.addNode=函数(_值){
var node=新节点(\u id,\u value,\u last);
如果(此长度){
this.last.next=节点;
node.last=this.last;
this.last=节点;
}否则
{
this.head=节点;
this.last=节点;
}
这个;
返回节点;
}
//打印功能
List.prototype.print=函数()
{
var s=“”;
var n=这个头;
while(n!=null){
s+=n.asString();
n=n.next;
}
返回s;
}
//创建数据结构对象
var_list=new createList();
//添加节点
var node=新节点(\u id,\u value,\u last);
node.id=1;
node.value=“A”;
_list.addNode(节点);
var node=新节点(\u id,\u value,\u last);
node.id=2;
node.value=“B”;
_list.addNode(节点);
var node=新节点(\u id,\u value,\u last);
node.id=3;
node.value=“C”;
_list.addNode(节点);
var node=新节点(\u id,\u value,\u last);
node.id=4;
node.value=“D”;
_list.addNode(节点);
var node=新节点(\u id,\u value,\u last);
node.id=5;
node.value=“E”;
_list.addNode(节点);
_list.print();
欢迎来到StackOverlfow!你能解释一下为什么你认为你错了吗?这是很多代码-也许您可以创建一个较小的示例?当我尝试在JSFIDLE编辑器中运行它时,没有输出?我用代码snip运行它,错误是没有定义id。这是我的提琴的链接。你的提琴设置为重载运行。单击齿轮,因为JavaScript,并将其设置为在头部或身体末端。感谢您修复了它
<br/>
<br/> Name of Node being added:
<input type="textbox" id="LinkName" Value="New Node" />
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="demo"></p>
<br/>
var list = null;
//a function for creating a list
function createList() {
var value = document.getElementById("LinkName").value;
var length = document.getElementById("LinkName").length;
list = new List(value, length);
document.getElementById("demo").innerHTML = list.print();
}
//a function for adding a new node to list
function addNode() {
var id = document.getElementById("LinkName").id;
var value = document.getElementById("LinkName").value;
list.addNode(id, value);
document.getElementById("demo").innerHTML = list.print();
}
// Define the link object with following properties
function Node(_id, _value, _last) {
this.id = _id; // A simple id for the node itself
this.value = _value; // The value stored
this.last = _last; // A pointer to the previous link
this.next = null; // a pointer to the next link
return this; // returns the created node
}
Node.prototype.asString = function() {
return "Node Value:" + this.value + "<br/>";
}
// Define the List object
function List(_value, _length) {
// We will define the list with the first link defined
this.length = _length;
this.head = new Node(_id, _value, null); // Pointer TO the head is null
this.last = this.head; // When created - head and last are the same.
}
List.prototype.addNode = function(_value) {
var node = new Node(_id, _value, _last);
if (this._length) {
this.last.next = node;
node.last = this.last;
this.last = node;
} else
{
this.head = node;
this.last = node;
}
this._length++;
return node;
}
//print function
List.prototype.print = function()
{
var s = "";
var n = this.head;
while (n != null) {
s += n.asString();
n = n.next;
}
return s;
}
//create data structure object
var _list = new createList();
//add nodes
var node = new Node(_id, _value, _last);
node.id = 1;
node.value = "A";
_list.addNode(node);
var node = new Node(_id, _value, _last);
node.id = 2;
node.value = "B";
_list.addNode(node);
var node = new Node(_id, _value, _last);
node.id = 3;
node.value = "C";
_list.addNode(node);
var node = new Node(_id, _value, _last);
node.id = 4;
node.value = "D";
_list.addNode(node);
var node = new Node(_id, _value, _last);
node.id = 5;
node.value = "E";
_list.addNode(node);
_list.print();
<p id="demo"></p>
<p id="addNode2"></p>