1. 实现 advance(n) 方法,使当前节点向前移动 n 个节点。
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this._current = this.head;
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
this.advance = advance;
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
if (currNode == null) {
return null;
}
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
if (current) {
newNode.next = current.next;
current.next = newNode;
}
}
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode.next == null)) {
prevNode.next = prevNode.next.next;
}
}
function advance(n) {
if (n < 0) {
console.error("Cannot advance with negative number.");
return;
}
var count = 0;
while (count < n && this._current.next !== null) {
this._current = this._current.next;
count++;
}
}
2. 实现 back(n) 方法,使当前节点向后移动 n 个节点。
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this._current = this.head;
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
this.back = back;
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
if (currNode == null) {
return null;
}
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
if (current) {
newNode.next = current.next;
current.next = newNode;
}
}
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode.next == null)) {
prevNode.next = prevNode.next.next;
}
}
function back(n) {
if (n < 0) {
console.error("Cannot back with negative number.");
return;
}
var count = 0;
while (count < n && this._current !== this.head) {
this._current = this.findPrevious(this._current.element);
count++;
}
}
3. 实现 show() 方法,只显示当前节点上的数据。
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this._current = this.head;
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
this.show = show;
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
if (currNode == null) {
return null;
}
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
if (current) {
newNode.next = current.next;
current.next = newNode;
}
}
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function findPrevious(item) {
var currNode = this.head;
while (!(currNode.next == null) && (currNode.next.element != item)) {
currNode = currNode.next;
}
return currNode;
}
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode.next == null)) {
prevNode.next = prevNode.next.next;
}
}
function show() {
console.log(this._current.element);
}
4. 使用单向链表写一段程序,记录用户输入的一组测验成绩。
function recordScores() {
var list = new LList();
console.log("请输入一组测验成绩,每输入一个成绩按回车键确认,输入 'end' 结束输入:");
function readInput() {
var input = prompt("请输入成绩:");
if (input === 'end') {
console.log("所有成绩记录完成。");
console.log("成绩列表:");
list.display();
return;
}
var score = parseInt(input);
if (isNaN(score)) {
console.error("无效的输入,请输入一个有效的整数。");
readInput();
} else {
list.insert(score, list._current.element);
list.advance(1);
readInput();
}
}
readInput();
}
recordScores();
5. 使用双向链表重写例 6-4 的程序。
function Node(element) {
this.element = element;
this.next = null;
this.previous = null;
}
function LList() {
this.head = new Node("head");
this.find = find;
this.insert = insert;
this.remove = remove;
this.display = display;
this.findLast = findLast;
this.dispReverse = dispReverse;
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
if (currNode == null) {
return null;
}
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
if (current) {
newNode.next = current.next;
newNode.previous = current;
if (current.next !== null) {
current.next.previous = newNode;
}
current.next = newNode;
}
}
function display() {
var currNode = this.head;
while (!(currNode.next == null)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function remove(item) {
var currNode = this.find(item);
if ((!currNode) && !(currNode.next == null)) {
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.previous = null;
currNode.next = null;
}
}
function findLast() {
var currNode = this.head;
while (!(currNode.next == null)) {
currNode = currNode.next;
}
return currNode;
}
function dispReverse() {
var currNode = this.head;
currNode = this.findLast();
while (!(currNode.previous == null)) {
console.log(currNode.element);
currNode = currNode.previous;
}
}
var cities = new LList();
cities.insert("conway", "head");
cities.insert("Russellville","conway");
cities.insert("carlisle","Russellville");
cities.insert("Alma","carlisle");
cities.display();
cities.remove("carlisle");
cities.display();
cities.dispReverse();
6. 传说在公元 1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的 40 个同胞 被罗马士兵包围。犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案。他 们围成一个圈,从一个人开始,数到第三个人时将第三个人杀死,然后再数,直到杀光 所有人。约瑟夫和另外一个人决定不参加这个疯狂的游戏,他们快速地计算出了两个位 置,站在那里得以幸存。写一段程序将 n 个人围成一圈,并且第 m 个人会被杀掉,计算 一圈人中哪两个人最后会存活。使用循环链表解决该问题。
function Node(element) {
this.element = element;
this.next = null;
}
function LList() {
this.head = new Node("head");
this.head.next = this.head;
this.find = find;
this.insert = insert;
this.findPrevious = findPrevious;
this.remove = remove;
this.display = display;
this.initList = initList;
}
function find(item) {
var currNode = this.head;
while (currNode.element != item) {
currNode = currNode.next;
if (currNode == this.head) {
return null;
}
}
return currNode;
}
function insert(newElement, item) {
var newNode = new Node(newElement);
var current = this.find(item);
newNode.next = current.next;
current.next = newNode;
}
function display() {
var currNode = this.head;
while (!(currNode.next == this.head)) {
console.log(currNode.next.element);
currNode = currNode.next;
}
}
function findPrevious(item) {
var currNode = this.head;
while (currNode.next.element != item) {
currNode = currNode.next;
if (currNode == this.head) {
return null;
}
}
return currNode;
}
function remove(item) {
var prevNode = this.findPrevious(item);
if (!(prevNode == null)) {
prevNode.next = prevNode.next.next;
}
}
function initList(n) {
for (let i = n; i > 0; i--) {
this.insert(i, "head");
}
}
function josephusProblem(n, m) {
var list = new LList();
list.initList(n);
var currNode = list.head.next;
var count = n;
while (count > 2) {
for (var i = 1; i < m; i++) {
currNode = currNode.next;
}
var nextNode = currNode.next;
list.remove(currNode.element);
count--;
console.log("还剩:" + count + "人");
currNode = nextNode;
}
console.log("存活的2人最开始应站立的位置为:" + list.head.next.element + "," + list.head.next.next.element);
}
const n = 41;
const m = 3;
console.log(josephusProblem(n, m));