图解栈和队列实现

栈和队列

栈的实现
  1. 顺序栈,数组实现

在这里插入图片描述

class stackOfArray{
    public int[] elem;
    public int usedsize;
    public stackOfArray() {
        this.elem = new int[5];  //初始设置为5
    }

    public void push(int value) {  //往栈中压入元素
     if(isFull()) {
      //栈满时也可以扩容 this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
         System.out.println("栈满,无法push");
         return;
     }
     this.elem[this.usedsize] = value;
     this.usedsize++;
    }

    public void pop() {  //获取栈顶元素,并删除
        if (isEmpty()) {
            System.out.println("栈空,不能pop");
            return;
        }
        System.out.println(this.elem[this.usedsize - 1]);
        this.usedsize--;
    }

    public void peek() {  //获取栈顶元素,不删除
        if (isEmpty()) {
            System.out.println("栈空,不能peek");
            return;
        }
        System.out.println(this.elem[this.usedsize - 1]);
    }

    public boolean isEmpty() {  //判空
        if(this.usedsize == 0) {
            return true;
        }
        return false;
    }

    public boolean isFull() {  //判满
        if (this.elem.length == this.usedsize) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "stackOfArray{" +
                "elem=" + Arrays.toString(elem) +
                '}';
    }
}
  1. 泛型实现顺序栈
    //泛型实现顺序栈,主要将类改为泛型类
    //上述顺序栈代码只需更改以下几处
    class stackOfArray<T>{  //添加泛型
        public T[] elem; //泛型
        public int usedsize;
        public stackOfArray() {
            this.elem = (T[]) new Object[5];
        }
    
        public void push(T value) {  //往栈中压入元素  //参数也为泛型
         if(isFull()) {
             this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
    //         System.out.println("栈满,无法push");
    //         return;
         }
         this.elem[this.usedsize] = value;
         this.usedsize++;
        }
    }
    
  2. 单链表实现栈

    单链表实现栈,使用头插、头删方法效率最高,时间复杂度为O(1).

在这里插入图片描述

class myNode{
    public int val;
    public myNode next;
    public myNode(int value) {
        this.val = value;
    }
}
class stackOfListNode{
    public myNode head;  //头节点

    public void push(int value) {  
        myNode node = new myNode(value);
    if (isEmpty()) {
        this.head = node;
    }else {
        node.next = this.head;  //头插
        this.head = node;
    }
    }

    public void pop() {
    if (isEmpty()) {
        System.out.println("栈空,无法pop");
    }else {
        System.out.println(this.head.val);  
        this.head = this.head.next; //头删
    }
    }

    public void peek() {
    if (isEmpty()) {
        System.out.println("栈空,无法peek");
    }else {
        System.out.println(this.head.val);
    }
    }

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

