算法与数据结构学习
This commit is contained in:
51
Chapter2/my.mjs
Normal file
51
Chapter2/my.mjs
Normal file
@ -0,0 +1,51 @@
|
||||
// 对数组进行复制,非引用赋值
|
||||
function copy(arr) {
|
||||
const arr2 = [];
|
||||
const len = arr.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
arr2[i] = arr[i];
|
||||
}
|
||||
|
||||
return arr2;
|
||||
}
|
||||
|
||||
// 关于sort排序
|
||||
// 数字排序方式,从小到大升序排列
|
||||
// sort函数接受的排序方法,返回的值应当为负数、0、正数,排序也按照这个顺序排序
|
||||
function sortNumber(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
// 获取奇数
|
||||
function isOdd(num) {
|
||||
return num % 2 !== 0;
|
||||
}
|
||||
|
||||
// 获取偶数
|
||||
function isEven(num) {
|
||||
return num % 2 === 0;
|
||||
}
|
||||
|
||||
const nums = [];
|
||||
for (let i = 0; i < 20; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
|
||||
const oddList = nums.filter(isOdd);
|
||||
const evenList = nums.filter(isEven);
|
||||
|
||||
console.log(oddList);
|
||||
console.log(evenList);
|
||||
|
||||
// 创建二维数组(表格)
|
||||
Array.matrix = function(numrows, numcols, initval) {
|
||||
const arr = [];
|
||||
for (let i = 0; i < numrows; i++) {
|
||||
const columns = [];
|
||||
for (let j = 0; j < numcols; j++) {
|
||||
columns[j] = initval;
|
||||
}
|
||||
arr[i] = columns;
|
||||
}
|
||||
return arr;
|
||||
}
|
64
Chapter2/test.mjs
Normal file
64
Chapter2/test.mjs
Normal file
@ -0,0 +1,64 @@
|
||||
// 1
|
||||
// 创建一个记录学生成绩的对象,提供一个添加成绩的方法,以及一个显示学生平均成绩的方法。
|
||||
function createTranscript() {
|
||||
this.gradeList = [];
|
||||
this.add = (num) => {
|
||||
this.gradeList.push(num);
|
||||
};
|
||||
this.calcAvge = () => {
|
||||
const sum = this.gradeList.reduce((sum, grade) => {
|
||||
sum += grade;
|
||||
return sum;
|
||||
}, 0);
|
||||
return sum / this.gradeList.length;
|
||||
};
|
||||
}
|
||||
|
||||
const transcript1 = new createTranscript();
|
||||
transcript1.add(120);
|
||||
transcript1.add(80);
|
||||
console.log(transcript1.calcAvge());
|
||||
|
||||
// 2
|
||||
// 将一组单词存储在一个数组中,并按正序和倒序分别显示这些单词
|
||||
const list = ["fan", "sheng", "fa", "lan", "xiu", "ping"];
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
console.log(list[i]);
|
||||
}
|
||||
console.log("--------------------------")
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
console.log(list[i]);
|
||||
}
|
||||
|
||||
// 3
|
||||
function weekTemps() {
|
||||
this.dataStore = [];
|
||||
for (let i = 0; i < 12; i++) {
|
||||
this.dataStore.push([])
|
||||
}
|
||||
this.add = add;
|
||||
this.average = average;
|
||||
}
|
||||
|
||||
function add(month, temp) {
|
||||
this.dataStore[month].push(temp);
|
||||
}
|
||||
|
||||
function average() {
|
||||
var total = 0;
|
||||
for (var i = 0; i < this.dataStore.length; ++i) {
|
||||
total += this.dataStore[i];
|
||||
}
|
||||
return total / this.dataStore.length;
|
||||
}
|
||||
|
||||
var thisWeek = new weekTemps();
|
||||
thisWeek.add(52);
|
||||
thisWeek.add(55);
|
||||
thisWeek.add(61);
|
||||
thisWeek.add(65);
|
||||
thisWeek.add(55);
|
||||
thisWeek.add(50);
|
||||
thisWeek.add(52);
|
||||
thisWeek.add(49);
|
||||
print(thisWeek.average()); // displays 54.875
|
143
Chapter3/list.mjs
Normal file
143
Chapter3/list.mjs
Normal file
@ -0,0 +1,143 @@
|
||||
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) {
|
||||
// 元素追加后,listSize 自增
|
||||
this.dataStore[this.listSize++] = element;
|
||||
}
|
||||
|
||||
function find(element) {
|
||||
for (let i = 0; i < this.dataStore.length; i++) {
|
||||
if (this.dataStore[i] === element) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function remove(element) {
|
||||
const 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) {
|
||||
const insertPos = this.find(after);
|
||||
if (insertPos > -1) {
|
||||
this.dataStore.splice(after, 0, element);
|
||||
++this.listSize;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function clear() {
|
||||
delete this.dataStore;
|
||||
this.dataStore.length = 0;
|
||||
this.listSize = this.pos = 0;
|
||||
}
|
||||
|
||||
function contains(element) {
|
||||
for (let 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;
|
||||
}
|
||||
|
||||
const names = new List();
|
||||
names.append('Clayton');
|
||||
names.append('Raymond');
|
||||
names.append('Cynthia');
|
||||
names.append('Jennifer');
|
||||
names.append('Bryan');
|
||||
names.append('Danny');
|
||||
names.front();
|
||||
// console.log(names.getElement());
|
||||
names.next();
|
||||
// console.log(names.getElement());
|
||||
names.next();
|
||||
names.next();
|
||||
names.prev();
|
||||
// console.log(names.getElement());
|
||||
|
||||
// 从前往后遍历
|
||||
for (names.front(); names.hasNext(); names.next()) {
|
||||
console.log(names.getElement());
|
||||
}
|
||||
console.log('------------------------------------')
|
||||
// 从后往前遍历
|
||||
for (names.end(); names.hasPrev(); names.prev()) {
|
||||
console.log(names.getElement());
|
||||
}
|
192
Chapter3/movies.mjs
Normal file
192
Chapter3/movies.mjs
Normal file
@ -0,0 +1,192 @@
|
||||
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.appendWhenBig = appendWhenBig;
|
||||
this.appendWhenSmall = appendWhenSmall;
|
||||
}
|
||||
|
||||
function append(element) {
|
||||
// 元素追加后,listSize 自增
|
||||
this.dataStore[this.listSize++] = element;
|
||||
}
|
||||
|
||||
function find(element) {
|
||||
for (let i = 0; i < this.dataStore.length; i++) {
|
||||
if (this.dataStore[i] === element) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function remove(element) {
|
||||
const 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) {
|
||||
const insertPos = this.find(after);
|
||||
if (insertPos > -1) {
|
||||
this.dataStore.splice(after, 0, element);
|
||||
++this.listSize;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function clear() {
|
||||
delete this.dataStore;
|
||||
this.dataStore.length = 0;
|
||||
this.listSize = this.pos = 0;
|
||||
}
|
||||
|
||||
function contains(element) {
|
||||
for (let 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;
|
||||
}
|
||||
|
||||
function appendWhenBig(element) {
|
||||
let flag = true;
|
||||
for (this.front(); this.hasNext(); this.next()) {
|
||||
if (this.getElement() > element) {
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
this.append(element);
|
||||
}
|
||||
}
|
||||
|
||||
function appendWhenSmall(element) {
|
||||
let flag = true;
|
||||
for (this.front(); this.hasNext(); this.next()) {
|
||||
if (this.getElement() < element) {
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (flag) {
|
||||
this.append(element);
|
||||
}
|
||||
}
|
||||
|
||||
function displayList(list) {
|
||||
for (list.front(); list.currPos() < list.length(); list.next()) {
|
||||
console.log(list.getElement());
|
||||
}
|
||||
}
|
||||
|
||||
function Customer(name, movie) {
|
||||
this.name = name;
|
||||
this.movie = movie;
|
||||
}
|
||||
|
||||
function checkout(name, movie, movieList, customerList) {
|
||||
if (movieList.contains(movie)) {
|
||||
const c = new Customer(name, movie);
|
||||
customerList.append(c);
|
||||
movieList.remove(movie);
|
||||
} else {
|
||||
console.log(movie + ' is not available.');
|
||||
}
|
||||
}
|
||||
|
||||
const movies = ['大室家 亲爱的姐妹们', '紫罗兰永恒花园', '牵牛花与加濑同学。', '利兹与青鸟', '摇曳百合 夏日时光!'];
|
||||
const movieList = new List();
|
||||
const customers = new List();
|
||||
for (let i = 0; i < movies.length; i++) {
|
||||
movieList.append(movies[i]);
|
||||
}
|
||||
console.log('Available moviews: \n');
|
||||
displayList(movieList);
|
||||
|
||||
const readline = require('readline');
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
rl.question('Enter your name: ', (name) => {
|
||||
rl.question('What movie would you like?', (movie) => {
|
||||
checkout(name, movie, movieList, customers);
|
||||
console.log('Customer Rentals: ');
|
||||
displayList(customers);
|
||||
console.log('Movies Now Available');
|
||||
displayList(movieList);
|
||||
rl.close();
|
||||
})
|
||||
})
|
29
Chapter4/stack.mjs
Normal file
29
Chapter4/stack.mjs
Normal file
@ -0,0 +1,29 @@
|
||||
function push(element) {
|
||||
this.dataSource[this.top++] = element;
|
||||
}
|
||||
|
||||
function pop() {
|
||||
return this.dataSource[--this.top];
|
||||
}
|
||||
|
||||
function peek() {
|
||||
return this.dataSource[this.top - 1];
|
||||
}
|
||||
|
||||
function length() {
|
||||
return this.top;
|
||||
}
|
||||
|
||||
function clear() {
|
||||
this.top = 0;
|
||||
}
|
||||
|
||||
export default function Stack() {6
|
||||
this.dataSource = [];
|
||||
this.top = 0;
|
||||
this.push = push;
|
||||
this.pop = pop;
|
||||
this.peek = peek;
|
||||
this.length = length;
|
||||
this.clear = clear;
|
||||
}
|
20
Chapter4/test1.mjs
Normal file
20
Chapter4/test1.mjs
Normal file
@ -0,0 +1,20 @@
|
||||
import Stack from "./stack.mjs";
|
||||
// 找出缺失的括号
|
||||
function findMissingParentheses(expression) {
|
||||
const stack = new Stack();
|
||||
const expressionList = expression.split('');
|
||||
for (let i = 0; i < expressionList.length; i++) {
|
||||
const char = expressionList[i];
|
||||
if (char === '(') {
|
||||
stack.push(i);
|
||||
}
|
||||
if (char === ')') {
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
if (stack.length() > 0) {
|
||||
console.log('missing parent index:', stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
findMissingParentheses('2.3 + 23 / 12 + (3.14159 x 0.24')
|
111
Chapter4/test2.mjs
Normal file
111
Chapter4/test2.mjs
Normal file
@ -0,0 +1,111 @@
|
||||
import Stack from "./stack.mjs";
|
||||
|
||||
function infixToPostfix(infixExpression) {
|
||||
const stack1 = new Stack();
|
||||
const stack2 = new Stack();
|
||||
|
||||
function warOperator(operator1, operator2) {
|
||||
if (operator2 === undefined || operator2 === "(") {
|
||||
stack1.push(operator1);
|
||||
} else {
|
||||
if (["x", "/"].includes(operator1) && ["+", "-"].includes(operator2)) {
|
||||
stack1.push(operator1);
|
||||
} else {
|
||||
const top = stack1.pop();
|
||||
stack2.push(top);
|
||||
const nextOperator = stack1.peek();
|
||||
warOperator(operator1, nextOperator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const dealInfixExpression = infixExpression.replaceAll(" ", "");
|
||||
|
||||
let numStr = "";
|
||||
for (let i = 0; i < dealInfixExpression.length; i++) {
|
||||
const char = dealInfixExpression[i];
|
||||
if (["+", "-", "x", "/"].includes(char)) {
|
||||
if (numStr.length > 0) {
|
||||
stack2.push(numStr);
|
||||
numStr = "";
|
||||
}
|
||||
const topOperator = stack1.peek();
|
||||
warOperator(char, topOperator);
|
||||
} else if (["(", ")"].includes(char)) {
|
||||
if (numStr.length > 0) {
|
||||
stack2.push(numStr);
|
||||
numStr = "";
|
||||
}
|
||||
if (char === "(") {
|
||||
stack1.push(char);
|
||||
} else {
|
||||
while (stack1.length()) {
|
||||
let operator = stack1.pop();
|
||||
if (operator === "(") {
|
||||
break;
|
||||
}
|
||||
stack2.push(operator);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
numStr += char;
|
||||
if (i === dealInfixExpression.length - 1) {
|
||||
stack2.push(numStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (stack1.length()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
|
||||
const postfixExpression = [];
|
||||
while (stack2.length()) {
|
||||
postfixExpression.push(stack2.pop());
|
||||
}
|
||||
postfixExpression.reverse();
|
||||
return postfixExpression;
|
||||
}
|
||||
|
||||
function calc(num1, num2, operator) {
|
||||
const n1 = Number(num1);
|
||||
const n2 = Number(num2);
|
||||
let result;
|
||||
switch (operator) {
|
||||
case "+":
|
||||
result = n1 + n2;
|
||||
break;
|
||||
case "-":
|
||||
result = n1 - n2;
|
||||
break;
|
||||
case "x":
|
||||
result = n1 * n2;
|
||||
break;
|
||||
case "/":
|
||||
result = n1 / n2;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function calculate(postfixExpression) {
|
||||
const stack = new Stack();
|
||||
for (let i = 0; i < postfixExpression.length; i++) {
|
||||
const element = postfixExpression[i];
|
||||
if (["+", "-", "x", "/"].includes(element)) {
|
||||
const num2 = stack.pop();
|
||||
const num1 = stack.pop();
|
||||
stack.push(calc(num1, num2, element));
|
||||
} else {
|
||||
stack.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
return stack.pop();
|
||||
}
|
||||
|
||||
const expression = "12+((2.1+3) x45.2)-5.22";
|
||||
const postfixExpression = infixToPostfix(expression);
|
||||
console.log(postfixExpression);
|
||||
const result = calculate(postfixExpression);
|
||||
console.log(result);
|
Reference in New Issue
Block a user