4、鏈表

一、名詞解釋:鏈表存儲有序的元素集合,但不一樣於數組,鏈表中的元素在內存中並非連續放置的。每一個元素由一個存儲元素自己的節點和一個指向下一個元素的引用(也稱指針或連接)組成。node

二、鏈表功能:數組

  1. 添加節點:addNode()
  2. 移除節點:remove()
  3. 插入節點:insert()
  4. 將鏈表變成字符串:toString()
  5. 判斷鏈表是否存在節點:isEmpty()
  6. 返回鏈表長度:size()
  7. 返回鏈表:getHead()

三、單項鍊表基本框架:框架

function lb() {
    function node(elem) {
        this.element = elem;
        this.next = null;
    }
    let head = null,
        cur, par;
    let length = 0;
}
  1. 添加節點:若是鏈表爲空,添加到head節點下,不然添加到最後的節點下。
    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++;
        }

     

  2. 移除節點:判斷傳入的是鏈表的下標位置,仍是元素的內容。默認如下標查找元素。
    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;
        }

     

  3. 插入節點:判斷傳入的是鏈表的下標位置,仍是元素的內容。默認如下標查找元素。
    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('鏈表中不存在您選擇的元素')
                }
            }
        }

     

  4. 將鏈表變成字符串輸出:
    this.toString = function () {
            let cur = head,
                str = '';
            while (cur) {
                str += cur.element + (cur.next ? ' next,' : ' null')
                cur = cur.next
            }
            return str
        }

     

  5. 判斷鏈表的內容是否爲空:
    this.isEmpty = function () {
            return length === 0;
        }

     

  6. 顯示鏈表的長度:
    this.size = function () {
            return length;
        }

     

  7. 獲取鏈表的內容:
    this.getHead = function () {
            return head;
        }

     

 四、雙向鏈表:連接是雙向的:一個鏈向下一個元素,另外一個鏈向前一個元this

  • 基本框架:
    1. 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;
      }

       

    2. 添加節點:
      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++;
          }
    3. 移除節點:
      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;
          }
    4. 插入節點
      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

  1. 添加元素
    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++;
        }

     

  2. 移除元素
    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;
        }

     

  3. 插入元素
    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('鏈表中不存在您選擇的元素')
                }
            }
        }

     

六、雙向循環列表指針

  1. 添加元素
    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++;
        }

     

  2. 移除元素
    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;
        }

     

  3. 插入元素
    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('鏈表中不存在您選擇的元素')
                }
            }
        }
相關文章
相關標籤/搜索