赞
踩
目录
栈的push 和 pop操作大概可以用下面这张图来进行表示:
栈在现实生活中的例子:
在Java中,关于栈的常用的方法有以下几个:
我们可以用代码来感受一下这些方法的使用:
- public static void main(String[] args) {
- Stack<Integer> s = new Stack();
- s.push(1);
- s.push(2);
- s.push(3);
- s.push(4);
- System.out.println(s.size()); // 获取栈中有效元素个数---> 4
- System.out.println(s.peek()); // 获取栈顶元素---> 4
- s.pop(); // 4出栈,栈中剩余1 2 3,栈顶元素为3
- System.out.println(s.pop()); // 3出栈,栈中剩余1 2 栈顶元素为3
- if(s.empty()){
- System.out.println("栈空");
- }else{
- System.out.println(s.size());
- }
- }

- public int push(int e){
- ensureCapacity();
- array[size++] = e;
- return e;
- }
pop方法
- public int pop(){
- int e = peek();
- size--;
- return e;
- }
peek方法
- public int peek(){
- if(empty()){
- throw new RuntimeException("栈为空,无法获取栈顶元素");
- }
- return array[size-1];
- }
我们可以根据栈的先进后出的特性来对其进行分析
我们可以发现,A选项中是先将1入栈再出栈,再将234依次入栈后出栈所得到的结果
B选项是先将1入栈,再将234轮流入栈出栈,最后将1出栈得到的结果
C选项是不可能做到的,因为如果按照31的顺序出栈,那么2必然先比1出栈
D选项则是先把12入栈,再将34轮流进行出栈入栈的操作后,再将12出栈
根据这道例题,我们发现可以运用栈来改变元素的序列
2. 将递归转化为循环
- // 递归方式
- void printList(Node head){
- if(null != head){
- printList(head.next);
- System.out.print(head.val + " ");
- }
- }
而如果我们使用栈,那么代码就会演变成这样:
- void printList(Node head){
- if(null == head){
- return;
- }
- Stack<Node> s = new Stack<>();
- // 将链表中的结点保存在栈中
- Node cur = head;
- while(null != cur){
- s.push(cur);
- cur = cur.next;
- }
- // 将栈中的元素出栈
- while(!s.empty()){
- System.out.print(s.pop().val + " ");
- }
- }

综上,我们可以得知:通过栈的实现,我们可以将递归转化成循环
那么在Java中,常见的队列的方法有以下几种:
注意:Queue是个接口,在实例化时必须实例化LinkedList的对象,因为LinkedList实现了Queue接口。
- public static void main(String[] args) {
- Queue<Integer> q = new LinkedList<>();
- q.offer(1);
- q.offer(2);
- q.offer(3);
- q.offer(4);
- q.offer(5); // 从队尾入队列
- System.out.println(q.size());
- System.out.println(q.peek()); // 获取队头元素
- q.poll();
- System.out.println(q.poll()); // 从队头出队列,并将删除的元素返回
- if(q.isEmpty()){
- System.out.println("队列空");
- }else{
- System.out.println(q.size());
- }
- }

-
- public class Queue {
- // 双向链表节点
- public static class ListNode{
- ListNode next;
- ListNode prev;
- int value;
- ListNode(int value){
- this.value = value;
- }
- }
- ListNode first; // 队头
- ListNode last; // 队尾
- int size = 0;
- // 入队列---向双向链表位置插入新节点
- public void offer(int e){
- ListNode newNode = new ListNode(e);
- if(first == null){
- first = newNode;
- // last = newNode;
- }else{
- last.next = newNode;
- newNode.prev = last;
- // last = newNode;
- }
- last = newNode;
- size++;
- }
- // 出队列---将双向链表第一个节点删除掉
- public int poll(){
- // 1. 队列为空
- // 2. 队列中只有一个元素----链表中只有一个节点---直接删除
- // 3. 队列中有多个元素---链表中有多个节点----将第一个节点删除
- int value = 0;
- if(first == null){
- return null;
- }else if(first == last){
- last = null;
- first = null;
- }else{
- value = first.value;
- first = first.next;
- first.prev.next = null;
- first.prev = null;
- }
- --size;
- return value;
- }
- // 获取队头元素---获取链
- public int peek(){
- if(first == null){
- return null;
- }
- return first.value;
- }
- public int size() {
- return size;
- }
- public boolean isEmpty(){
- return first == null;
- }
- }

数组下标循环的小技巧
如何区分空与满
1. 通过添加 size 属性记录2. 保留一个位置3. 使用标记
Deque是一个接口,使用时必须创建LinkedList的对象。
在实际工程中,使用Deque接口是比较多的,栈和队列均可以使用该接口。
- Deque<Integer> stack = new ArrayDeque<>();//双端队列的线性实现
- Deque<Integer> queue = new LinkedList<>();//双端队列的链式实现
- class MyStack {
- Queue<Integer> queue1;
- Queue<Integer> queue2;
-
- public MyStack() {
- queue1 = new LinkedList<Integer>();
- queue2 = new LinkedList<Integer>();
- }
-
- public void push(int x) {
- queue2.offer(x);
- while (!queue1.isEmpty()) {
- queue2.offer(queue1.poll());
- }
- Queue<Integer> temp = queue1;
- queue1 = queue2;
- queue2 = temp;
- }
-
- public int pop() {
- return queue1.poll();
- }
-
- public int top() {
- return queue1.peek();
- }
-
- public boolean empty() {
- return queue1.isEmpty();
- }
- }

- class MyQueue {
- public Stack<Integer> stack1;
- public Stack<Integer> stack2;
- public MyQueue() {
- stack1 = new Stack<>();
- stack2 = new Stack<>();
- }
-
- public void push(int x) {
- stack1.push(x);
- }
-
- public int pop() {
- if (stack2.isEmpty()) {
- in2out();
- }
- return stack2.pop();
- }
-
- public int peek() {
- if (stack2.isEmpty()){
- in2out();
- }
- return stack2.peek();
- }
-
- public boolean empty() {
- return stack1.isEmpty() && stack2.isEmpty();
- }
- private void in2out() {
- while (!stack1.isEmpty()) {
- stack2.push(stack1.pop());
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。