Java创建栈和队列的几种方式
1.创建队列
1.1 使用Queue接口 , Queue的实现类有LinkedList和PriorityQueue。最常用的实现类是LinkedList。
Queue的六种方法:
add()和 offer()
向队列中添加元素,将元素压入队尾。当超出容量时add()会抛出异常 , offer()会返回false。
remove() 和 poll()
移除元素,将元素从队头移出。当当前容量为0执行移除操作时,remove()会抛出异常 , poll()会返回false。
element() 和 peek()
获取队头元素,当前容量为0时。element()会抛出异常 , peek()会返回null。
2.创建栈
使用Stack类,Stack是Java中本身具有的集合类型,所包含的方法包括:
boolean isEmpty() // 判断当前栈是否为空
synchronized E peek() //获得当前栈顶元素
synchronized E pop() //获得当前栈顶元素并删除
push(E object) //将元素加入栈顶
synchronized int search(Object o) //查找元素在栈中的位置,由栈低向栈顶方向数
3.Deque接口。
Deque是一个双端队列接口,继承自Queue接口,Deque的实现类是LinkedList、ArrayDeque、LinkedBlockingDeque,其中LinkedList是最常用的。
1 public static void main(String[] args) { 2 //使用offer添加元素,得到 3 Deque<Integer> test = new LinkedList<>(); 4 System.out.println("测试offer相关函数**************************************"); 5 test.offer(1); 6 System.out.println(test); 7 test.offerFirst(0); 8 System.out.println(test); 9 //test.offerLast(2); 10 test.offer(2); 11 System.out.println(test); 12 /** 13 * offer() 与 offerLast()都是将元素添加到右边即队尾 14 * offerFirst()将元素添加到左边即队头 15 * push()是将元素压在左边队头 16 */ 17 test.offer(3); 18 test.offer(4); 19 test.offer(5); 20 test.offer(6); 21 test.offer(7); 22 test.offer(8); 23 test.offer(9); 24 test.offer(10); 25 test.offer(11); 26 test.offer(12); 27 28 System.out.println(test); 29 test.poll(); 30 System.out.println(test); 31 test.pollLast(); 32 System.out.println(test); 33 test.pollFirst(); 34 System.out.println(test); 35 /** 36 * poll()与pollFirst()等价,都是将队头元素移除 37 * pollLast()是将队尾元素移除 38 */ 39 System.out.println("测试peek,element相关函数****************************************"); 40 int temp = test.peek(); 41 System.out.println(temp); 42 temp = test.peekLast(); 43 System.out.println(temp); 44 temp = test.element(); 45 System.out.println(temp); 46 temp = test.pop(); //这时候调用pop()返回的是队头元素 47 System.out.println(temp); 48 System.out.println(test); 49 System.out.println(test.contains(6)); //还有contain()函数判断是否包含某元素 50 /** 51 * peek()和peekFirst()都是返回队头元素 52 * peekLast()是返回队尾元素 53 */ 54 System.out.println("测试remove相关函数**********************************************"); 55 test.remove(); 56 System.out.println(test); 57 test.removeFirst(); 58 System.out.println(test); 59 test.removeLast(); 60 System.out.println(test); 61 /** 62 * remove()和remove()是移除队头元素 63 * removeLast()是移除队尾元素 64 */ 65 int pop = test.pop(); 66 System.out.println(pop); 67 System.out.println(test); 68 /** 69 * 记住当是队列时,使用pop()弹出队头的元素 70 */ 71 test.push(5); 72 System.out.println(test); 73 test.offer(5); 74 System.out.println(test); 75 test.poll(); 76 System.out.println(test); 77 test.add(11); 78 System.out.println(test); 79 test.addLast(12); 80 System.out.println(test); 81 test.addFirst(13); 82 System.out.println(test); 83 /** 84 * add()和addLast()将元素加在右边,即队尾,和offer()方法等价 85 * addFirst()将元素加在左边,即队头 86 */ 87 System.out.println(); 88 System.out.println(); 89 System.out.println(); 90 System.out.println(); 91 System.out.println("*********************************测试Deque实现堆栈相关函数********************************"); 92 }
运行结果:
测试offer相关函数**************************************
[1]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
测试peek,element相关函数****************************************
2
11
2
2
[3, 4, 5, 6, 7, 8, 9, 10, 11]
true
测试remove相关函数**********************************************
[4, 5, 6, 7, 8, 9, 10, 11]
[5, 6, 7, 8, 9, 10, 11]
[5, 6, 7, 8, 9, 10]
5
[6, 7, 8, 9, 10]
[5, 6, 7, 8, 9, 10]
[5, 6, 7, 8, 9, 10, 5]
[6, 7, 8, 9, 10, 5]
[6, 7, 8, 9, 10, 5, 11]
[6, 7, 8, 9, 10, 5, 11, 12]
[13, 6, 7, 8, 9, 10, 5, 11, 12]
Deque使用总结:
3.1添加操作相关函数:
offer() :将元素添加到队尾,即添加到右侧
offerFirst():添加到队头
offerLast():添加到队头
add():将元素添加到队尾,即添加到右侧
addFirst():添加到队头
addLast():添加到队尾
push():将元素添加到队头,即添加到左侧
3.2 获取头部元素peek相关操作
peek() : 返回队头元素,即最左边的元素
peekFirst():返回队头元素
peekLast():返回队尾元素,即最右边的元素
element():返回队头元素,即最左边的元素
pop():返回队头元素即最左边的元素,并将其从队列中删除
3.3 remove相关函数
remove():移除队头元素,即最左边的元素
removeFirst():移除队头元素
removeLast():移除队尾元素
4.3 poll相关函数
poll():返回队头元素并移除,即最左边的元素
pollFirst():返回队头元素,即最左边的元素
pollLast():返回队尾元素,即最右边的元素
总结:当实现队列时,使用offer()进行添加操作 , 使用poll()进行出队操作,offer()和poll()要搭配使用
当实现堆栈时,使用push()进行添加操作,使用pop()进行出队操作,push()和pop()要搭配使用
peek()是返回队头元素或栈顶元素即最左边的元素。

浙公网安备 33010602011771号