js:數組實現隊列和棧、棧實現隊列、隊列實現棧

目錄

 

一、利用數組結構實現大小固定的隊列和棧

二、僅用隊列結構實現棧結構

三、僅用棧結構實現隊列結構

四、總結


一、利用數組結構實現大小固定的隊列和棧

1.數組結構 實現 隊列

  ①隊列的特點:先進先出,後進後出。

  ② 注意:需要定義 start、end變量,以標記 出隊和入隊位置;還要定義一個size變量,約束出入隊的行爲。

<script type="text/javascript">
	// arrayToQueue
	var start = 0,end = 0,size = 0;
	function peek(arr){  // 返回隊頭元素
		if (size == 0) {
			console.log("This Queue is empty!");
			return;
		}
		console.log("隊頭元素:"+ arr[start]);
	}
	function push(arr,n){  // 入隊操作
		if (size == arr.length-1 ) {
			console.log("This Queue is full!")
			return;
		}
		arr[end] = n;
		console.log(arr[end]+"入隊列成功!")
		size++;
		end = end == arr.length-1 ? 0 : end+1;
		
	}
	function pop(arr){  // 出隊操作
		if (size == 0) {
			console.log("This Queue is empty!")
			return;
		}
		console.log(arr[start]+"出隊列成功!")
		size--;
		start = start == arr.length-1 ? 0 : start+1;
	}
	var arr = [];
	arr.length = 3;  // 設置隊列長只有3
	push(arr,18)  // 18入隊列成功!
	push(arr,3)  // 3入隊列成功!
	pop(arr)  // 18出隊列成功!
	pop(arr)  // 3出隊列成功!
	pop(arr)  // This Queue is null!
	peek(arr)  // This Queue is null!
	push(arr,5)  // 5入隊列成功!
	peek(arr)  // 棧頂元素:5
</script>

2.數組結構 實現 棧

  ① 棧的特點:先進後出,後進先出

  ② 需要一個 size 變量來記錄位置

<script type="text/javascript">
	// arrayToStack
	var size =0;
	function peek(arr){  // 返回棧頂元素
		if (size == 0) {
			console.log(" This Stack is empty!")
			return;
		}
		console.log(" 棧頂元素: "+arr[size-1]);
	}
	function push(arr,n){  // 入棧操作
		if (size == arr.length) {
			console.log(" This Stack is full!")
			return;
		}
		arr[size++] = n;
		console.log( n + " 成功入棧!");
	}
	function pop(arr){  // 出棧操作
		if (size == 0) {
			console.log(" This Stack is empty!")
			return;
		}
		console.log( arr[--size] + " 成功出棧!");
	}
	var arr = [];
	arr.length = 3;
	push(arr,3);  // 3 成功入棧!
	push(arr,5);  // 5 成功入棧!
	peek(arr);  //  棧頂元素: 5
	pop(arr);  // 5 成功出棧!
	peek(arr);  // 棧頂元素: 3
</script>

 

二、僅用隊列結構實現棧結構

1.思路:

  ① 先準備兩個隊列 queue、help;

  ② queue 用來實現出入棧操作;

  ③ 出棧時: queue先把前面的值push進help裏,然後彈出棧頂的數;

  ④ 入棧:queue的隊尾push一個數;

  ⑤ 出棧後,交換queue和help兩個隊列。

<script type="text/javascript">
	// queueToStack
	var queue = [],help = [],length = 3;  // 先默認長度爲3
	function peek(){  // 返回棧頂元素
		if (queue.length == 0) {
			console.log("This Stack is empty!");
			return;
		}
		console.log("棧頂元素:" + queue[queue.length-1]);
	}
	function pushStack(n){  // 入棧操作
		if (queue.length == length) {
			console.log("This Stack is full!");
			return;
		}
		queue.push(n);
		console.log( n + " 成功入棧!");
	}
	function popStack(){  // 出棧操作
		if (queue.length == 0) {
			console.log("This Stack is empty!");
			return;
		}
		help = queue.splice(0,queue.length-1);
		console.log( queue.pop() + " 成功出棧!");
		swap();
	}
	function swap(){
		var temp;
		temp = queue;
		queue = help;
		help = temp; 
	}
	pushStack(5);  // 5 成功入棧!
	pushStack(3);  // 3 成功入棧!
	pushStack(1);  // 1 成功入棧!
	peek();  // 棧頂元素:1
	popStack();  // 1 成功出棧!
	peek();  // 棧頂元素:3
	pushStack(8);  // 8 成功入棧!
	peek();  // 棧頂元素:8
</script>

三、僅用棧結構實現隊列結構

1.思路:

  ① 先準備兩個棧 stackPush、stackPop;

  ② 入隊時:只在 stackPush 進行 push 操作;

  ③ 出隊時: 考慮3種情況:

     - stackPush、stackPop 都爲空,則無法出隊;

     - stackPush不爲空,stackPop爲空:將stackPush除了第一個數去,都移到stackPop裏,出隊數爲第一個數;

     - stackPush、stackPop 都不爲空:出隊數爲stackPop的最後一個數;

<script type="text/javascript">
	// stackToQueue
	var stackPush = [],stackPop = [],length = 3;
	function peek(){  // 返回隊頭元素
		if (stackPush.length == 0 && stackPop.length == 0) {
			console.log("This Queue is empty!");
			return;
		}
		if(stackPop.length != 0){
			console.log("隊頭元素:" + stackPop[stackPop.length-1]);
			return;
		}
		console.log("隊頭元素:" + stackPush[0]);
	}
	function pushQueue(n){  // 入隊操作
		if (stackPush.length == length) {
			console.log("This Queue is full!");
			return;
		}
		stackPush.push(n);
		console.log( n + " 成功入隊!");
	}
	function popQueue(){  // 出隊操作
		if (stackPush.length == 0 && stackPop.length == 0) {
			console.log("This Queue is empty!");
			return;
		}
		if (stackPop.length == 0 && stackPush.length != 0) {
			stackPop = (stackPush.splice(1,stackPush.length-1)).reverse();
			console.log( stackPush.pop() + " 成功出隊!");
			return;
		}
		console.log( stackPop.pop() + " 成功出隊!");
	}
	pushQueue(3);  // 3 成功入隊!
	pushQueue(5);  // 5 成功入隊!
	pushQueue(8);  // 8 成功入隊!
	peek();  // 隊頭元素:3
	popQueue();  // 3 成功出隊!
	pushQueue(1);  // 1 成功入隊!
	peek();  // 隊頭元素:5
	popQueue();  // 5 成功出隊!
	popQueue();  // 5 成功出隊!
	popQueue();  // 5 成功出隊!
	popQueue();  // This Queue is empty!
</script>

四、總結

        通過這次的學習,我完成了數組結構實現隊列、棧的邏輯實現,以及僅用隊列結構實現棧結構和僅用棧結構實現隊列結構的實現。收穫還是很大的,更瞭解了這三者之間的位置轉換,也對js的函數、變量等有了更深刻的理解,更深刻的是熟悉了部分數組方法的使用。再接再厲吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章