使用javaScript來實現一個單鏈表
阿新 • • 發佈:2020-06-30
1.建立連結串列節點
class Node{ constructor(element,next){ this.element = element; this.next = next; } }
2.建立一個比較函式
function defaultEquals(a , b){ return a == b; }
3.建立一個單鏈表的類
class LinkedList { constructor(equalsFn = defaultEquals){ this.head = undefined;this.count = 0; this.equalsFn = equalsFn; } }
4.獲取連結串列的長度
size(){ return this.count; }
5.判斷連結串列是否為空
isEmpty(){ return this.size() == 0; }
6.新增元素
push(element){ let node = new Node(element); if(this.head == undefined){//如果連結串列只有一個節點 this.head = node; }else{ let current = this.head; while(current.next!=null){//一直遍歷到連結串列尾部 current = current.next; } current.next = node; }this.count++; }
7.獲取指定位置的值
getElementAt(index){ if(index>=0 && index< this.count){ let current = this.head; for(let i = 0; i < index && current != null; i++){ current = current.next; } return current; } return 'index out of range'; }
8.查詢值的位置
indexOf(element){ let current = this.head; for(let i = 0; i < this.count; i++){ if(current.element === element){ return i; } current = current.next; } return -1; }
9.指定位置插入元素
insert(element,position){ if(position >=0 && position <= this.count){ let node = new Node(element); if(position == 0){ if(this.head === undefined){ this.head = node; }else{ node.next = this.head; this.head = node; } }else if(position == this.count){ let current = this.getElementAt(position - 1); current.next = node; }else{ let previous = this.getElementAt(position - 1); let current = previous.next; node.next = current; previous.next = node; } this.count++; } return "position out of range"; }
10.刪除元素
remove(element){ if(this.isEmpty())return "linkedlist is null"; let index = this.indexOf(element); this.removeAt(index); }
11.刪除指定位置的元素
removeAt(index){ if(this.isEmpty())return "linkedlist is null"; if(index >= 0 && index < this.count){ if(index == 0){//這裡總是容易被忽略,如果index為0的話,就直接更改this.head this.head = this.head.next; }else{ let previous = this.getElementAt(index -1); let current = previous.next; previous.next = current.next; } this.count--; } return 'index out of range'; }
12.列印連結串列的所有的值
toString(){ if(this.isEmpty())return 'linkedlist is null' let current = this.head.next; let objString = this.head.element; for(let i = 1; i < this.count; i++){ objString = `${objString},${current.element}`; current = current.next; } return objString; }
13.完整程式碼
class LinkedList { constructor(equalsFn = defaultEquals){ this.head = undefined; this.count = 0; this.equalsFn = equalsFn; } size(){ return this.count; } isEmpty(){ return this.size() == 0; } push(element){ let node = new Node(element); if(this.head == undefined){ this.head = node; }else{ let current = this.head; while(current.next!=null){ current = current.next; } current.next = node; } this.count++; } getElementAt(index){ if(index>=0 && index< this.count){ let current = this.head; for(let i = 0; i < index && current != null; i++){ current = current.next; } return current; } return 'index out of range'; } indexOf(element){ let current = this.head; for(let i = 0; i < this.count; i++){ if(current.element === element){ return i; } current = current.next; } return -1; } insert(element,position){ if(position >=0 && position <= this.count){ let node = new Node(element); if(position == 0){ if(this.head === undefined){ this.head = node; }else{ node.next = this.head; this.head = node; } }else if(position == this.count){ let current = this.getElementAt(position - 1); current.next = node; }else{ let previous = this.getElementAt(position - 1); let current = previous.next; node.next = current; previous.next = node; } this.count++; } return "position out of range"; } remove(element){ if(this.isEmpty())return "linkedlist is null"; let index = this.indexOf(element); this.removeAt(index); } removeAt(index){ if(this.isEmpty())return "linkedlist is null"; if(index >= 0 && index < this.count){ if(index == 0){//這裡總是容易被忽略,如果index為0的話,就直接更改this.head this.head = this.head.next; }else{ let previous = this.getElementAt(index -1); let current = previous.next; previous.next = current.next; } this.count--; } return 'index out of range'; } toString(){ let current = this.head.next; let objString = this.head.element; for(let i = 1; i < this.count; i++){ objString = `${objString},${current.element}`; current = current.next; } return objString; } }
14.結果