栈和队列的基本实现

栈和队列

栈和队列都是一种特殊的链表

  1. 栈是仅限定于在表尾进行插入和删除操作的线性表。
  2. 栈就相当于一个箱子,你往里面放书,最先放进去的书,我们取得时候就要把上面的书全部拿出来才可以将第一本放进去的书拿出来。
  3. 它遵循的是先进后出的规则
  4. 它的底层实现可以分为顺序表和单链表两种的实现方式。
  5. 它的两种实现方式插入和删除的基本操作时间复杂度都是O(1);

链表实现栈

package myStack;

class Node {
    public Integer val;
    public Node next;

    public Node(Integer val) {
        this.val = val;
    }
}

// 链式结构的栈
public class myStack {

    private Node head = null; //头节点
    private int size = 0;

    // 栈 先进后出

    // 压栈  O(1)
    public void push (int x) {
        if (this.head == null) {
            this.head = new Node(x);
            this.size++;
            return;
        }
        // 先进后出的话 我们要使用尾插法
        Node node = new Node(x);
        node.next = this.head;
        this.head = node;
        this.size++;
    }


    // 取栈顶元素 O(1)
    public Integer peek() {
        if (this.head == null) {
            return null;
        }
        return this.head.val;
    }

    //出栈 O(1)
    public Integer pop() {
        if (this.head == null) {
            return null;
        }
        Integer num = this.head.val;
        this.head = this.head.next;
        this.size--;
        return num;
    }


}

队列

  1. 队列就是只允许一端进行插入,一端进行删除操作的线性表
  2. 就像它的名字,我们排队,先进去的人先出来,它的表头是进行删除操作的,表尾是进行插入操作的
  3. 它遵循了先进后出的操作
  4. 它的底层实现可以分为顺序表(顺序表一般采用循环队列的方式)和单链表两种的实现方式。
  5. 它的两种实现方式插入和删除的基本操作时间复杂度都是O(1);

链表实现队列

package myQueue;

class Node {
    public Integer val;
    public Node next;

    public Node(Integer val) {
        this.val = val;
    }
}


// 链表法
public class myQueue {

    private Node head = null;
    private Node tail = null;
    // 设置了一个tail 来定义标识尾部  以便于入队
    private int size = 0;

    //先进先出的数据结构
    // 入队 O(1)
    public Integer offer(int x) {
        Node num = new Node(x);
        if (this.head == null) {
            this.head = num;
            this.tail = num;
            this.size++;
            return x;
        }
        //尾插
        this.tail.next = num;
        this.tail = num;
        this.size++;
        return x;
    }

    // 出队  0(1)
    public Integer poll() {
        if (this.head == null) {
            return -1;
        }
        Integer x = this.head.val;
        this.head = this.head.next;
        if (this.head == null) {
            this.tail = null;
        }
        this.size--;
        return x;
    }

    // 获取队首元素
    public Integer peek() {
        if (this.head == null) {
            //throw new RuntimeException("队列为空");
            return -1;
        }
        return head.val;
    }

}

顺序表实现循环队列

package 循环队列;

public class MyCircularQueue {

    public Integer[] queue;
    public int start;
    public int length;
    public int size;

    /** Initialize your data structure here. Set the size of the queue to be k. */
    public MyCircularQueue(int k) {
        //构造器,设置队列长度为 k
        this.queue = new Integer[k];
        this.length = k;
        this.start = 0;
        this.size = 0;
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        //向循环队列插入一个元素。如果成功插入则返回真。
        if ( size == this.length) {
            //证明队列为满
           return false;
        }
        this.queue[(this.start + this.size) % length] = value;
        this.size++;
        return true;
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        //从循环队列中删除一个元素。如果成功删除则返回真
        //删除元素就是出队列

        //为空
        if (this.size == 0) {
            return false;
        }
        // 头指针向前进一位
        this.start = (this.start + 1) % this.length;
        this.size--;
        return true;
    }

    /** Get the front item from the queue. */
    public int Front() {
        //从队首获取元素。如果队列为空,返回 -1
        if (this.size == 0) {
            return -1;
        }
        return this.queue[this.start];
    }

    /** Get the last item from the queue. */
    public int Rear() {
        //获取队尾元素。如果队列为空,返回 -1
        if (this.size == 0) {
            return -1;
        }
        int x = (this.start + this.size - 1) % this.length;
        return this.queue[x];
    }

    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        //检查循环队列是否为空
        if (this.size == 0) {
            return true;
        }
        return false;
    }

    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        //检查循环队列是否已满
        return (this.size == this.length);
    }

}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值