美文网首页
《数据结构与算法JavaScript描述》- 第三章 列表练习

《数据结构与算法JavaScript描述》- 第三章 列表练习

作者: 尤小小 | 来源:发表于2018-12-29 18:41 被阅读5次
  1. 添加一个向列表中插入元素的方法,该方法只在待插元素大于列表中的所有元素时才执行插入操作。这里的大于有多重含义,对于数字,它是指数值上的大小;对于字符,它是指在字母表中出现的先后顺序。
  2. 添加一个向列表中插入元素的方法,该方法只在待插元素小于列表中的所有元素时才执行插入操作。
  3. 创建Person类,该类用于保存人的姓名和性别信息。创建一个至少包含10个Person对象的列表。写一个函数显示列表中所有拥有相同性别的人。
  4. 修改本章的影碟租赁程序,当一部影片检出后,将其加入一个已租影片列表,每当有客户检出一部影片,都显示该列表中的内容。
  5. 为影碟租赁程序创建一个check-in()函数,当客户归还一部影片时,将该影片从已租列表中删除,同时添加到现有影片列表中。
{
    // 首先实现列表类
    function List() {
     this.listSize = 0;
     this.pos = 0;
     this.dataStore = [];
     this.clear = clear;
     this.find = find;
     this.toString = toString;
     this.insert = insert;
     this.append = append;
     this.remove = remove;
     this.removePop = removePop;
     this.front = front;
     this.end = end;
     this.prev = prev;
     this.next = next;
     this.length = length;
     this.currPos = currPos;
     this.moveTo = moveTo;
     this.getElement = getElement;
     this.contains = contains;
     this.insertBigElement = insertBigElement;
     this.insertSmallElement = insertSmallElement;
    }
    
    // 清空列表中所有的元素
    function clear() {
     delete this.dataStore
     this.dataStore = []
     this.listSize = this.pos = 0
    }
    
    // 在列表中查找到某一元素的位置
    function find(element) {
     for (var i = 0; i < this.dataStore.length; i++) {
         if (this.dataStore[i] == element) {
             return i
         }
     }
     return -1
    }
    
    // 显示列表中的元素
    function toString() {
     return this.dataStore
    }
    
    // 向列表中某个元素之后(某个元素第一次出现的位置)插入一个新元素
    function insert(element, after) {
     var insertPos = this.find(after)
     if (insertPos > -1) {
         this.dataStore.splice(insertPos, 0, element)
             ++this.listSize
         return true
     }
     return false
    }
    
    // 给列表添加元素
    function append(element) {
     this.dataStore[this.listSize++] = element
    }
    
    // 从列表中删除元素
    function remove(element) {
     var findAt = this.find(element)
    
     if (findAt > -1) {
         this.dataStore.splice(findAt, 1)
             --this.listSize
         return true
     }
     return false
    }
    
    // 按照位置从列表中删除元素
    function removePop (index) {
     if (index > -1) {
         this.dataStore.splice(index, 1)
             --this.listSize
         return true
     }
     return false
    }
    
    // 起始序列号
    function front() {
     this.pos = 0
    }
    
    // 最后一个序列号
    function end() {
     this.pos = this.listSize - 1
    }
    
    // 下一个
    function next() {
     if (this.pos < this.listSize) {
         ++this.pos
     } else {
         return
     }
    }
    
    // 上一个
    function prev() {
     if (this.pos >= 0) {
         --this.pos
     } 
    }
    
    // 列表中有多少个元素
    function length() {
     return this.listSize
    }
    
    // 当前位置
    function currPos() {
     return this.pos
    }
    
    // 指定当前位置
    function moveTo(position) {
     this.pos = position
    }
    
    // 当前位置的元素
    function getElement() {
     return this.dataStore[this.pos]
    }
    
    // 判断给定值是否在列表中
    function contains(element) {
     for (var i = 0; i < this.dataStore.length; i++) {
         if (this.dataStore[i] == element) {
             return true
         }
     }
     return false
    }
    
    // 1. 添加一个向列表中插入元素的方法, 该方法只在待插元素大于列表中的所有元素时才执行插入操作。 这里的大于有多重含义, 对于数字, 它是指数值上的大小; 对于字符, 它是指在字母表中出现的先后顺序。
    
    function insertBigElement(element) {
     return insertCommonElement.call(this, 1, element)
    }
    
    // 2. 添加一个向列表中插入元素的方法, 该方法只在待插元素小于列表中的所有元素时才执行插入操作。
    
    function insertSmallElement(element) {
      return insertCommonElement.call(this, -1, element)
    }
    
    function insertCommonElement(dir, element) {
      let typeofElement = typeof element;
      let tatalNum = 0
      let tempNum = 0
    
      for (let i = 0; i < this.dataStore.length; i++) {
          if (typeof this.dataStore[i] == typeofElement) {
    
              tatalNum++
              if (dir > 0 && element > this.dataStore[i]) {
                  tempNum++
              }
    
              if (dir < 0 && element < this.dataStore[i]) {
                  tempNum++
              }
    
          }
      }
    
      if (tatalNum == tempNum) {
          this.dataStore[this.listSize++] = element
          return true
      } else {
          return false
      }
    }
    
    var tempList = new List()
    tempList.append('gaoxiaohei')
    tempList.append('heihei')
    tempList.append('dalu')
    tempList.append(10)
    tempList.append(23)
    tempList.append(2)
    tempList.append(1)
    
    // 正序遍历
    //  for(tempList.front(); tempList.currPos() < tempList.length(); tempList.next()) {
    //  console.log(tempList.getElement())
    //  }
    
    // 倒序遍历
    //  for(tempList.end(); tempList.currPos() >= 0; tempList.prev()) {
    //      console.log(tempList.getElement())
    //  }
    
    // 第1题的测试用例
    //  console.log(tempList.insertBigElement(10)) // false
    //  console.log(tempList.toString()) //  ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1]
    //  console.log(tempList.insertBigElement(100)) // true
    //  console.log(tempList.toString()) // ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100]
    //  console.log(tempList.insertBigElement('zzz')) // true
    //  console.log(tempList.toString()) // ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100, "zzz"]
    //  console.log(tempList.insertBigElement('o')) // false
    //  console.log(tempList.toString()) // ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100, "zzz"]
    
    // 第2题的测试用例
    //  console.log(tempList.insertSmallElement(0)) // true
    //  console.log(tempList.toString()) //  ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100, "zzz", 0]
    //  console.log(tempList.insertSmallElement(0)) // false
    //  console.log(tempList.toString()) //  ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100, "zzz", 0]
    //  console.log(tempList.insertSmallElement('ao')) // true
    //  console.log(tempList.toString()) // ["gaoxiaohei", "heihei", "dalu", 10, 23, 2, 1, 100, "zzz", 0, "ao"]
    
    
    //  3. 创建Person类, 该类用于保存人的姓名和性别信息。 创建一个至少包含10个Person对象的列表。 写一个函数显示列表中所有拥有相同性别的人。
    let person = new List()
    
    person.append({name: ' 张臭臭',gender: '男'})
    person.append({name: '高小黑',gender: '男'})
    person.append({name: '高密密',gender: '女'})
    person.append({name: '陈宇',gender: '男'})
    person.append({name: '周亚楠',gender: '男'})
    person.append({name: '王思远',gender: '男'})
    person.append({name: '李冬',gender: '男'})
    person.append({name: '王素环',gender: '女'})
    person.append({name: '毛小华',gender: '男'})
    person.append({name: '张赛磊',gender: '男'})
    
    function sameGender (gender) {
     if (!gender) return
     let newArr = []
    
     for(person.front(); person.currPos() < person.length(); person.next()) {
         let item = person.getElement()
         
         if(item.gender == gender) {
            newArr.push(item)
         }
     
     }
    
     return newArr
    }
    // 第3题的测试用例
    // console.log(sameGender('男'))
    // console.log(sameGender('女'))
    
    // 4. 修改本章的影碟租赁程序, 当一部影片检出后, 将其加入一个已租影片列表, 每当有客户检出一部影片, 都显示该列表中的内容。
    // 先把影碟租赁的程序写出来,可以运行再做作业
    var movies = [
    '肖申克的救赎',
    '教父',
    '教父2',
    '低俗小说',
    '黄金三嫖客',
    '十二怒汉',
    '辛德勒名单',
    '黑暗骑士',
    '指环王:王者归来',
    '搏击俱乐部',
    '星球大战5:帝国反击战',
    '飞越疯人院',
    '指环王:护戒使者',
    '盗梦空间',
    '好家伙',
    '星球大战',
    '七武士',
    '黑客帝国',
    '阿甘正传',
    '上帝之城',
    ]
    
    var movieList = new List()
    var customers = new List()
    
    for(let i = 0; i < movies.length; i++) {
    movieList.append(movies[i])
    }
    
    function Customer(name, movie) {
    this.name = name
    this.movie = movie 
    }
    
    // 影碟清单函数
    function displayList(list){
    for (list.front(); list.currPos() < list.length(); list.next()) {
        if (list.getElement() instanceof Customer) {
            let temp = list.getElement()
            console.log(temp['name'] + ', ' + temp['movie'])
        } else {
            console.log(list.getElement())
        }
    }
    }
    
    // 查询影片函数
    function checkOut(name, movie, movieList, customerList) {
    if (movieList.contains(movie)) {
    
        var c = new Customer(name, movie)
        customers.append(c) // 加入到客户列表,已租影片列表
    
        // displayList(movieList) // 查看电影列表
    
        movieList.remove(movie) // 从影碟列表删除
    } else {
        console.log(movie + ' 影片被借出.')
    }
    }
    
    // displayList(movieList)
    checkOut('xiaohei','盗梦空间', movieList, customers)
    checkOut('臭臭', '教父', movieList, customers)
    
    
    // 4. 修改本章的影碟租赁程序, 当一部影片检出后, 将其加入一个已租影片列表, 每当有客户检出一部影片, 都显示该列表中的内容。
    // 调整checkOut()方法 在此方法中去显示列表即可
    // 5. 为影碟租赁程序创建一个check-in() 函数, 当客户归还一部影片时, 将该影片从已租列表中删除, 同时添加到现有影片列表中。
    
    function checkIn(name, movie, movieList, customerList) {
    for (customerList.front(); customerList.currPos() < customerList.length(); customerList.next()){
        let item = customerList.getElement()
    
        if (item.name == name || item.movie == movie) {
            let c = new Customer(name, movie)
    
            customerList.removePop(customerList.currPos()) // 从用户列表删除
            movieList.append(movie) // 加入到影片列表
    
            displayList(movieList) // 查看电影列表
            displayList(customers) // 查看用户列表
    
        } else {
            console.log(movie + ' 不是这的影片.')
        }
    }
}

    checkIn('xiaohei', '教父', movieList, customers)
    
}

第5题最难,难在还影片的时候,将影片从已出租的列表中删除,这次删除不是删除一个普通元素,而是删除一个实例化对象元素,就不能直接使用列表类的remove方法了。我们给列表类添加一个删除方法,通过位置删除元素。其中对列表的操作都是通过迭代器,而不应该为列表直接添加或者删除元素。迭代器只是用来在列表上随意移动。这一点切记。

列表的使用场景:待办事项列表、购物清单等。对一个序列查找排序列表尤为重要。

相关文章

网友评论

      本文标题:《数据结构与算法JavaScript描述》- 第三章 列表练习

      本文链接:https://www.haomeiwen.com/subject/bhmplqtx.html