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

1. 修改 Queue 类,形成一个 Deque 类。这是一个和队列类似的数据结构,允许从队列两端 添加和删除元素,因此也叫双向队列。写一段测试程序测试该类。

function Deque() { 
	this.dataStore = []; 
	this.enqueue = enqueue; 
	this.addFront = addFront;
	this.dequeue = dequeue; 
	this.removeEnd = removeEnd;
	this.front = front; 
	this.back = back; 
	this.toString = toString; 
	this.empty = empty; 
} 
 
function enqueue(element) { 
	this.dataStore.push(element); 
} 
 
function dequeue() { 
	return this.dataStore.shift(); 
} 
 
function front() { 
	return this.dataStore[0]; 
} 
 
function back() { 
	return this.dataStore[this.dataStore.length-1]; 
} 
 
function toString() { 
	var retStr = "";
	for (var i = 0; i < this.dataStore.length; ++i) { 
		retStr += this.dataStore[i] + "\n"; 
	} 
	return retStr; 
} 
 
function empty() { 
	if (this.dataStore.length == 0) { 
		return true; 
	} else { 
		return false; 
	} 
} 

function addFront(element) {
	this.dataStore.unshift(element);
}

function removeEnd() {
	return this.dataStore.pop();
}

var d = new Deque();
d.enqueue(1);
d.enqueue(2);
d.enqueue(3);
d.addFront(0);
d.addFront(-1);
console.log("Deque after adding elements:");
console.log(d.toString());
console.log("Removed element from front: " + d.dequeue());
console.log("Removed element from front: " + d.dequeue());
console.log("Removed element from end: " + d.removeEnd());
console.log("Deque after removing elements:");
console.log(d.toString());
console.log("Is deque empty? " + d.empty());

2. 使用前面完成的 Deque 类来判断一个给定单词是否为回文。

function isPalindrome(word) {
    var d = new Deque();
    
    for (var i = 0; i < word.length; i++) {
        d.enqueue(word[i]);
    }
    
    while (!d.empty()) {
        if (d.front() !== d.back()) {
            return false;
        }
        
        d.dequeue();
        d.removeEnd();
    }
    
    return true;
}
var words = ["racecar", "hello", "madam", "noon", "world"];
words.forEach(function(word) {
	if (isPalindrome(word)) {
		console.log(word + " is a palindrome\n");
	}
});

3. 修改例 5-5 中的优先队列,使得优先级高的元素优先码也大。写一段程序测试你的改动。

function Queue() { 
	this.dataStore = []; 
	this.enqueue = enqueue; 
	this.dequeue = dequeue; 
	this.front = front; 
	this.back = back; 
	this.toString = toString; 
	this.empty = empty; 
} 
 
function enqueue(element) { 
	this.dataStore.push(element); 
} 
 
// function dequeue() { 
	// return this.dataStore.shift();
// }
function dequeue() { 
	var priority = 0;
	for (var i = 1; i < this.dataStore.length; i++) {
		if (this.dataStore[i].code > this.dataStore[priority].code) {
			priority = i;
		}
	}
	return this.dataStore.splice(priority, 1);
} 
 
function front() { 
	return this.dataStore[0]; 
} 
 
function back() { 
	return this.dataStore[this.dataStore.length-1]; 
} 
 
// function toString() { 
	// var retStr = "";
	// for (var i = 0; i < this.dataStore.length; ++i) { 
		// retStr += this.dataStore[i] + "\n"; 
	// } 
	// return retStr; 
// } 
function toString() { 
	var retStr = "";
	for (var i = 0; i < this.dataStore.length; ++i) { 
		retStr += this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n"; 
	} 
	return retStr; 
}
 
function empty() { 
	if (this.dataStore.length == 0) { 
		return true; 
	} else { 
		return false; 
	} 
} 




function Patient(name, code) { 
 this.name = name; 
 this.code = code; 
}


var ed = new Queue(); 
ed.enqueue(new Patient("Smith",5)); 
ed.enqueue(new Patient("Jones", 4));
ed.enqueue(new Patient("Fehrenbach", 6)); 
ed.enqueue(new Patient("Brown", 1)); 
ed.enqueue(new Patient("Ingram", 1)); 
console.log(ed.toString()); 
var seen = ed.dequeue(); 
console.log("Patient being treated: " + seen[0].name); 
console.log("Patients waiting to be seen: ") 
console.log(ed.toString()); 
var seen = ed.dequeue(); 
console.log("Patient being treated: " + seen[0].name); 
console.log("Patients waiting to be seen: ") 
console.log(ed.toString()); 
var seen = ed.dequeue(); 
console.log("Patient being treated: " + seen[0].name); 
console.log("Patients waiting to be seen: ") 
console.log(ed.toString());

4. 修改例 5-5 中的候诊室程序,使得候诊室内的活动可以被控制。写一个类似菜单系统, 让用户可以进行如下选择: a. 患者进入候诊室; b. 患者就诊; c. 显示等待就诊患者名单。

function Queue() { 
	this.dataStore = []; 
	this.enqueue = enqueue; 
	this.dequeue = dequeue; 
	this.front = front; 
	this.back = back; 
	this.toString = toString; 
	this.empty = empty; 
} 
 
function enqueue(element) { 
	this.dataStore.push(element); 
} 
 
// function dequeue() { 
	// return this.dataStore.shift();
// }
function dequeue() { 
	var priority = 0;
	for (var i = 1; i < this.dataStore.length; i++) {
		if (this.dataStore[i].code < this.dataStore[priority].code) {
			priority = i;
		}
	}
	return this.dataStore.splice(priority, 1);
} 
 
function front() { 
	return this.dataStore[0]; 
} 
 
function back() { 
	return this.dataStore[this.dataStore.length-1]; 
} 
 
// function toString() { 
	// var retStr = "";
	// for (var i = 0; i < this.dataStore.length; ++i) { 
		// retStr += this.dataStore[i] + "\n"; 
	// } 
	// return retStr; 
// } 
function toString() { 
	var retStr = "";
	for (var i = 0; i < this.dataStore.length; ++i) { 
		retStr += this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n"; 
	} 
	return retStr; 
}
 
function empty() { 
	if (this.dataStore.length == 0) { 
		return true; 
	} else { 
		return false; 
	} 
} 




function Patient(name, code) { 
 this.name = name; 
 this.code = code; 
}


function main() {
    var ed = new Queue();

    while (true) {
        console.log("\n请选择操作:");
        console.log("a. 患者进入候诊室");
        console.log("b. 患者就诊");
        console.log("c. 显示等待就诊患者名单");
        console.log("d. 退出");

        var choice = prompt("请输入选项:").toLowerCase();

        switch (choice) {
            case 'a':
                var name = prompt("请输入患者姓名:");
                var code = parseInt(prompt("请输入患者优先级代码:"));
                ed.enqueue(new Patient(name, code));
                console.log(name + "已进入候诊室");
                break;

            case 'b':
                if (!ed.empty()) {
                    var seen = ed.dequeue();
                    console.log("正在治疗的患者:" + seen[0].name);
                } else {
                    console.log("候诊室为空,无法就诊!");
                }
                break;

            case 'c':
                if (!ed.empty()) {
                    console.log("等待就诊的患者列表:\n" + ed.toString());
                } else {
                    console.log("候诊室为空!");
                }
                break;

            case 'd':
                console.log("退出程序。");
                return;

            default:
                console.log("无效的选择,请重新输入!");
                break;
        }
    }
}

main();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值