栈和队列-简单实现

1.栈

栈:是一种特殊的线性表,只允许在固定的一端插入和删除,一端称为栈底,一端称为栈顶。栈遵循先进后出的原则。
栈的相关操作:压栈,向栈顶插入一个元素
出栈,在栈顶处删除一个元素
取栈顶元素,获取到在最后一个在栈顶元素的结果。
栈的实现:
1.基于顺序表:
使用尾插,尾删来分别表示入栈和出栈。根据下标获取元素表示取栈顶元素
实现代码:

public class MyStack {
    private int[] arr=new int[100];
    private int size=0;
    //入栈 不考虑扩容
    public void push(int val){
        arr[size]=val;
        size++;
    }
    //出栈
    public Integer pop(){
        if(size==0){
            return null;
        }
        int ret=arr[size-1];
        size--;
        return ret;
    }
    //取栈顶元素
    public Integer peek(){
        if(size==0){
            return  null;
        }
        int ret=arr[size-1];
        return  ret;
    }
}

2.基于链表
使用头插,头删来表示入栈和出栈。直接取头结点就是取栈顶元素。
实现代码

class Node{
    int val;
    Node next;
    public Node(int val){
        this.val=val;
    }
}
public class MyLinkedStack {
    private Node head=null;

    //入栈
    public void push(int val){
        Node newNode=new Node(val);
        //栈为空
        if(head==null){
            head=newNode;
            return;
        }
        //栈不为空
        newNode.next=head;
        head=newNode;
    }
    //出栈
    public Integer pop(){
        if(head==null){
            return null;
        }
        if(head.next==null){
            int ret=head.val;
            head=null;
            return ret;
        }
        int ret=head.val;
        head=head.next;
        return ret;
    }
    //取栈顶元素
    public Integer peek(){
        if(head==null){
            return null;
        }
        return head.val;
    }
}

2.队列

队列:只允许在一端进行插入,在另一端进行删除操作的特殊线性表。队列遵循先进先出的原则。
队列的操作:入队列:在队尾进行插入操作
出队列:在队头进行删除操作
队列的实现:
通过链表:
使用尾插表示入队列,使用头删表示出队列。直接获取到头结点,就是取队头元素。
实现代码:

class Node{
    int val;
    Node next;
    public Node(int val){
        this.val=val;
    }
}
public class MyQueue {
    Node head=null;
    Node tail=null;
    //入队列
    public boolean offer(int val){
        Node newNode=new Node(val);
        //队列为空
        if(head==null){
            head=newNode;
            tail=newNode;
            return true;
        }
        //不为空
        tail.next=newNode;
        tail=tail.next;
        return true;
    }
    //出队列
    public Integer poll() {
        if (head == null) {
            return null;
        }
        int ret = head.val;
        if (head.next == null) {
            head=null;
            return ret;
        }
        head=head.next;
        return ret;
    }
    //取队头元素
    public Integer peek(){
        if(head==null){
            return null;
        }
        return head.val;
    }
}

基于顺序表实现循环队列:

队列的有效元素区间:[head,tail)
循环队列和普通队列的区别,只是队头队尾可以重新从0开始往后移动。相比于链表版的队列,操作速度更快,但空间是固定大小的,扩容成本高。
入队列操作:把新的元素放到tail对应的下标上,同时tail++
出队列操作:head++ ,相当于把原来head指向的元素排除到有效区间外。
缺陷:队列为空时,tail和head是重合的。队列为满时,tail和head也是重合的,所以无法正确判断当前的队列是空还是满。
这个问题的解决方案:1.留出一个元素大小的空间,这样当队列为满时,tail和head不会重合,可以判断为满。tail==head-1即队列为满。
2.设置size属性,通过操作size来判断队列是否空或满。

public class MyQueue2 {
    private int data[]=new int[100];
    private int head=0;
    private int tail=0;
    private int size=0;
    //入队列 不考虑扩容
    public boolean offer(int val){
        if(size==data.length){
            return false;
        }
        data[tail]=val;
        tail++;
        if(tail==data.length){
            tail=0;
        }
        size++;
        return true;
    }
    //出队列
    public Integer poll(){
        if(size==0){
            return null;
        }
        int ret=data[head];
        head++;
        if(head==data.length){
            head=0;
        }
        size--;
        return ret;
    }
    //取队头元素
    public Integer peek(){
        if(size==0){
            return null;
        }
        return data[head];
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值