一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現棧和隊列面試題

Java實現棧和隊列面試題

2020-01-05 14:28lijiao JAVA教程

這篇文章主要介紹了Java實現棧和隊列的面試題,每個例題代碼實現非常詳細,每一個方法講解也很到位,特別適合參加Java面試的朋友閱讀。

面試的時候,棧和隊列經常會成對出現來考察。本文包含棧和隊列的如下考試內容:

(1)棧的創建

(2)隊列的創建

(3)兩個棧實現一個隊列

(4)兩個隊列實現一個棧

(5)設計含最小函數min()的棧,要求min、push、pop、的時間復雜度都是O(1)

(6)判斷棧的push和pop序列是否一致

1、棧的創建:

我們接下來通過鏈表的形式來創建棧,方便擴充。

代碼實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Stack {
 
public Node head;
 public Node current;
 
//方法:入棧操作
 public void push(int data) {
  if (head == null) {
   head = new Node(data);
   current = head;
  } else {
   Node node = new Node(data);
   node.pre = current;//current結點將作為當前結點的前驅結點
   current = node; //讓current結點永遠指向新添加的那個結點
  }
 }
 
public Node pop() {
  if (current == null) {
   return null;
  }
 
Node node = current; // current結點是我們要出棧的結點
  current = current.pre; //每出棧一個結點后,current后退一位
  return node;
 
}
 
class Node {
  int data;
  Node pre; //我們需要知道當前結點的前一個結點
 
public Node(int data) {
   this.data = data;
  }
 }
 
public static void main(String[] args) {
 
Stack stack = new Stack();
  stack.push(1);
  stack.push(2);
  stack.push(3);
 
System.out.println(stack.pop().data);
  System.out.println(stack.pop().data);
  System.out.println(stack.pop().data);
 }
 
}

入棧操作時,14、15行代碼是關鍵。

運行效果:

Java實現棧和隊列面試題

2、隊列的創建:

隊列的創建有兩種形式:基于數組結構實現(順序隊列)、基于鏈表結構實現(鏈式隊列)。

我們接下來通過鏈表的形式來創建隊列,這樣的話,隊列在擴充時會比較方便。隊列在出隊時,從頭結點head開始。

代碼實現:

入棧時,和在普通的鏈表中添加結點的操作是一樣的;出隊時,出的永遠都是head結點。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Queue {
 public Node head;
 public Node curent;
 
//方法:鏈表中添加結點
 public void add(int data) {
  if (head == null) {
   head = new Node(data);
   curent = head;
  } else {
   curent.next = new Node(data);
   curent = curent.next;
  }
 }
 
//方法:出隊操作
 public int pop() throws Exception {
  if (head == null) {
   throw new Exception("隊列為空");
  }
 
Node node = head; //node結點就是我們要出隊的結點
  head = head.next; //出隊之后,head指針向下移
 
return node.data;
 
}
 
class Node {
  int data;
  Node next;
 
public Node(int data) {
   this.data = data;
  }
 }
 
public static void main(String[] args) throws Exception {
  Queue queue = new Queue();
  //入隊操作
  for (int i = 0; i < 5; i++) {
   queue.add(i);
  }
 
//出隊操作
  System.out.println(queue.pop());
  System.out.println(queue.pop());
  System.out.println(queue.pop());
 
}
}

運行效果:

Java實現棧和隊列面試題

3、兩個棧實現一個隊列:

思路:

棧1用于存儲元素,棧2用于彈出元素,負負得正。

說的通俗一點,現在把數據1、2、3分別入棧一,然后從棧一中出來(3、2、1),放到棧二中,那么,從棧二中出來的數據(1、2、3)就符合隊列的規律了,即負負得正。

完整版代碼實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.Stack;
 
