列表類的實現

第3章——列表類的實現

首先定義一個List類,在構造函數constructor體內設置2個變量dataStorelistSize分別用來初始化一個空數組保存列表元素列表元素個數,之後是一些方法的實現:👇👇👇

class List {
	 constructor() {
	     this.dataStore = [];   //初始化一個空數組保存列表元素
	     this.listSize = 0;     //列表元素個數
	 }
	 <!--方法-->
	 
	 // apend()
	 
	 //find()
	 
	 ......

}

append()

  • List類裏面添加append()方法

     append(element) {
        this.dataStore[this.listSize++] = element;
    }
    
  • 實踐(增加幾條數據)

    let newList = new List();
    newList.append('Anna');
    newList.append('Jack');
    console.log(newList);
    
  • 結果
    在這裏插入圖片描述

find()

  • List類裏面添加find()

    find(element) {
        for (let i = 0; i < this.dataStore.length; i++) {
            if (this.dataStore[i] == element)
                return i;
        }
        return -1;
    }
    
  • 實踐(查找上面添加的某條數據)

    let findData=newList.find("Anna");
    console.log(findData);   //0
    

remove()

  • List類裏面添加remove()

     remove(element) {
        let foundAt = this.find(element);
        if (foundAt > -1) {
            this.dataStore.splice(foundAt, 1);
            --this.listSize;
            return true;
        }
        return false;
    }
    
  • 實踐(刪除上面增加的某條數據)

    let removeData=newList.remove("Kan");
    console.log(removeData);   //true
    console.log(newList);
    
  • 效果

    在這裏插入圖片描述

length()

  • List類裏面添加length()

     length() {
         return this.listSize;
     }
    
  • 實踐

    let length=newList.length();
    console.log(length);  //4
    

toString() — 顯示列表中的元素

  • List類裏面添加toString()

     toString() {
       return this.dataStore;
    }
    
  • 實踐

    let stringData=newList.toString();
    console.log(stringData);  // ["Anna", "Jack", "Minni", "Kan"]
    

insert()

insert()方法用到了 find()方法,find()方法會尋找傳入的after 參數在列 表中的位置,找到該位置後,使用 splice()方法將新元素插入該位置之後,然後將變量listSize1並返回true,表明插入成功。

  • List類裏面添加insert()

    insert(element, after) {
        let insertPos = this.find(after);
        if (insertPos > -1) {
            this.dataStore.splice(insertPos + 1, 0, element);
            ++this.listSize;
            return true;
        }
    
        return false;
    }
    
  • 實踐

    比如我想在Anna 後面 插入Linda:

    let insertData=newList.insert("Linda",'Anna');
    console.log(insertData);
    console.log(newList);
    
  • 效果

    在這裏插入圖片描述

clear()

  • List類裏面添加clear()

    clear() {
       delete this.dataStore;
       this.dataStore = [];
       this.listSize =  0;
    }
    
  • 實踐

    let clearData=newList.clear();
    console.log(newList);
    
  • 效果

    在這裏插入圖片描述

contains

  • List類裏面添加contains()

    contains(element) {
      for (let i = 0; i < this.dataStore.length; i++) {
          if (this.dataStore[i] == element) {
              return true;
          }
      }
      return false;
    }
    
  • 實踐

    let containsData=newList.contains('Anna');
    console.log(containsData);  //true
    

遍歷列表

以下方法允許用戶在列表上自由移動,getElement() 返回列表的當前元素:

  • List類裏面添加以下方法:

    	// 1)front()——最前位置
        front() {
            this.pos = 1;
        }
        // 2)end()——最後位置
        end() {
            this.pos = this.listSize - 1;
        }
    
        // 3)prev()—— 移動至前一個位置
        prev() {
            if (this.pos > 0) {
                --this.pos;
            }
        }
    
        // 4)next()—— 移動至下一個位置
        next() {
            if (this.pos < this.listSize - 1) {
                ++this.pos;
            }
        }
    
        // 5)currPos()—— 當前位置
        currPos() {
            return this.pos;
        }
    
        // 6)moveTo()——  移動至指定位置
        moveTo(position) {
            this.pos = position;
        }
    
        // 7)getElement()—— 獲取當前元素
        getElement() {
            return this.dataStore[this.pos];
        }
    
  • 實踐

    調用以上任意方法後若想返回該元素,得使用getElement()方法:

    //調用front()
    let frontData = newList.front();
    console.log(newList.getElement());   //Jack
    
    //調用end()
     newList.end();
     console.log(newList.getElement());    //Kan
    
    //調用prev()——調用之前得調用上面2個任意方法,得知道this.pos的值
     newList.prev();    
     console.log(newList.getElement());   //調用這個之前看調用了front()還是end(),若是前者該結果是Anna,若是後者該結果是Minni
    
    //調用next()
     newList.next();
     console.log(newList.getElement());   //Minni
    
    //調用currPos()
     newList.currPos();
     console.log(newList.getElement());    //Jack
    
    //調用moveTo()
    newList.moveTo(0);
    console.log(newList.getElement());  //Anna
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章