1.增加一个向列表中插入元素的方法,该方法只在待插元素大于列表中所有元素时才执行插入操作。这里的大于有多重含义,对于数字,它是指数值上的大小; 对于字母,它是指在字母表中出现的先后顺序。
////////////////////////////列表定义开始///////////////////////////////////
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.hasNext = hasNext;
this.hasPrev = hasPrev;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
this.insertLargerThen = insertLargerThen;
this.insertSmallerThen = insertSmallerThen;
}
// 给列表添加元素
function append(element){
this.dataStore[this.listSize++] = element;
}
// 从列表中删除元素
function remove(element){
var foundAt = this.find(element);
if (foundAt > -1){
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
// 查找元素
function find(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return i;
}
}
return -1;
}
// 返回元素个数
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 insertLargerThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] > element){
return false;
}
}
this.dataStore.push(element);
return true;
}
else if(type === 'string'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] > element){
return false;
}
}
this.dataStore.push(element);
return true;
}
}
function insertSmallerThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] < element){
return false;
}
}
this.dataStore.push(element);
return true;
}
else if(type === 'string'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] < element){
return false;
}
}
this.dataStore.push(element);
return true;
}
}
// 清空列表
function clear(){
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
// 判断给定值是否在列表内
function contains(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return true;
}
}
return false;
}
function front(){
this.pos = 0;
}
function end(){
this.pos = this.listSize - 1;
}
function prev(){
--this.pos;
}
function next(){
if (this.pos < this.listSize){
++ this.pos;
}
}
function currPos(){
return this.pos;
}
function moveTo(position){
this.pos = position;
}
function getElement(){
return this.dataStore[this.pos];
}
function hasNext(){
return this.pos < this.listSize;
}
function hasPrev(){
return this.pos > 0;
}
//////////////////////////////////列表定义结束////////////////////////////////////
// 测试
let DataThen = new List();
DataThen.append(`Mazey`);
DataThen.append(`Cherrie`);
DataThen.append(`Luna`);
DataThen.append(`John`);
DataThen.append(`July`);
DataThen.append(23);
DataThen.append(73);
print("origin:"+DataThen.toString());// origin:Mazey,Cherrie,Luna,John,July,23,73
//插入两个数字测试一下
DataThen.insertLargerThen(99);
DataThen.insertLargerThen(12);
print("insert two nums:"+DataThen.toString()); // insert two nums:Mazey,Cherrie,Luna,John,July,23,73,99
//插入两个字符串测试一下
DataThen.insertLargerThen(`Jay`);
DataThen.insertLargerThen(`Zero`);
print("insert two strings:"+DataThen.toString()); //insert two strings:Mazey,Cherrie,Luna,John,July,23,73,99,Zero
2.增加一个向列表中插入元素的方法,该方法只在待插入元素小于列表中的所有元素时才能进行插入操作。
////////////////////////////列表定义开始///////////////////////////////////
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.hasNext = hasNext;
this.hasPrev = hasPrev;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
this.insertLargerThen = insertLargerThen;
this.insertSmallerThen = insertSmallerThen;
}
// 给列表添加元素
function append(element){
this.dataStore[this.listSize++] = element;
}
// 从列表中删除元素
function remove(element){
var foundAt = this.find(element);
if (foundAt > -1){
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
// 查找元素
function find(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return i;
}
}
return -1;
}
// 返回元素个数
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 insertLargerThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] > element){
return false;
}
}
this.dataStore.push(element);
return true;
}
else if(type === 'string'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] > element){
return false;
}
}
this.dataStore.push(element);
return true;
}
}
function insertSmallerThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] < element){
return false;
}
}
this.dataStore.push(element);
return true;
}
else if(type === 'string'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] < element){
return false;
}
}
this.dataStore.push(element);
return true;
}
}
// 清空列表
function clear(){
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
// 判断给定值是否在列表内
function contains(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return true;
}
}
return false;
}
function front(){
this.pos = 0;
}
function end(){
this.pos = this.listSize - 1;
}
function prev(){
--this.pos;
}
function next(){
if (this.pos < this.listSize){
++ this.pos;
}
}
function currPos(){
return this.pos;
}
function moveTo(position){
this.pos = position;
}
function getElement(){
return this.dataStore[this.pos];
}
function hasNext(){
return this.pos < this.listSize;
}
function hasPrev(){
return this.pos > 0;
}
//////////////////////////////////列表定义结束////////////////////////////////////
// 测试
let DataThen = new List();
DataThen.append(`Mazey`);
DataThen.append(`Cherrie`);
DataThen.append(`Luna`);
DataThen.append(`John`);
DataThen.append(`July`);
DataThen.append(23);
DataThen.append(73);
print(DataThen.toString()); //Mazey,Cherrie,Luna,John,July,23,73
//插入两个数字测试一下
DataThen.insertSmallerThen(99);
DataThen.insertSmallerThen(12);
print(DataThen.toString()); // Mazey,Cherrie,Luna,John,July,23,73,12
//插入两个字符串测试一下
DataThen.insertSmallerThen(`Jay`);
DataThen.insertSmallerThen(`Zero`);
print(DataThen.toString()); //Mazey,Cherrie,Luna,John,July,23,73,12
3.创建Person类,该类用于保存人的姓名和性别信息。创建一个至少包含10个Person对象的列表。写一个显示列表中多有拥有相同性别的人。
////////////////////////////列表定义开始///////////////////////////////////
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.hasNext = hasNext;
this.hasPrev = hasPrev;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
}
// 给列表添加元素
function append(element){
this.dataStore[this.listSize++] = element;
}
// 从列表中删除元素
function remove(element){
var foundAt = this.find(element);
if (foundAt > -1){
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
// 查找元素
function find(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return i;
}
}
return -1;
}
// 返回元素个数
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 insertThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] > element){
return false;
}
this.dataStore.append(element);
return true;
}
}
else {
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] > element){
return false;
}
this.dataStore.append(element);
return true;
}
}
}
// 清空列表
function clear(){
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
// 判断给定值是否在列表内
function contains(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return true;
}
}
return false;
}
function front(){
this.pos = 0;
}
function end(){
this.pos = this.listSize - 1;
}
function prev(){
--this.pos;
}
function next(){
if (this.pos < this.listSize){
++ this.pos;
}
}
function currPos(){
return this.pos;
}
function moveTo(position){
this.pos = position;
}
function getElement(){
return this.dataStore[this.pos];
}
function hasNext(){
return this.pos < this.listSize;
}
function hasPrev(){
return this.pos > 0;
}
//////////////////////////////////列表定义结束////////////////////////////////////
//////////////////////////////////定义PersonList////////////////////////////////
// 创建Person对象
function Person(name, gender){
this.name = name;
this.gender = gender;
}
function PersonClass(){
this.list = new List();
this.showSameGender = showSameGender;
this.addPerson = addPerson;
}
function addPerson(name, gender){
var c = new Person(name, gender);
this.list.append(c);
}
function showSameGender(gender){
var personList = this.list;
var list = new List();
for (personList.front(); personList.hasNext();personList.next()){
if (personList.getElement().gender == gender){
list.append(personList.getElement().name);
}
}
print(gender + ":" + list.toString());
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////测试/////////////////////////////////////////////
var personList = new PersonClass();
personList.addPerson("libai","male");
personList.addPerson("jinke","female");
personList.addPerson("yangjian","male");
personList.addPerson("sunwukong","male");
personList.addPerson("zhenji","female");
personList.addPerson("machao","male");
personList.addPerson("wuzetian","female");
personList.addPerson("yangyuhuan","female");
personList.addPerson("peiqinhu","male");
personList.addPerson("hanxin","male");
personList.addPerson("laofuzi","male");
personList.addPerson("guanyu","male");
personList.addPerson("jing","female");
personList.addPerson("dongfangyao","male");
personList.addPerson("houyi","male");
personList.addPerson("jialuo","female");
personList.addPerson("sunshangxiang","female");
personList.showSameGender("male");// male:libai,yangjian,sunwukong,machao,peiqinhu,hanxin,laofuzi,guanyu,dongfangyao,houyi
personList.showSameGender("female");//female:jinke,zhenji,wuzetian,yangyuhuan,jing,jialuo,sunshangxiang
4和5.影碟租赁程序
////////////////////////////列表定义开始///////////////////////////////////
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.hasNext = hasNext;
this.hasPrev = hasPrev;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
}
// 给列表添加元素
function append(element){
this.dataStore[this.listSize++] = element;
}
// 从列表中删除元素
function remove(element){
var foundAt = this.find(element);
if (foundAt > -1){
this.dataStore.splice(foundAt,1);
--this.listSize;
return true;
}
return false;
}
// 查找元素
function find(element){
// if (element instanceof Customer){
// for (var i = 0; i < this.dataStore.length; ++i){
// if (this.dataStore[i]["name"] == element["name"] && this.dataStore[i]["movie"] == element["movie"]){
// return i;
// }
// }
// return -1;
// }
if (element instanceof Customer){
for (this.front();this.hasNext();this.next()){
if (this.getElement().name == element.name && this.getElement().movie == element.movie){
return this.currPos();
}
}
return -1;
}
else{
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
// print(i);
return i;
}
}
return -1;
}
}
// 返回元素个数
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 insertThen(element){
let type = typeof element;
if (type === 'number'){
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'number' && this.dataStore[i] > element){
return false;
}
this.dataStore.append(element);
return true;
}
}
else {
for (var i = 0; i < this.dataStore.length; ++i){
if (typeof this.dataStore[i] === 'string' && this.dataStore[i] > element){
return false;
}
this.dataStore.append(element);
return true;
}
}
}
// 清空列表
function clear(){
delete this.dataStore;
this.dataStore.length = 0;
this.listSize = this.pos = 0;
}
// 判断给定值是否在列表内
function contains(element){
for (var i = 0; i < this.dataStore.length; ++i){
if (this.dataStore[i] == element){
return true;
}
}
return false;
}
function front(){
this.pos = 0;
}
function end(){
this.pos = this.listSize - 1;
}
function prev(){
--this.pos;
}
function next(){
if (this.pos < this.listSize){
++ this.pos;
}
}
function currPos(){
return this.pos;
}
function moveTo(position){
this.pos = position;
}
function getElement(){
return this.dataStore[this.pos];
}
function hasNext(){
return this.pos < this.listSize;
}
function hasPrev(){
return this.pos > 0;
}
//////////////////////////////////列表定义结束////////////////////////////////////
// 测试
// var names = new List();
// names.append("Clayton");
// names.append("Raymond");
// names.append("Cynthia");
// names.append("Jennnifer");
// names.append("Bryan");
// names.append("Danny");
// names.front();
// print(names.getElement());
// names.next();
// names.next();
// names.next();
// print(names.getElement());
// names.prev();
// print(names.getElement());
// for (names.front(); names.hasNext();names.next()){
// print(names.pos+1 +": " + names.getElement());
// }
// for (names.end(); names.hasPrev(); names.prev()){
// print(names.getElement());
// }
// print(movies);
// 影碟租赁
//读取数据
function createArr(file){
var arr = read(file).split("\n");
for (var i = 0; i < arr.length; ++i){
arr[i] = arr[i].trim();
}
return arr;
}
//读取电影片名
var movies = createArr("film.txt");
//创建影片列表
var movieList = new List();
for (var i = 0; i < movies.length; ++i){
movieList.append(movies[i]);
}
//显示影碟清单
function displayList(list){
for (list.front(); list.hasNext(); list.next()){
if (list.getElement() instanceof Customer){
print(list.getElement().name + ","+ list.getElement().movie);
}
else{
print(list.getElement());
}
}
}
//创建影片列表
var customers = new List();
//创建客户对象
function Customer(name, movie){
this.name = name;
this.movie = movie;
}
//租赁函数,为客户列表添加客户对象,同时删除电影列表中对应的电影
function checkOut(name, movie, moiveList, customerList){
if (moiveList.contains(movie)){
var c = new Customer(name, movie);
customerList.append(c);
moiveList.remove(movie);
}
else{
print(movie + "is not available");
}
}
//归还函数
function checkin(name, movie, moiveList, customerList){
if (!moiveList.contains(movie)){
var c = new Customer(name, movie);
customerList.remove(c);
moiveList.append(movie);
}
else{
print(movie + "is already here");
}
}
// 租赁 测试
function companyWork(){
print("Available movies: \n");
displayList(movieList);
flag = 1;
while(flag){
putstr("Please choose rent or return or quit?");
var action = readline();
if (action == "rent"){
putstr("\nEnter your name:");
var name = readline();
putstr("What movie would you like?");
var movie = readline();
checkOut(name, movie, movieList, customers);
}
else if(action == "return"){
putstr("\nEnter your name:");
var name = readline();
putstr("What movie are you giving back?");
var movie = readline();
checkin(name, movie, movieList, customers);
}
else{
flag = 0;
}
//租赁信息
print("\nCustomer Rentals: \n");
displayList(customers);
//现有电影
print("\nMovies Now Available\n");
displayList(movieList);
}
}
//运行租赁程序
companyWork();