/**
* Created by smyhvae on 2015/9/9.
*/
public class Queue {
 
private Stack<Integer> stack1 = new Stack<>();//執行入隊操作的棧
 private Stack<Integer> stack2 = new Stack<>();//執行出隊操作的棧
 
//方法:給隊列增加一個入隊的操作
 public void push(int data) {
  stack1.push(data);
 
}
 
//方法:給隊列正價一個出隊的操作
 public int pop() throws Exception {
 
if (stack2.empty()) {//stack1中的數據放到stack2之前,先要保證stack2里面是空的(要么一開始就是空的,要么是stack2中的數據出完了),不然出隊的順序會亂的,這一點很容易忘
 
while (!stack1.empty()) {
    stack2.push(stack1.pop());//把stack1中的數據出棧,放到stack2中【核心代碼】
   }
 
}
 
if (stack2.empty()) { //stack2為空時,有兩種可能:1、一開始,兩個棧的數據都是空的;2、stack2中的數據出完了
   throw new Exception("隊列為空");
  }
 
return stack2.pop();
 }
 
public static void main(String[] args) throws Exception {
  Queue queue = new Queue();
  queue.push(1);
  queue.push(2);
  queue.push(3);
 
System.out.println(queue.pop());
 
queue.push(4);
 
System.out.println(queue.pop());
  System.out.println(queue.pop());
  System.out.println(queue.pop());
 
}
 
}

注意第22行和第30行代碼的順序,以及注釋,需要仔細理解其含義。

運行效果:

Java實現棧和隊列面試題

4、兩個隊列實現一個棧:

思路:

將1、2、3依次入隊列一, 然后最上面的3留在隊列一,將下面的2、3入隊列二,將3出隊列一,此時隊列一空了,然后把隊列二中的所有數據入隊列一;將最上面的2留在隊列一,將下面的3入隊列二。。。依次循環。

代碼實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayDeque;
import java.util.Queue;
 
/**
* Created by smyhvae on 2015/9/9.
*/
public class Stack {
 
Queue<Integer> queue1 = new ArrayDeque<Integer>();
 Queue<Integer> queue2 = new ArrayDeque<Integer>();
 
//方法:入棧操作
 public void push(int data) {
  queue1.add(data);
 }
 
//方法:出棧操作
 public int pop() throws Exception {
  int data;
  if (queue1.size() == 0) {
   throw new Exception("棧為空");
  }
 
while (queue1.size() != 0) {
   if (queue1.size() == 1) {
    data = queue1.poll();
    while (queue2.size() != 0) { //把queue2中的全部數據放到隊列一中
     queue1.add(queue2.poll());
     return data;
    }
   }
   queue2.add(queue1.poll());
  }
  throw new Exception("棧為空");//不知道這一行的代碼是什么意思
 }
 
public static void main(String[] args) throws Exception {
  Stack stack = new Stack();
 
stack.push(1);
  stack.push(2);
  stack.push(3);
 
System.out.println(stack.pop());
  System.out.println(stack.pop());
  stack.push(4);
 }
}

運行效果:

Java實現棧和隊列面試題

5、設計含最小函數min()的棧,要求min、push、pop、的時間復雜度都是O(1)。min方法的作用是:就能返回是棧中的最小值。【微信面試題】

普通思路:

一般情況下,我們可能會這么想:利用min變量,每次添加元素時,都和min元素作比較,這樣的話,就能保證min存放的是最小值。但是這樣的話,會存在一個問題:如果最小的元素出棧了,那怎么知道剩下的元素中哪個是最小的元素呢?

改進思路:

這里需要加一個輔助棧,用空間換取時間。輔助棧中,棧頂永遠保存著當前棧中最小的數值。具體是這樣的:原棧中,每次添加一個新元素時,就和輔助棧的棧頂元素相比較,如果新元素小,就把新元素的值放到輔助棧中,如果新元素大,就把輔助棧的棧頂元素再copy一遍放到輔助棧的棧頂;原棧中,出棧時,

完整代碼實現:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.Stack;
 
