用数组实现栈和队列,以及栈和队列的相互实现

1.用数组实现栈
栈:先进后出

//用数组实现栈
//1.压栈
//2.出栈
//3.判断栈是否为空
public class Stack {
    //首先需要一个数组以及栈的空间大小
    private int[] arr;
    private int size;
    private int cur;

    public Stack(int size) {
        arr = new int[size];
        this.size = size;
        cur = 0;
    }

    //压栈
    public void push(int num) {
        if(cur == size) {
            System.out.println("栈已满,无法入栈");
        }else {
            arr[cur++] = num;
        }
    }

    //出栈
    public int pop() throws Exception {
        if(isEmpty()) {
            throw new Exception("栈为空,无法出栈元素");
        }else {
            return arr[--cur];
        }
    }
    //获取当前栈顶的元素,但不弹出
    public int peek() {
        return arr[--cur];
    }
    //判断栈是否为空,需要确定一个表示当前栈的指针
    public boolean isEmpty() {
        return cur == 0;
    }
    //判断栈是否已满
    public boolean inFull() {
        return cur == size;
    }
    //获取当前栈的大小
    public int getSize() {
        return size;
    }

2.用数组实现队列(该队列为循环队列)
队列:先进先出

public class Queue {
    private int[] arr;
    private int size;
    private int front;
    private int tail;

    public Queue(int size) {
        arr = new int[size];
        front = 0;
        tail = 0;
        size = 0;
    }
    //入队
    public void push(int obj) {
        if (size == arr.length) {
            throw new ArrayIndexOutOfBoundsException("队列已满");
        }else {
            size++;
            arr[tail] = obj;
            tail = tail == arr.length-1? 0: tail+1;
        }
    }
    //出队
    public int poll() {
        if (size == 0) {
            throw new ArrayIndexOutOfBoundsException("队列为空");
        }
        size--;
        int tmp = front;
        front = front == arr.length? 0: front+1;
        return arr[tmp];
    }

    //获取第一个元素
    public int peek() {
        if (size == 0) {
            throw new ArrayIndexOutOfBoundsException("队列为空");
        }
        return arr[front];
    }

    //判断队列是否为空:
    public boolean isEmpy() {
        return size == 0;
    }
}

3.用两个栈实现一个队列

import java.util.Stack;
public class TwoStackQueue {
    private Stack<Integer> stackPush;
    private Stack<Integer> stackPop;

    public TwoStackQueue() {
        stackPush = new Stack<Integer>();
        stackPop = new Stack<Integer>();
    }

    //压队:直接将元素压入到push栈中
    public void add(int obj) {
        stackPush.push(obj);
    }

    //出队:首先判断push栈的元素是否为空或者元素的个数是否为1,若是抛异常或者直接弹出
    //否则,就将push栈的元素弹出压入到pop栈中,并留取一个元素,直接弹出,然后将pop栈中的数据返回到push中
    public int poll() {
        if (stackPush.isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        if (stackPush.size() != 1) {
            while (stackPush.size() != 1) {
                stackPop.push(stackPush.pop());
            }
            int tmp = stackPush.pop();
            dao();
            return tmp;
        }
        return stackPush.pop();
    }
    //将StackPop的数据全都倒入StackPush中
    public void dao() {
        if(stackPop.isEmpty()) {
            throw new RuntimeException("栈的元素为空,无法进行该操作");
        }else {
            while (!stackPop.isEmpty()) {
                stackPush.push(stackPop.pop());
            }
        }
    }

4.用两个队列实现栈


import java.util.LinkedList;
import java.util.Queue;

public class TwoQueueStack {
    private Queue<Integer> data;
    private Queue<Integer> help;

    public TwoQueueStack() {
        data = new LinkedList<Integer>();
        help = new LinkedList<Integer>();
    }

    //入栈
    public void push(int obj) {
        data.add(obj);
    }

    //出栈
    public int pop() {
        if (data.isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        while (data.size() > 1) {
            help.add(data.poll());
        }
        int res = data.poll();
        swap();
        return res;
    }

    //获取第一个栈顶的元素
    public int peek() {
        if (data.isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        while (data.size() > 1) {
            help.add(data.poll());
        }
        int res = data.poll();
        help.add(res);
        swap();
        return res;
    }

    //交换引用
    public void swap() {
        Queue<Integer> tmp = help;
        help = data;
        data = tmp;
    }

}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值