<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      javascript中在鏈表中向前(向后)移動n個節(jié)點

       1.概念

         在鏈表上移動n個節(jié)點,我第一眼看到這個需求的時候首先想到的是當(dāng)前節(jié)點。使用這個當(dāng)前節(jié)點作為參考來移動,沒有這個當(dāng)前節(jié)點的話是沒有辦法在鏈表上前進和后退的。初始化定義鏈表的時候定義一個當(dāng)前節(jié)點,并且給這個當(dāng)前節(jié)點賦值為頭節(jié)點。向前移動的時候只需要使這個當(dāng)前節(jié)點指向它下一個節(jié)點:this.currentNode = this.currentNode.next; 向后移動節(jié)點只需要使當(dāng)前節(jié)點指向它前一個節(jié)點:this.currentNode = this.currentNode.next; 有了這個思路就好辦了,剩下的只不過要使用循環(huán)控制移動n個節(jié)點就好了,當(dāng)然向后移動的時候要判斷是否到達鏈表末尾,向前移動的時候要判斷是否到達鏈表頭,如果是就停下來,這就說明這個需求有問題了。

        還有顯示當(dāng)前節(jié)點的值,這個就非常容易了,只需要把這個節(jié)點的element打印出來就好了。

      2.代碼實現(xiàn)

      /**
       * 實現(xiàn)在鏈表中向前移動n個節(jié)點和向后移動n個節(jié)點
       * 
       * */
      
      //鏈表節(jié)點
      function Node(element){
          this.element = element;
          this.next = null;
          this.previous = null;
      }
      
      //鏈表
      function LList(){
          this.head = new Node('head');
          this.find = find;
          this.insert = insert;
          this.display = display;
          this.remove = remove;
          this.findLast = findLast;
          this.dispReverse = dispReverse;
          //當(dāng)前節(jié)點就是頭節(jié)點
          this.currentNode = this.head;
          //從鏈表開頭向前移動n個節(jié)點
          this.advance = advance;
          //從鏈表某個節(jié)點向后回退n個節(jié)點
          this.back = back;
          //顯示當(dāng)前節(jié)點
          this.show = show;
      }
      
      //倒序輸出鏈表中的所有節(jié)點
      function dispReverse(){
          var currNode = this.head;
          currNode = this.findLast();
          while (!(currNode.previous == null)){
              document.write(currNode.element + '&nbsp;');
              currNode = currNode.previous;
          }
      }
      
      //找到最后一個節(jié)點
      function findLast(){
          var currNode = this.head;
          while (!(currNode.next == null)){
              currNode = currNode.next;
          }
          return currNode;
      }
      
      //刪除某一個節(jié)點
      function remove(item){
          var currNode = this.find(item);
          if(!(currNode.next == null)){
              currNode.previous.next = currNode.next;
              currNode.next.previous = currNode.previous;
              currNode.next = null;
              currNode.previous = null;
          }
      }
      
      //打印所有鏈表節(jié)點
      function display(){
          var currNode = this.head;
          while (!(currNode.next == null)){
              document.write(currNode.next.element + '&nbsp;');
              currNode = currNode.next;
          }
      }
      
      //找到某一個節(jié)點
      function find(item){
          var currNode = this.head;
          while (currNode.element != item){
              currNode = currNode.next;
          }
          return currNode;
      }
      
      //插入某一個節(jié)點
      function insert(newElement , item){
          var newNode = new Node(newElement);
          var current = this.find(item);
          newNode.next = current.next;
          newNode.previous = current;
          current.next = newNode;
      }
      
      //在鏈表中向前移動n個節(jié)點
      function advance(n){
          while ((n>0) && !(this.currentNode.next==null)){
              this.currentNode = this.currentNode.next; 
              n--
          }
      }
      
      //在鏈表中向后移動n個節(jié)點
      function back(n){
          while (n>0 && !(this.currentNode.element=='head')){
              this.currentNode = this.currentNode.previous;
              n--;
          }
      }
      
      //顯示當(dāng)前節(jié)點
      function show(){
          document.write(this.currentNode.element);
      }
      
      var cities = new LList();
      cities.insert('Conway','head');
      cities.insert('Russellville', 'Conway');
      cities.insert('Carlisle', 'Russellville');
      cities.insert('Alma' , 'Carlisle');
      cities.insert('dezhou' , 'Alma');
      cities.insert('alasijia' , 'dezhou');
      cities.display();
      document.write('<br>');
      
      cities.show();
      cities.advance(4);
      document.write('<br>');
      cities.show();
      cities.back(2);
      document.write('<br>');
      cities.show();

       

      posted @ 2016-10-16 23:42  nd  閱讀(1653)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 天天澡日日澡狠狠欧美老妇| 国产熟睡乱子伦视频在线播放| 亚洲成人资源在线观看| 少妇爆乳无码专区| 欧洲码亚洲码的区别入口| 国产自在自线午夜精品| 国产精品免费视频不卡| 欧美日韩高清在线观看| 艳妇乳肉豪妇荡乳av无码福利| 亚洲最大成人av在线天堂网| 一出一进一爽一粗一大视频| 91午夜福利一区二区三区| 免费看男女做好爽好硬视频| 福利一区二区在线播放| 乱中年女人伦av三区| 日本高清无卡码一区二区| 国产成人亚洲精品狼色在线| 国产成人无码免费视频在线| 久久99久国产精品66| 人人妻人人澡人人爽不卡视频| 美女高潮黄又色高清视频免费| 美日韩精品一区二区三区| 国产乱码日韩亚洲精品成人| 99人体免费视频| 四虎永久播放地址免费| 国产精品久久无中文字幕| 亚洲www永久成人网站| 无码国内精品人妻少妇| 国产精品大片中文字幕| 国产一区二区三区亚洲精品| 久久一区二区中文字幕| 亚洲女初尝黑人巨| 18禁一区二区每日更新| 丰满少妇被猛烈进出69影院| 动漫av纯肉无码av在线播放| 17岁日本免费bd完整版观看| 国产一区二区不卡91| 亚欧美闷骚院| 国产一区二区三区韩国| 日韩a无v码在线播放| 日本五十路熟女一区二区|