1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 10 function Node(ele){ 11 this.ele = ele; 12 this.next = null; 13 } 14 15 function linkList(){ 16 this.head = new Node("head"); 17 this.find = find; 18 this.insert = insert; 19 this.remove = remove; 20 this.findPrev = findPrev; 21 this.display = display; 22 } 23 24 function find(item){ 25 var currNode = this.head; 26 while(currNode.ele != item ){ 27 currNode = currNode.next; 28 } 29 return currNode; 30 } 31 32 function insert(newEle,ele){ 33 var newNode = new Node(newEle); 34 var current = this.find(ele); 35 newNode.next = current.next; 36 current.next = newNode; 37 } 38 39 function display(){ 40 var currNode = this.head; 41 while(currNode.next != null){ 42 console.log(currNode.next.ele); 43 currNode = currNode.next; 44 } 45 } 46 47 function findPrev(item){ 48 var currNode = this.head; 49 while ((currNode.next != null) && (currNode.next.ele != item)){ 50 currNode = currNode.next; 51 } 52 return currNode; 53 } 54 55 function remove(item){ 56 var prev = this.findPrev(item); 57 if(prev.next != null){ 58 prev.next = prev.next.next; 59 } 60 } 61 62 63 64 var obj = new linkList(); 65 obj.insert("zhangsan","head"); 66 obj.insert("lisi","zhangsan"); 67 obj.insert("zhaowu","lisi") 68 console.log(obj.find("lisi")); 69 obj.display(); 70 obj.remove("lisi"); 71 obj.display(); 72 </script> 73 </body> 74 </html>