//双向链表
var BLinkList = function () {
this.head = new BNode('head');
this.find = function (item) {
var currNode = this.head;
while(currNode.element != item){
currNode = currNode.next;
}
return currNode;
}
this.insert = function (newElement,item) {
var found = this.find(item);
var newNode = new BNode(newElement);
newNode.next = found.next;
newNode.previous = found;
found.next = newNode;
}
this.remove = function (item) {
var currNode = this.find(item);
if(!(currNode.next == null)){
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
this.findLast = function () {
var currNode = this.head;
while(!(currNode.next == null)){
currNode = currNode.next;
}
return currNode;
}
this.disReverse = function () {
var currNode = this.findLast();
while(!(currNode.previous == null)){
console.log(currNode.element);
currNode = currNode.previous;
}
}
this.display = function () {
var currNode = this.head;
while(currNode != null){
console.log(currNode.element);
currNode = currNode.next;
}
};
}
var BBLinkList = function () { this.currNode = this.head; }; BBLinkList.prototype = new BLinkList();
1.实现advance(n)方法,使当前节点向前移动n个节点。
//向前移动n个节点
BBLinkList.prototype.advance = function (n) {
while ((n>0) && !(this.currNode==null)){
this.currNode = this.currNode.next;
n--
}
};
2.实现back(n)方法,使当前节点向后移动n个节点。
BBLinkList.prototype.back = function (n) {
while ((n>0) && !(this.currNode==null)){
this.currNode = this.currNode.previous;
n--
}
}
3.实现show()方法,只显示当前节点上的数据
BBLinkList.prototype.show = function () { return this.currNode?this.currNode.element:'溢出'; }
//测试程序
var bblist = new BBLinkList();
bblist.insert('A','head');
bblist.insert('B','A');
bblist.display();
console.log('向前移动2个节点');
bblist.advance(2);
console.log(bblist.show());
console.log('向后移动1个节点');
bblist.back(1);
console.log(bblist.show());
4.使用单向列表写一段程序,记录用户输入的一组测验成绩。
//单向列表
var LinkedList = function () {
this.head = new Node('head');
this.find = function (item) {
var currNode = this.head;
while (currNode.element != item){
currNode = currNode.next;
}
return currNode;
}
this.insert = function (newElement,item) {
var newNode = new Node(newElement);
var foundAt = this.find(item);
newNode.next = foundAt.next;
foundAt.next = newNode;
}
this.remove = function (item) {
var foundPreviousAt = this.findPrevious(item);
if(foundPreviousAt.next != null){
foundPreviousAt.next = foundPreviousAt.next.next;
}
}
this.findPrevious = function (item) {
var currNode = this.head;
var previousNode = null;
while (currNode.element != item){
previousNode = currNode;
currNode = currNode.next;
}
return previousNode;
}
this.findLast = function () {
var currNode = this.head;
while(!(currNode.next == null)){
currNode = currNode.next;
}
return currNode;
}
this.display = function () {
var currNode = this.head;
while(currNode != null){
if(currNode.element != 'head'){
console.log(currNode.element);
}
currNode = currNode.next;
}
}
};
//学生类
var Student = function (name,score) {
this.name = name;
this.score = score;
};
//成绩类
var Grade = function () {};
Grade.prototype = new LinkedList();
//输入成绩
Grade.prototype.input = function (student) {
var lastNode = this.findLast();
this.insert(student,lastNode.element);
}
var grade = new Grade();
grade.input(new Student('a',30));
grade.input(new Student('b',40));
grade.display();
5.使用双向链表重写例6-4程序
//双向链表
var BLinkList = function () {
this.head = new BNode('head');
this.find = function (item) {
var currNode = this.head;
while(currNode.element != item){
currNode = currNode.next;
}
return currNode;
}
this.insert = function (newElement,item) {
var found = this.find(item);
var newNode = new BNode(newElement);
newNode.next = found.next;
newNode.previous = found;
found.next = newNode;
}
this.remove = function (item) {
var currNode = this.find(item);
if(!(currNode.next == null)){
currNode.previous.next = currNode.next;
currNode.next.previous = currNode.previous;
currNode.next = null;
currNode.previous = null;
}
}
this.findLast = function () {
var currNode = this.head;
while(!(currNode.next == null)){
currNode = currNode.next;
}
return currNode;
}
this.disReverse = function () {
var currNode = this.findLast();
while(!(currNode.previous == null)){
console.log(currNode.element);
currNode = currNode.previous;
}
}
this.display = function () {
var currNode = this.head;
while(currNode != null){
console.log(currNode.element);
currNode = currNode.next;
}
};
}
//双向列表
var blist = new BLinkList();
//插入A
blist.insert('A','head');
//插入B
blist.insert('B','A');
//删除A
blist.remove('A');
//只剩下B被显示
blist.disReverse();
6.传说在公元1世纪的犹太战争中,犹太历史学家费拉奥*约瑟夫和他的40个同胞 被罗马士兵保卫。犹太士兵决定宁可自杀也不做俘虏,于是商量除了一个自杀方案。 他们围城一个圈,从一个人开始,数到第三个人时将第三个人杀死,然后再数,直到杀光所有人。 约瑟夫和另外一个人决定不参加这个疯狂的游戏,他们快速地计算出了两个位置,站在那里得意幸存。 写一段程序将n个人围城一圈,并且第m个人会被杀掉,计算一圈人中哪两个人最后会存货。使用循环链表 解决该问题。
//循环链表
var CycleChain = function () {
this.head = new Node('head');
this.head.next = this.head;
this.find = function (item) {
var currNode = this.head;
while(!(currNode.element == item)){
currNode = currNode.next;
}
return currNode;
};
this.insert = function (newElement,item) {
var found = this.find(item);
var newNode = new Node(newElement);
newNode.next = found.next;
found.next = newNode;
var lastNode = this.findLast();
lastNode.next = this.head;
};
this.display = function () {
var currNode = this.head;
while (!(currNode == null) && !(currNode.next.element == 'head')){
console.log(currNode.next.element);
currNode = currNode.next;
}
};
this.findPrevious = function (item) {
var currNode = this.head;
var previous = null;
while(!(currNode.element == item)){
previous = currNode;
currNode = currNode.next;
}
return previous;
};
this.remove= function (item) {
var foundPrevious = this.findPrevious(item);
if(!(foundPrevious == null)){
foundPrevious.next = foundPrevious.next.next;
}
};
this.findLast = function () {
var currNode = this.head;
while(!(currNode.next == null) && !(currNode.next.element == 'head')){
currNode = currNode.next;
}
return currNode;
};
};
//杀人游戏类
var Game = function () {};
//继承循环链表
Game.prototype = new CycleChain();
Game.prototype.currNode = Game.prototype.head;
//向前移动n个节点
Game.prototype.advance = function (n) {
while (n>0){
this.currNode = this.currNode.next;
if(this.currNode.element == 'head'){
this.currNode = this.currNode.next;
}
n--;
}
};
//计算幸存者
Game.prototype.survive = function (n,m) {
for(var i = 0; i < (n-2); i++){
this.kill(m);
this.display();
}
}
//杀掉第m个人
Game.prototype.kill = function (m) {
this.advance(m);
this.remove(this.currNode.element);
}
Game.prototype.display = function () {
var currNode = this.head;
var arr = [];
while (!(currNode == null) && !(currNode.next.element == 'head')){
arr.push(currNode.next.element);
currNode = currNode.next;
}
console.log(arr);
}
//A B C D E F
var game = new Game();
game.insert('1','head');
game.insert('2','1');
game.insert('3','2');
game.insert('4','3');
game.insert('5','4');
game.insert('6','5');
game.insert('7','6');
game.insert('8','7');
game.insert('9','8');
game.insert('10','9');
game.display();
game.survive(10,3);