一、名詞解釋:鏈表存儲有序的元素集合,但不一樣於數組,鏈表中的元素在內存中並非連續放置的。每一個元素由一個存儲元素自己的節點和一個指向下一個元素的引用(也稱指針或連接)組成。node
二、鏈表功能:數組
三、單項鍊表基本框架:框架
function lb() { function node(elem) { this.element = elem; this.next = null; } let head = null, cur, par; let length = 0; }
this.addNode = function (elem) { let n = new node(elem), cur; if (head === null) { head = n; } else { cur = head; while (cur.next) { cur = cur.next; } cur.next = n; } length++; }
this.remove = function (ele, isNum) { let iN = isNum == false ? false : true; cur = head; if (iN) { //isNum let i = 0; if (ele > -1 && ele < length) { if (ele === 0) { head = cur.next; } else { while (i++ < ele) { par = cur; cur = cur.next; } par.next = cur.next; } } else { return null; } } else { //no is Num while (cur.element != ele) { par = cur; cur = cur.next; } par.next = cur.next; } length--; return cur.element; }
this.insert = function (position, element, isIndex) { let iI = isIndex == false ? false : true; var n = new node(element); let index = 0; cur = head; if (iI) { //position is index if (position == 0) { n.next = cur; head = n; } else if (position == length) { while (cur.next) { cur = cur.next; } cur.next = n; } else { for (let i = 0; i < position; i++) { par = cur; cur = cur.next; } n.next = cur; par.next = n; } length++; } else { //position ist index for (let i = 0; i < length; i++) { if (cur.element == position) { par = cur; cur = cur.next; break; } else { cur = cur.next } } if (par) { n.next = cur par.next = n length++; } else { console.log('鏈表中不存在您選擇的元素') } } }
this.toString = function () { let cur = head, str = ''; while (cur) { str += cur.element + (cur.next ? ' next,' : ' null') cur = cur.next } return str }
this.isEmpty = function () { return length === 0; }
this.size = function () { return length; }
this.getHead = function () { return head; }
四、雙向鏈表:連接是雙向的:一個鏈向下一個元素,另外一個鏈向前一個元this
function Doublelb() { function node(elem) { this.element = elem; this.next = null; this.prev = null; } let head = null, cur, par; let length = 0; let tail = null; }
this.addNode = function (elem) { let n = new node(elem), cur; if (head === null) { n.prev = 'head'; head = n; } else { cur = head; while (cur.next) { cur = cur.next; } cur.next = n; while (cur.prev){ par = cur.element; cur = cur.next; } cur.prev = par; } length++; }
this.remove = function (ele, isNum) { let iN = isNum == false ? false : true; cur = head; if (iN) { //isNum let i = 0; if (ele > -1 && ele < length) { if (ele === 0) { head = cur.next; } else { while (i++ < ele) { par = cur; cur = cur.next; } par.next = cur.next; cur.next.prev = par.element; } } else { return null; } } else { //no is Num while (cur.element != ele) { par = cur; cur = cur.next; } par.next = cur.next; cur.next.prev = par.element; } length--; return cur.element; }
this.insert = function (position, element, isIndex) { let iI = isIndex == false ? false : true; var n = new node(element); let index = 0; cur = head; if (iI) { //position is index if (position == 0) { cur.prev = n.element; n.next = cur; n.prev = 'head'; head = n; } else if (position == length) { while (cur.next) { cur = cur.next; } n.prev = cur.element; cur.next = n; } else { for (let i = 0; i < position; i++) { par = cur; cur = cur.next; } cur.prev = n.element; n.next = cur; n.prev = par.element; par.next = n; } length++; } else { //position ist index for (let i = 0; i < length; i++) { if (cur.element == position) { par = cur; cur = cur.next; break; } else { cur = cur.next; } } if (par) { cur.prev = n.element; n.next = cur; n.prev = par.element; par.next = n; length++; } else { console.log('鏈表中不存在您選擇的元素') } } }
五、單項循環列表spa
this.addNode = function (elem) { let n = new node(elem), cur; if (head === null) { head = n; } else { cur = head; while (cur.next && cur.next != head.element) { cur = cur.next; } cur.next = n; while (cur.next) { cur = cur.next; } cur.next = head.element; } length++; }
this.remove = function (ele, isNum) { let iN = isNum == false ? false : true; cur = head; if (iN) { //isNum let i = 0; if (ele > -1 && ele < length) { if (ele === 0) { head = cur.next; } else { while (i++ < ele) { par = cur; cur = cur.next; } par.next = cur.next; } } else { return null; } } else { //no is Num while (cur.element != ele) { par = cur; cur = cur.next; } par.next = cur.next; } length--; return cur.element; }
this.insert = function (position, element, isIndex) { let iI = isIndex == false ? false : true; var n = new node(element); let index = 0; cur = head; console.log(length) if (iI) { //position is index if (position == 0) { n.next = cur; head = n; } else if (position == length) { while (cur.next&&cur.next!=head.element) { cur = cur.next; } n.next = head.element; cur.next = n; } else if (position > 0 && position < length) { for (let i = 0; i < position; i++) { par = cur; cur = cur.next; } n.next = cur; par.next = n; }else{ console.log('鏈表中不存在您選擇的元素') } length++; } else { //position ist index for (let i = 0; i < length; i++) { if (cur.element == position) { par = cur; cur = cur.next; break; } else { cur = cur.next } } if (par) { n.next = cur par.next = n length++; } else { console.log('鏈表中不存在您選擇的元素') } } }
六、雙向循環列表指針
this.addNode = function (elem) { let n = new node(elem), cur; if (head === null) { n.prev = 'head'; head = n; } else { cur = head; cur.prev = n.element; n.next = head.element; while(cur.next!=head.element&&cur.next){ cur = cur.next; } cur.next = n; while (cur.prev) { par = cur.element; cur = cur.next; } cur.prev = par; } length++; }
this.remove = function (ele, isNum) { let iN = isNum == false ? false : true; cur = head; if (iN) { //isNum let i = 0; if (ele > -1 && ele < length) { if (ele === 0) { head = cur.next; } else { while (i++ < ele) { par = cur; cur = cur.next; } par.next = cur.next; cur.next.prev = par.element; } } else { return null; } } else { //no is Num while (cur.element != ele) { par = cur; cur = cur.next; } par.next = cur.next; cur.next.prev = par.element; } length--; return cur.element; }
this.insert = function (position, element, isIndex) { let iI = isIndex == false ? false : true; var n = new node(element); let index = 0; cur = head; if (iI) { //position is index while(cur.next!=head.element&&cur.next){ cur = cur.next; } cur = head; if (position == 0) { cur.next = n.element; cur.prev = n.element; n.next = cur; n.prev = 'head'; head = n; } else if (position == length) { while (cur.next) { cur = cur.next; } n.prev = cur.element; cur.next = n; } else { for (let i = 0; i < position; i++) { par = cur; cur = cur.next; } cur.prev = n.element; n.next = cur; n.prev = par.element; par.next = n; } length++; } else { //position ist index for (let i = 0; i < length; i++) { if (cur.element == position) { par = cur; cur = cur.next; break; } else { cur = cur.next; } } if (par) { cur.prev = n.element; n.next = cur; n.prev = par.element; par.next = n; length++; } else { console.log('鏈表中不存在您選擇的元素') } } }