单链表
function Node(element){
this.element=element;
this.next=null;
}
function LinkList() {
this.root=null;
this.find=find;
this.insert=insert;
this.display=display;
}
function find(element) {
let node=this.root;
while(node!=null){
if(node.element==element)
return node;
else
node=node.next;
}
return null;
}
function insert(node) {
if(this.root==null)
this.root=node;
else{
let currrent=this.root;
while (currrent.next!=null){
currrent=currrent.next;
}
currrent.next=node;
}
}
function display() {
let node =this.root;
while (node!=null){
console.log(node.element);
node=node.next;
}
}
let node1=new Node(1);
let node2=new Node(2);
let node3=new Node(3);
let linklist=new LinkList();
linklist.insert(node1);
linklist.insert(node2);
linklist.insert(node3);
linklist.display();
双链表
function Node(element) {
this.element=element;
this.previous=null;
this.next=null;
}
function LinkList() {
this.root=null;
this.find=find;
this.insert=insert;
this.display=display;
}
function display() {
let current=this.root;
while (current!=null){
console.log(current.element);
current=current.next;
}
}
function find(element) {
let current=this.root;
while (current!=null){
if(current.element==element)
return current;
else
current=current.next;
}
return null;
}
function insert(node) {
if(this.root==null)
this.root=node;
else{
let current=this.root;
while (current.next!=null){
current=current.next;
}
current.next=node;
node.previous=current;
}
}
let node1=new Node(3);
let node2=new Node(5);
let node3=new Node(4);
let linklist=new LinkList();
linklist.insert(node1);
linklist.insert(node2);
linklist.insert(node3);
linklist.display();