03
2018
09

队列

队列是一种先进先出的数据结构。


var q = new Array(10);
q.head = 0;
q.tail = 0;
var n = 0;
while(!queFull(q)){
	n++;
	enQueue(q, n);
	console.log("入队:",n);
}
console.log(q);
while(!queEmpty(q)){
	console.log("出队:",deQueue(q));
}
console.log(q);

function enQueue(q, x){
	if(queFull(q)){
		throw(new Error("que is full"));
	}
	q[q.tail] = x;
	if(q.tail == q.length - 1){
		q.tail = 0;
	} else {
		q.tail++;
	}
}

function deQueue(q){
	if(queEmpty(q)){
		throw(new Error("que is empty"));
	}
	var x = q[q.head];
	if(q.head == q.length - 1){
		q.head = 0;
	}
	else {
		q.head = q.head + 1;
	}
	return x;
}

function queFull(q){
	if(q.head == (q.tail + 1) % q.length){
		return true;
	}
	return false;
}

function queEmpty(q){
	if(q.head == q.tail){
		return true;
	}
	return false;
}

https://github.com/hanyeah/lianxi/tree/master/算法导论/10

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。