/**
* Created by smyhvae on 2015/9/9.
*/
public class MinStack {
 
private Stack<Integer> stack = new Stack<Integer>();
 private Stack<Integer> minStack = new Stack<Integer>(); //輔助棧:棧頂永遠保存stack中當前的最小的元素
 
public void push(int data) {
  stack.push(data); //直接往棧中添加數據
 
//在輔助棧中需要做判斷
  if (minStack.size() == 0 || data < minStack.peek()) {
   minStack.push(data);
  } else {
   minStack.add(minStack.peek()); //【核心代碼】peek方法返回的是棧頂的元素
  }
 }
 
public int pop() throws Exception {
  if (stack.size() == 0) {
   throw new Exception("棧中為空");
  }
 
int data = stack.pop();
  minStack.pop(); //核心代碼
  return data;
 }
 
public int min() throws Exception {
  if (minStack.size() == 0) {
   throw new Exception("棧中空了");
  }
  return minStack.peek();
 }
 
public static void main(String[] args) throws Exception {
  MinStack stack = new MinStack();
  stack.push(4);
  stack.push(3);
  stack.push(5);
 
System.out.println(stack.min());
 }
}

運行效果:

Java實現棧和隊列面試題

6、判斷棧的push和pop序列是否一致:

通俗一點講:已知一組數據1、2、3、4、5依次進棧,那么它的出棧方式有很多種,請判斷一下給出的出棧方式是否是正確的?

例如:

數據:

1、2、3、4、5

出棧1:

5、4、3、2、1(正確)

出棧2:

4、5、3、2、1(正確)

出棧3:

4、3、5、1、2(錯誤)

完整版代碼:

?
1
2
3
import java.util.Stack;
/**
* Created by smyhvae on 2015/9/9.<br>*/<br>public class StackTest {<br><br>//方法:data1數組的順序表示入棧的順序。現在判斷data2的這種出棧順序是否正確<br>    public static boolean sequenseIsPop(int[] data1, int[] data2) {<br>        Stack<Integer> stack = new Stack<Integer>(); //這里需要用到輔助棧<br><br>for (int i = 0, j = 0; i < data1.length; i++) {<br>            stack.push(data1[i]);<br><br>while (stack.size() > 0 && stack.peek() == data2[j]) {<br>                stack.pop();<br>                j++;<br>            }<br>        }<br>        return stack.size() == 0;<br>    }<br><br>public static void main(String[] args) {<br><br>Stack<Integer> stack = new Stack<Integer>();<br><br>int[] data1 = {1, 2, 3, 4, 5};<br>        int[] data2 = {4, 5, 3, 2, 1};<br>        int[] data3 = {4, 5, 2, 3, 1};<br><br>System.out.println(sequenseIsPop(data1, data2));<br>        System.out.println(sequenseIsPop(data1, data3));<br>    }<br>}<br><br>

代碼比較簡潔,但也比較難理解,要仔細體會。

運行效果:

Java實現棧和隊列面試題

以上就是有關java棧和隊列的經典面試題目,希望可以幫助大家順利通過面試。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美三茎同入 | 97网站| 亚洲视频在线免费观看 | 91香蕉视频导航 | 亚洲国产精品久久久久 | 91麻豆制片厂| 国产免费专区 | 日本高清免费中文字幕不卡 | 成人一区二区丝袜美腿 | 成年女人毛片免费观看中文w | 午夜福利在线观看6080 | 国产精品免费精品自在线观看 | 99久久999久久久综合精品涩 | 99久久这里只有精品 | 色哟哟观看 | 激情视频激情小说 | 5月色婷婷| 花房乱爱在线观看 | 色综合 成人 | 国产午夜精品久久久久小说 | 亚洲精品乱码蜜桃久久久 | 香蕉91| 韩国美女被的免费视频 | 婚前试爱全集免费观看 | 99久久精品99999久久 | 亚洲国产综合自在线另类 | 亚洲午夜精品久久久久久人妖 | 欧美日韩国产成人精品 | 日韩视频一区 | 美国高清xxxxx18| 性xxxx欧美高清 | 男人j进女屁股视频在线观看 | 国产精品亚洲片夜色在线 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 亚洲国产精品无码中文在线 | 国产亚洲欧美在线中文bt天堂网 | 久久久久国产一级毛片高清片 | chinese男男gayxxx chinese老头和老太交hd | 疯狂伦交1一6小说 | 日韩资源在线 | 好涨好大我快受不了了视频网 |