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

ops/2024/10/17 19:32:25/

1. 增加一个向列表中插入元素的方法,该方法只在待插元素大于列表中的所有元素时才执 行插入操作。这里的大于有多重含义,对于数字,它是指数值上的大小;对于字母,它 是指在字母表中出现的先后顺序。

javascript">function isGreaterThan(a, b) {if (typeof a === 'number' && typeof b === 'number') {return a > b;} else if (typeof a === 'string' && typeof b === 'string') {return a.localeCompare(b) > 0;} else {throw new Error('Unsupported data type');}
}function insertIfGreaterThanAll(element) {for (var i = 0; i < this.dataStore.length; i++) {if (!isGreaterThan(element, this.dataStore[i])) {return false;}}this.append(element);return true;
}

2. 增加一个向列表中插入元素的方法,该方法只在待插元素小于列表中的所有元素时才执 行插入操作。

javascript">function isLessThan(a, b) {if (typeof a === 'number' && typeof b === 'number') {return a < b;} else if (typeof a === 'string' && typeof b === 'string') {return a.localeCompare(b) < 0;} else {throw new Error('Unsupported data type');}
}function insertIfLessThanAll(element) {for (var i = 0; i < this.dataStore.length; i++) {if (!isLessThan(element, this.dataStore[i])) {return false;}}this.append(element);return true;
}

3. 创建 Person 类,该类用于保存人的姓名和性别信息。创建一个至少包含 10 个 Person 对 象的列表。写一个函数显示列表中所有拥有相同性别的人。

javascript">function Person(name, gender) {this.name = name;this.gender = gender;
}function displayPeopleByGender(peopleList, gender) {for (peopleList.front(); peopleList.currPos() < peopleList.length(); peopleList.next()) {if (peopleList.getElement().gender === gender) {console.log(gender + "性:" + peopleList.getElement().name);}}
}var list = new List();
list.append(new Person("张三", "男"));
list.append(new Person("李四", "女"));
list.append(new Person("王五", "男"));
list.append(new Person("赵六", "女"));
list.append(new Person("刘七", "男"));
list.append(new Person("陈八", "女"));
list.append(new Person("周九", "男"));
list.append(new Person("吴十", "女"));
list.append(new Person("郑十一", "男"));
list.append(new Person("孙十二", "女"));
displayPeopleByGender(list, "男");
displayPeopleByGender(list, "女");

4. 修改本章的影碟租赁程序,当一部影片检出后,将其加入一个已租影片列表。每当有客 户检出一部影片,都显示该列表中的内容。

javascript">function checkOut(name, movie, filmList, customerList, rentList) {if (filmList.contains(movie)) {var c = new Customer(name, movie);customerList.append(c);rentList.append(movie);filmList.remove(movie);console.log("Rented movies: ");displayList(rentList);} else {console.log(movie + " is not available.");}
}

5. 为影碟租赁程序创建一个 check-in() 函数,当客户归还一部影片时,将该影片从已租列 表中删除,同时添加到现有影片列表中。

javascript">function checkIn(name, movie, filmList, customerList, rentList) {if (rentList.contains(movie)) {rentList.remove(movie);filmList.append(movie);console.log(movie + " has been returned.\n");console.log("rent movies: \n");displayList(rentList);for (customerList.front(); customerList.currPos() < customerList.length(); customerList.next()) {if (customerList.getElement().movie == movie) {customerList.remove(customerList.getElement());}}console.log("\nCustomer Rentals: \n");displayList(customers);} else {console.log(movie + " is not rented.\n");}
}

List不是JavaScript的内置类型,记录一下数组实现的List对象:

