大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 使用隊列實現堆棧

使用隊列實現堆棧

更新時間:2022-07-01 09:26:10 來源:動力節點 瀏覽1283次

可以使用兩個隊列來實現Java堆棧。讓要實現的堆棧為“s”,用于實現的隊列為“q1”和“q2”。Stack 's' 可以通過兩種方式實現:

方法1:通過使 push 操作代價高昂

該方法確保新進入的元素始終位于 'q1' 的前面,因此 pop 操作只是從 'q1' 出列。'q2' 用于將每個新元素放在 'q1' 的前面。

1.push(s, x)操作的步驟描述如下:

將 x 排入隊列 q2

從 q1 逐一出列并入隊到 q2。

交換 q1 和 q2 的名稱

2.pop(s)操作的功能描述如下:

從 q1 中取出一個項目并返回它。

下面是上述方法的實現:

/* Java Program to implement a stack using
two queue */
import java.util.*;
class GfG {
	static class Stack {
		// Two inbuilt queues
		static Queue<Integer> q1 = new LinkedList<Integer>();
		static Queue<Integer> q2 = new LinkedList<Integer>();
		// To maintain current number of
		// elements
		static int curr_size;
		Stack()
		{
			curr_size = 0;
		}
		static void push(int x)
		{
			curr_size++;
			// Push x first in empty q2
			q2.add(x);
			// Push all the remaining
			// elements in q1 to q2.
			while (!q1.isEmpty()) {
				q2.add(q1.peek());
				q1.remove();
			}
			// swap the names of two queues
			Queue<Integer> q = q1;
			q1 = q2;
			q2 = q;
		}
		static void pop()
		{
			// if no elements are there in q1
			if (q1.isEmpty())
				return;
			q1.remove();
			curr_size--;
		}
		static int top()
		{
			if (q1.isEmpty())
				return -1;
			return q1.peek();
		}
		static int size()
		{
			return curr_size;
		}
	}
	// driver code
	public static void main(String[] args)
	{
		Stack s = new Stack();
		s.push(1);
		s.push(2);
		s.push(3);
		System.out.println("current size: " + s.size());
		System.out.println(s.top());
		s.pop();
		System.out.println(s.top());
		s.pop();
		System.out.println(s.top());
		System.out.println("current size: " + s.size());
	}
}
// This code is contributed by Prerna

輸出

當前尺寸:3
3
2
1
當前尺寸:1

方法2:通過使pop操作成本高

在push操作中,新元素總是排隊到q1。在 pop() 操作中,如果 q2 為空,則除了最后一個元素之外的所有元素都被移動到 q2。最后最后一個元素從 q1 出列并返回。

1.push(s, x)操作:

將 x 排入隊列 q1(假設 q1 的大小是無限的)。

2.彈出操作:

除了從 q1 到 q2 的最后一個元素,一個接一個地從隊列中取出所有元素。

將q1的最后一項出隊,出隊的項就是result,存儲起來。

交換 q1 和 q2 的名稱

返回步驟 2 中存儲的項目。

/* Java Program to implement a stack
using two queue */
import java.util.*;
class Stack {
	Queue<Integer> q1 = new LinkedList<>(), q2 = new LinkedList<>();
	int curr_size;
	public Stack()
	{
		curr_size = 0;
	}
	void remove()
	{
		if (q1.isEmpty())
			return;
		// Leave one element in q1 and
		// push others in q2.
		while (q1.size() != 1) {
			q2.add(q1.peek());
			q1.remove();
		}
		// Pop the only left element
		// from q1
		q1.remove();
		curr_size--;
		// swap the names of two queues
		Queue<Integer> q = q1;
		q1 = q2;
		q2 = q;
	}
	void add(int x)
	{
		q1.add(x);
		curr_size++;
	}
	int top()
	{
		if (q1.isEmpty())
			return -1;
		while (q1.size() != 1) {
			q2.add(q1.peek());
			q1.remove();
		}
		// last pushed element
		int temp = q1.peek();
		// to empty the auxiliary queue after
		// last operation
		q1.remove();
		// push last element to q2
		q2.add(temp);
		// swap the two queues names
		Queue<Integer> q = q1;
		q1 = q2;
		q2 = q;
		return temp;
	}
	int size()
	{
		return curr_size;
	}
	// Driver code
	public static void main(String[] args)
	{
		Stack s = new Stack();
		s.add(1);
		s.add(2);
		s.add(3);
		s.add(4);
		System.out.println("current size: " + s.size());
		System.out.println(s.top());
		s.remove();
		System.out.println(s.top());
		s.remove();
		System.out.println(s.top());
		System.out.println("current size: " + s.size());
	}
}
// This code is contributed by Princi Singh

輸出

當前尺寸:4
4
3
2
當前尺寸:2

以上就是關于“使用隊列實現堆棧”的介紹,大家如果想了解更多相關知識,不妨來關注一下動力節點的Java隊列教程,里面有更豐富的知識等著大家去學習,相信對大家會有很大幫助的。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 日韩欧美一级毛片在线 | 在线成人免费观看国产精品 | 五月亭亭免费高清在线 | 泰国一级毛片aaa下面毛多 | 国产一级精品高清一级毛片 | 狠狠色丁香久久婷婷综合_中 | 99国产精品热久久久久久夜夜嗨 | 91久久精品午夜一区二区 | 亚洲日本在线播放 | 久久综合九色综合精品 | 国产极品粉嫩福利在线观看 | 手机看片一区 | 成人永久免费视频网站在线观看 | 中国精品白嫩bbwbbw | 亚洲 欧美 日韩 在线 香蕉 | 91久久精品一区二区三区 | 97国产在线视频公开免费 | 四虎影院黄色 | 天天做天天玩天天爽天天 | 奇米影视亚洲色图 | 荔枝污 | 国产区视频在线观看 | 最新中文字幕一区 | 色久阁| 成人亚洲欧美日韩中文字幕 | 久久亚洲视频 | 国产精品_国产精品_国产精品 | 91美女啪啪 | 四虎影视永久免费观看地址 | 国产日本欧美亚洲精品视 | 国产精品不卡视频 | 亚洲看片 | 国产观看精品一区二区三区 | 爱爱免费 | 精品中文字幕一区在线 | 欧美一区二区三区久久久人妖 | 国产中文字幕在线免费观看 | 日本黄页在线观看 | 日本aaaa级毛片在线看 | 亚洲视频在线网 | 中文字幕在线观看亚洲 |