    public void display() {
        myNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
队列的实现
  1. 链表实现队列

在这里插入图片描述

class myQueueNode{
    public int val;
    public myQueueNode next;
    public myQueueNode(int value) {
        this.val = value;
    }
}
class queueOfListNode{
    //使用带尾节点的单链表
    public myQueueNode head;
    public myQueueNode tail;

    public void offer(int val) {  //入队
        myQueueNode node = new myQueueNode(val);
        if (isEmpty()) {
            this.head = node;
            this.tail = node;
        }else {
            this.tail.next = node;
            this.tail = node;
        }
    }

    public void poll() {  //出队,并删除
        if (isEmpty()) {
            System.out.println("队空,不能poll");
        }else {
            int temp = this.head.val;
            this.head = this.head.next;
            System.out.println(temp);
        }
    }

    public void peek() {  //出队,不删除
        if (isEmpty()) {
            System.out.println("队空,不能peek");
        }else {
            System.out.println(this.head.val);
        }
    }

    public boolean isEmpty() {  //判空
        if(this.head == null) {
            return true;
        }
        return false;
    }

    public void display() {
        myQueueNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
  1. 数组实现循环队列

在这里插入图片描述

class circularQueue{
    public int[] elem;
    public int front;
    public int rear;
    public circularQueue(int cap) {
        this.elem = new int[cap + 1];  //浪费一个空间,以便于判断队满队空
    }

    public void enQueue(int value) {
    if (isFull()) {
        System.out.println("队满,不能入队");
    }else {
        this.elem[this.rear] = value;
        this.rear = (this.rear + 1) % this.elem.length;
    }
    }

    public void deQueue() {
    if (isEmpty()) {
        System.out.println("队空,不能出队");
    }else {
        System.out.println(this.elem[this.front]);
        this.front = (this.front + 1) % this.elem.length;
    }
    }


    public void getRear() {
    if (isEmpty()) {
        System.out.println("队空,不能getRear");
    }else {
        int index = this.rear == 0 ? this.elem.length - 1 : this.rear - 1;
        System.out.println(this.elem[index]);
    }
    }

    public void getFront() {
        if (isEmpty()) {
            System.out.println("队空,不能getFront");
        }else {
            System.out.println(this.elem[this.front]);
        }
    }

    public boolean isEmpty() {  //rear == front时队空
    if (this.rear == this.front) {
        return true;
    }
    return false;
    }

    public boolean isFull() {  //当(rear + 1) % this.elem.length == front时队满
     if ((this.rear + 1) % this.elem.length == this.front) {
         return true;
     }
     return false;
    }

    @Override
    public String toString() {
        return "circularQueue{" +
                "elem=" + Arrays.toString(elem) +
                '}';
    }
}
  1. 数组双端循环队列

    类似于循环队列,区别在于,双端队列支持头部删除插入,尾部也可以删除插入。

    class double_queue {
        public int[] elem;
        public int front;
        public int rear;
        public double_queue(int cap) {
            this.elem = new int[cap + 1]; //浪费一个空间,以便于判断队满队空
        }
    
        public void addRear(int value) {  //队尾添加元素
            if (isFull()) {
                System.out.println("队满,不能addRear");
            }else {
                this.elem[this.rear] = value;
                this.rear = (this.rear + 1) % this.elem.length;
            }
        }
    
        public void addFront(int value) {  //队头添加元素
            if (isFull()) {
                System.out.println("队满,不能addFront");
            }else {
                this.front = this.front == 0 ? this.elem.length - 1 : this.front - 1;
                this.elem[this.front] = value;
            }
        }
    
        public void deleteRear() { //从队尾删除元素,并输出
            if (isEmpty()) {
                System.out.println("队空,无法deleteRear");
            }else {
                this.rear = this.rear == 0 ? this.elem.length - 1 : this.rear - 1;
                System.out.println(this.elem[this.rear]);
            }
        }
    
        public void deleteFront() {  //从队头删除元素,并输出删除了哪个元素
            if (isEmpty()) {
                System.out.println("队空,不能deleteFront");
            }else {
                int temp = this.elem[this.front];
                this.front = (this.front + 1 ) % this.elem.length;
                System.out.println(temp);
            }
        }
    
    
        public int getFront() {  //获取队头元素
            if (isEmpty()) {
                System.out.println("队空,不能getFront");
                return -1;
            }
            return this.elem[this.front];
        }
    
        public int getRear() {  //获取队尾元素
            if (isEmpty()) {
                System.out.println("队空,不能getRear");
                return -1;
            }
            int index = this.rear == 0 ? this.elem.length - 1 : this.rear - 1;
            return this.elem[index];
        }
    
    
        public boolean isEmpty() {  //判空
            if (this.front == this.rear) {
                return true;
            }
            return false;
        }
    
        public boolean isFull() {  //判满
            if ((this.rear + 1) % this.elem.length == this.front) {
                return true;
            }
            return false;
        }
    
        @Override
        public String toString() {
            return "double_queue{" +
                    "elem=" + Arrays.toString(elem) +
                    '}';
        }
    }
    
*队列实现栈

在这里插入图片描述

class stack_with_queue{
    public Queue<Integer> que1;
    public Queue<Integer> que2;
    public int usedsize;
    public stack_with_queue(){
        this.que1 = new LinkedList<>();
        this.que2 = new LinkedList<>();
    }

    public void push(int val) {  //队不为空就放,两个队都为空随机选一个放
        if (!this.que1.isEmpty()) {
            this.que1.offer(val);
        }else if(!this.que2.isEmpty()){
            this.que2.offer(val);
        }else {
            this.que1.offer(val);
        }
        this.usedsize++;
    }

    public void pop() {  //队不为空,把队里前size-1个移到空队中,然后poll最后一个
        if (isEmpty()) {
            System.out.println("队空,无法pop");
            return;
        }
        int i = this.usedsize - 1;
        if (!this.que1.isEmpty()) {
           while(i > 0) {
               this.que2.offer(this.que1.poll());
               i--;
           }
            this.usedsize--;
            System.out.println(this.que1.poll());

        }else if (!this.que2.isEmpty()) {
          while(i > 0) {
              this.que1.offer(this.que2.poll());
              i--;
          }
            this.usedsize--;
            System.out.println(this.que2.poll());
        }
    }

    public void peek() {  //队不为空,就把所有元素移到空的里面,temp标记的就是栈的顶端节点
    if (isEmpty()) {
        System.out.println("队空,无法peek");
        return;
    }
    int index = this.usedsize;
    int temp = -1;
    if(!this.que1.isEmpty()) {
        while(index > 0) {
            temp = this.que1.poll();
            this.que2.offer(temp);
            index--;
        }
    }else if(!this.que2.isEmpty()) {
        while(index > 0) {
            temp = this.que2.poll();
            this.que1.offer(temp);
            index--;
        }
    }
        System.out.println(temp);
    }

    public boolean isEmpty() {
        if (this.que2.isEmpty() && this.que1.isEmpty()) {
            return true;
        }
        return false;
    }
}
*栈实现队列

在这里插入图片描述

class queue_with_stack{
    Stack<Integer> s1;  //只入队
    Stack<Integer> s2;  //只出队
    public queue_with_stack() {
        this.s1 = new Stack<>();
        this.s2 = new Stack<>();
    }

    public void offer(int value) {
     this.s1.push(value);
    }

    public void poll() {  //全部移到s2,然后pop s2的顶端数据
    if (this.s2.isEmpty()) {
        while(!this.s1.isEmpty()) {
            this.s2.push(this.s1.pop());
        }
    }
    if (!this.s2.isEmpty()) {
         int temp = this.s2.pop();
        System.out.println(temp);
    }
    }

    public void peek() {  //同poll一样,全部移到s2,peek顶端数据
    if(this.s2.isEmpty()) {
        while(!this.s1.isEmpty()) {
            this.s2.push(this.s1.pop());
        }
    }
    if(!this.s2.isEmpty()) {
        int temp = this.s2.peek();
        System.out.println(temp);
    }
    }

    public boolean isEmpty() {
     if(this.s1.isEmpty() && this.s2.isEmpty()) {
         return true;
     }
     return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值