2022.03.08 学习栈和队列

基础:栈和队列

参考:java数据结构与算法——栈与队列

顺序存储结构:使用数组实现栈

public class StackArray {
    //需要一个数组来保存栈的元素
    private int[] stack;
    //需要一个指针指向栈顶元素
    private  int point;

    public StackArray(int size){
        this.stack = new int[size];
        //-1表示栈空
        this.point = -1;
    }
    public void push(int elem){
        stack[++this.point] = elem;
    }

    public int pop(){
        if(!empty()){
            return stack[this.point--];
        }else{
            return -1;
        }
    }

    public int peek(){
        if(!empty()){
            return stack[this.point];
        }else{
            return -1;
        }
    }

    public  boolean empty(){
        if(this.point == -1){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        StackArray stackArray = new StackArray(10);
        stackArray.push(1);
        stackArray.push(2);
        stackArray.push(3);
        System.out.println(stackArray.peek());//3
        System.out.println(stackArray.pop());//3
        System.out.println(stackArray.peek());//2
        System.out.println(stackArray.pop());//2
        System.out.println(stackArray.pop());//1
        System.out.println(stackArray.empty());
        System.out.println(stackArray.pop());//-1
   }
}

链式存储结构(推荐)

package StackAndQueue;


public class StackList {
    //定义栈顶元素
    private Node top;
    //初始化
    public StackList(){
        this.top = null;
    }

    public void push(int i){
        //注意:栈的指针是从上向下的
        this.top = new Node(i, top);
    }

    public int pop(){
        if(!empty()){
            int val = this.top.element;
            this.top = this.top.next;
            return val;
        }
        return -1;
    }

    public int peak(){
        if(!empty()){
            int val = this.top.element;
            return val;
        }
        return -1;
    }

    public boolean empty(){
        if(this.top == null){
            return true;
        }
        else{
            return false;
        }
    }

    public static void main(String[] args) {
        StackList stackList = new StackList();
        stackList.push(1);
        stackList.push(2);
        stackList.push(3);
        System.out.println(stackList.peak());//3
        System.out.println(stackList.pop());//3
        System.out.println(stackList.peak());//2
        System.out.println(stackList.pop());//2
        System.out.println(stackList.pop());//1
        System.out.println(stackList.empty());
        System.out.println(stackList.pop());//-1
    }
}

队列

顺序存储结构

/*
用数组实现的队列是循环队列,这样可以节省空间
为了区分队列是满还是空,数组中需要有一个位置为空 -- 一般来说数组0位置为空
首先让front = rear = 0,进队 (rear + 1) % length,出队front++
这样就出现了数组0位置为空(刚开始),以后不一定空出什么位置
满:(rear+1) % length == front
空:rear = front
 */

public class QueueArray {
    int[] queue;
    int size;
    int front,rear;

    public QueueArray(int i){
        size = i;
        queue = new int[i];
        front = 0;
        rear = 0;
    }

    public void enter(int i){
        if(!full()){
            //先确定rear新位置,在更新
            rear = (rear + 1) % size;
            queue[rear] = i;
        }
    }

    public int leave(){
        if(!empty()){
            //指向下一个位置
            front = (front + 1) % size;
            //理解为当前指向的为空
            return queue[front];
        }
        return  -1;
    }

    public  int peek(){
        if(!empty()){
            return queue[(front + 1) % size];
        }
        return -1;
    }

    public boolean empty(){
        if(rear == front){
            return true;
        }
        return false;
    }

    public boolean full(){
        if((rear + 1) % size == front){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        QueueArray queueArray = new QueueArray(5);
        queueArray.enter(1);
        queueArray.enter(2);
        queueArray.enter(3);
        queueArray.enter(4);
        System.out.println(queueArray.full());//true
        System.out.println(queueArray.peek());//1
        System.out.println(queueArray.leave());//1
        System.out.println(queueArray.leave());//2
        System.out.println(queueArray.peek());//3
        System.out.println(queueArray.empty());//false
        System.out.println(queueArray.leave());//3
        System.out.println(queueArray.leave());//4
        System.out.println(queueArray.empty());//true


    }
}

链式存储结构

package StackAndQueue;

public class QueueList {
    //定义头节点、尾节点
    Node front;
    Node rear;

    //队列为空时,front和rear都为null
    public  QueueList(){
        this.front = null;
        this.rear = null;
    }

    public void enter(int i){
        Node newNode = new Node(i,null);
        //判断队列是否为空
        if(isEmpty()){
            this.front = newNode;
            this.rear = newNode;
        }else{
            this.rear.next = newNode;
            this.rear = newNode;
        }
    }

    public int leave(){
        if(isEmpty()){
            return -1;
        }
        int val = this.front.element;
        this.front = this.front.next;
        if(this.front == null){
            this.rear = null;
        }
        return val;
    }

    public int peek(){
        if(isEmpty()){
            return -1;
        }
        int val = this.front.element;
        return val;
    }

    public boolean isEmpty(){
        if(this.front == null){
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        QueueList queueList = new QueueList();
        queueList.enter(1);
        queueList.enter(2);
        queueList.enter(3);
        queueList.enter(4);
        System.out.println(queueList.peek());//1
        System.out.println(queueList.leave());//1
        System.out.println(queueList.leave());//2
        System.out.println(queueList.peek());//3
        System.out.println(queueList.isEmpty());//false
        System.out.println(queueList.leave());//3
        System.out.println(queueList.leave());//4
        System.out.println(queueList.isEmpty());//true
    }
}

java Deque使用
Java双端队列Deque使用详解
可以当作是队列以及堆栈使用

Queue方法等效的Deque方法说明
add(e)addLast(e)抛出异常
offer(e)offerLast(e)返回特殊值
remove()removeFirst()抛出异常
poll()pollFirst()返回特殊值
element()getFirst()抛出异常
peek()peekFirst()返回特殊值
Stack方法等效的Deque方法说明
push(e)addFirst(e)抛出异常
pop()removeFirst()抛出异常
peek()peekFirst()返回特殊值
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值