javascript">
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.front = front; this.end = end; this.prev = prev; this.next = next; this.length = length; this.currPos = currPos; this.moveTo = moveTo; this.getElement = getElement; this.length = length; this.contains = contains; this.insertIfGreaterThanAll = insertIfGreaterThanAll;this.insertIfLessThanAll = insertIfLessThanAll;
}function append(element) { this.dataStore[this.listSize++] = element; 
}
function find(element) { for (var i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] == element) { return i; } } return -1; 
}
function remove(element) { var foundAt = this.find(element); if (foundAt > -1) { this.dataStore.splice(foundAt,1); --this.listSize; return true; } return false; 
}
function length() { return this.listSize; 
}
function toString() { return this.dataStore; 
}
function insert(element, after) { var insertPos = this.find(after); if (insertPos > -1) { this.dataStore.splice(insertPos+1, 0, element); ++this.listSize; return true; } return false; 
}
function clear() { delete this.dataStore; this.dataStore = []; this.listSize = this.pos = 0; 
}
function contains(element) { for (var i = 0; i < this.dataStore.length; ++i) { if (this.dataStore[i] == element) { //if (this.dataStore[i].indexOf(element) >= 0) { return true; } } return false; 
}
function front() { this.pos = 0; 
} function end() { this.pos = this.listSize-1; 
} function prev() { if (this.pos > 0) { --this.pos; } 
} function next() { 
//	if (this.pos < this.listSize-1) { ++this.pos; 
//	} 
} function currPos() { return this.pos; 
} function moveTo(position) { this.pos = position; 
} function getElement() { return this.dataStore[this.pos]; 
}function isGreaterThan(a, b) {if (typeof a === 'number' && typeof b === 'number') {return a > b;} else if (typeof a === 'string' && typeof b === 'string') {return a.localeCompare(b) > 0;} else {throw new Error('Unsupported data type');}
}function isLessThan(a, b) {if (typeof a === 'number' && typeof b === 'number') {return a < b;} else if (typeof a === 'string' && typeof b === 'string') {return a.localeCompare(b) < 0;} else {throw new Error('Unsupported data type');}
}function insertIfGreaterThanAll(element) {for (var i = 0; i < this.dataStore.length; i++) {if (!isGreaterThan(element, this.dataStore[i])) {return false;}}this.append(element);return true;
}function insertIfLessThanAll(element) {for (var i = 0; i < this.dataStore.length; i++) {if (!isLessThan(element, this.dataStore[i])) {return false;}}this.append(element);return true;
}var films = "The Shawshank Redemption(《肖申克的救赎》)                    \n\The Godfather(《教父》)                                                   \n \The Godfather: Part II(《教父 2》)                                         \n\Pulp Fiction(《低俗小说》)                                                 \n\The Good, the Bad and the Ugly(《黄金三镖客》)                             \n\12 Angry Men(《十二怒汉》 )                                                \n\Schindler’s List(《辛德勒名单》)                                           \n\The Dark Knight(《黑暗骑士》)                                              \n\The Lord of the Rings: The Return of the King(《指环王:王者归来》)        \n\Fight Club(《搏击俱乐部》)                                                \n\Star Wars: Episode V - The Empire Strikes Back(《星球大战 5:帝国反击战》)\n\One Flew Over the Cuckoo’s Nest(《飞越疯人院》)                           \n\The Lord of the Rings: The Fellowship of the Ring(《指环王:护戒使者》)   \n\Inception(《盗梦空间》)                                                   \n\Goodfellas(《好家伙》)                                                    \n\Star Wars(《星球大战》)                                                   \n\Seven Samurai(《七武士》)                                                 \n\The Matrix(《黑客帝国》)                                                  \n\Forrest Gump(《阿甘正传》)                                                \n\City of God(《上帝之城》)                                                 \n"
function createArr(file) {var arr = file.split("\n");for (var i = 0; i < arr.length; i++) {arr[i] = arr[i].trim();}return arr;
}
function Customer(name, movie) {this.name = name;this.movie = movie;
}
function checkOut(name, movie, filmList, customerList, rentList) {if (filmList.contains(movie)) {var c = new Customer(name, movie);customerList.append(c);rentList.append(movie);filmList.remove(movie);console.log("Rented movies: ");displayList(rentList);} else {console.log(movie + " is not available.");}
}
function checkIn(name, movie, filmList, customerList, rentList) {if (rentList.contains(movie)) {rentList.remove(movie);filmList.append(movie);console.log(movie + " has been returned.\n");console.log("rent movies: \n");displayList(rentList);for (customerList.front(); customerList.currPos() < customerList.length(); customerList.next()) {if (customerList.getElement().movie == movie) {customerList.remove(customerList.getElement());}}console.log("\nCustomer Rentals: \n");displayList(customers);} else {console.log(movie + " is not rented.\n");}
}
function displayList(list) {for (list.front(); list.currPos() < list.length(); list.next()) {if (list.getElement() instanceof Customer) {console.log(list.getElement()["name"] + ", " + list.getElement()["movie"]);} else {console.log(list.getElement());}}
}var movies = createArr(films);
var movieList = new List();
var customers = new List();
var rentList = new List();
for (var i = 0; i < movies.length; i++) {movieList.append(movies[i]);
}
console.log("Available movies: \n");
displayList(movieList);
checkOut("Jane Doe", "The Godfather(《教父》)", movieList, customers, rentList);
console.log("\nCustomer Rentals: \n");
displayList(customers);
console.log("\nAvailable movies: \n");
displayList(movieList);
console.log("\n-----------------------------------------------------\n");
checkIn("Jane Doe", "The Godfather(《教父》)", movieList, customers, rentList);
console.log("Available movies: \n");
displayList(movieList);


http://www.ppmy.cn/ops/123874.html

相关文章

安宝特方案 | AR技术在轨交行业的应用优势

随着轨道交通行业不断向智能化和数字化转型&#xff0c;传统巡检方式的局限性日益凸显。而安宝特AR眼镜以其独特的佩戴方式和轻便设计&#xff0c;为轨道交通巡检领域注入了创新活力&#xff0c;提供了全新的解决方案。 01 多样化佩戴方法&#xff0c;完美适应户外环境 安宝特…

如何在数据库表中设置非空约束?

当你设计数据库模式时&#xff0c;确保数据的完整性和一致性是非常重要的。非空约束&#xff08;NOT NULL&#xff09;是一种保证字段值不为空的方法&#xff0c;这对于维护数据的质量至关重要。下面我将从几个方面来详细解答这个问题。 1. 使用SQL语句定义非空约束 在创建新…

JavaWeb - 8 - 请求响应 分层解耦

请求响应 请求&#xff08;HttpServletRequest&#xff09;&#xff1a;获取请求数据 响应&#xff08;HttpServletResponse&#xff09;&#xff1a;设置响应数据 BS架构&#xff1a;Browser/Server&#xff0c;浏览器/服务器架构模式。客户端只需要浏览器&#xff0c;应用程…

基于单片机的山林远程环境监测仪设计

本设计基于单片机的智能化的远程山林环境检测仪&#xff0c;该检测仪由硬件系统和软件系统构成。电源管理模块给整个硬件系统提供工作所需电源&#xff0c;系统可完成山林环境有关的温度、湿度、火焰和海拔高度的采集&#xff0c;并且可通过与按键设置阈值作对比判断危险情况&a…

Unity实现自定义图集(三)

以下内容是根据Unity 2020.1.0f1版本进行编写的   1、实现编辑器模式下进游戏前Pack全部自定义图集 同Unity的图集一样,Unity的编辑器模式会在进游戏前把全部的SpriteAtlas都打一次图集,如图: 我们也实现这样的效果。 首先需要获取全部的图集路径。因为目前使用的是以.…

JAVA学习-练习试用Java实现“反转链表 II”

问题&#xff1a; 给定单链表的头指针 head 和两个整数 left 和 right &#xff0c;其中 left < right 。请你反转从位置 left 到位置 right 的链表节点&#xff0c;返回 反转后的链表 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5], left 2, right 4 输出…

【NIO基础】基于 NIO 中的组件实现对文件的操作(文件编程),FileChannel 详解

目录 1、FileChannel (1&#xff09;获取 FileChannel (2&#xff09;读取文件 (3&#xff09;写入文件 (4&#xff09;关闭通道 (5&#xff09;当前位置与文件大小 (6&#xff09;强制写入磁盘 2、两个 FileChannel 之间的数据传输 (1&#xff09;使用 transferTo()…

Flink和elasticsearch的关系

Apache Flink 和 Elasticsearch 通常被一起用于实时数据处理和搜索的场景。Flink 是一个流处理框架&#xff0c;能够处理大规模的实时数据流&#xff0c;而 Elasticsearch 是一个基于 Lucene 的搜索引擎&#xff0c;它提供了分布式的全文搜索能力&#xff0c;通常用于日志数据或…