栈和队列-上

Posted 保护眼睛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了栈和队列-上相关的知识,希望对你有一定的参考价值。

上篇👇
Java比较器之Comparable和Comparator

栈和队列基本概念

栈和队列都属于线性表,因为它们也都用于存储逻辑关系为 “一对一” 的数据。
使用栈结构存储数据,讲究“先进后出”,即最先进栈的数据,最后出栈;使用队列存储数据,讲究 “先进先出”,即最先进队列的数据,也最先出队列。
栈和队列都属于线性表,根据线性表分为顺序表和链表的特点,栈也可分为顺序栈和链表,队列也分为顺序队列和链队列

顺序表实现栈及其基本操作

分析

top始终代表下一个存放的元素的下标,每次存放完之后top++.
在这里插入图片描述
出栈是得到栈顶元素elem[top-1],让top–,再次入栈时原来的值就会被覆盖掉
在这里插入图片描述

源码

/**
 * user:ypc;
 * date:2021-05-02;
 * time: 15:04;
 */
public class MyStack {
    int [] elem ;
    int top;
    MyStack(){
        this.elem = new int[10];
        this.top = 0;
    }
    public void push(int val){
        this.elem[this.top] = val;
        this.top++;
    }
  public int  pop() throws UnsupportedOperationException{
        if(empty())throw new UnsupportedOperationException("栈为空");
       int val = this.elem[this.top-1];
       this.top--;
       return val;
    }
    public int peek(){
        return this.elem[this.top-1];
    }
    public int size(){
        return this.top;
    }
    public boolean empty(){
        return this.top == 0;
    }
}
class MyStackTest{
    public static void main(String[] args) {
        System.out.println("顺序表表实现栈:===================================");
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println("栈顶是:"+stack.peek());
        System.out.println("栈的长度是:"+stack.size());;
        stack.pop();
        stack.pop();
        System.out.println("出栈后栈顶是:"+stack.peek());
        System.out.println("出栈后栈的长度是:"+stack.size());;
        System.out.println("栈是否是空:"+stack.empty());
        stack.pop();
        stack.pop();
        stack.pop();
        System.out.println("栈是否是空:"+stack.empty());
    }
}

运行结果

在这里插入图片描述

链表实现栈及其基本操作

分析

原理和顺序表实现栈一样,入栈每次让入的元素的next指向head,要入的元素的节点变成head,出栈就是每次让head的值出,head变为head的next,head==null的时候不能出。为什么不用尾插法呢?尾插要找尾 ,时间复杂度为O(n)。
在这里插入图片描述
在这里插入图片描述

源码

/**
 * user:ypc;
 * date:2021-05-02;
 * time: 15:04;
 */
class stackNode{
    int val;
    stackNode next;
    stackNode(int val){
        this.val = val;
    }
}
class MyLinkedStack{
    stackNode head;
    int top;
    public void push(int val){
        stackNode node = new stackNode(val);
        if(empty()){
          this.head = node;
        } else{
             node.next = this.head;
             this.head = node;
        }
        this.top++;
    }
    public int  pop()throws UnsupportedOperationException{
     if(empty()){
         throw new UnsupportedOperationException("栈为空");
     }
     int val = this.head.val;
     this.head = this.head.next;
     this.top--;
     return val;
    }
    public int peek(){
    return this.head.val;
    }
    public int size(){
        return this.top;
    }
    public boolean empty(){
        return this.head == null;
    }
}
class MyStackTest{
public static void main(String[] args) {
        System.out.println("链表实现栈:");
        MyLinkedStack myLinkedStack = new MyLinkedStack();
        myLinkedStack.push(1);
        myLinkedStack.push(2);
        myLinkedStack.push(3);
        myLinkedStack.push(4);
        myLinkedStack.push(5);
        System.out.println("栈顶是:"+myLinkedStack.peek());
        System.out.println("栈的长度是:"+myLinkedStack.size());;
        myLinkedStack.pop();
        myLinkedStack.pop();
        System.out.println("出栈后栈顶是:"+myLinkedStack.peek());
        System.out.println("出栈后栈的长度是:"+myLinkedStack.size());;
        System.out.println("栈是否是空:"+myLinkedStack.empty());
        myLinkedStack.pop();
        myLinkedStack.pop();
        myLinkedStack.pop();
        System.out.println("栈是否是空:"+myLinkedStack.empty());
        }
  }

运行结果

在这里插入图片描述
与顺序表实现栈对比
在这里插入图片描述

链表实现队列及其基本操作

分析

定义一个头front和一个尾rear,入队总是从尾入,出队从头出
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

源码

class Node{
    int val;
    Node next;
    Node(int val){
        this.val = val;
    }
}
public class MyQueue {
    public Node front;
    public Node rear;
    public int size;
    public void offer(int val){
        Node node = new Node(val);
        if(isEmpty()){
            this.front = node;
            this.rear = node;
        }else{
            this.rear.next = node;
            this.rear = node;
        }
        this.size++;
 }
    public int poll() throws UnsupportedOperationException{
        if(isEmpty()){
            throw new UnsupportedOperationException("队列为空!");
        }
        int val = this.front.val;
        this.front = this.front.next;
        this.size--;
        return val;
    }
    public int peek(){
        return this.front.val;
    }
    public int size(){
        return this.size;
    }
    public boolean isEmpty(){
        return front == null;
    }
}
class MyQueueTest{
    public static void main(String[] args) {
    System.out.println("=========================================");
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        myQueue.offer(4);
        System.out.println("队列是否为空:");
        System.out.println(myQueue.isEmpty());
        System.out.println("队列的peek是:");
        System.out.println(myQueue.peek());
        System.out.println("队列的长度是");
        System.out.println(myQueue.size());
        myQueue.poll();
        System.out.println("队列的peek是:");
        System.out.println(myQueue.peek());
        myQueue.poll();
        myQueue.poll();
        myQueue.poll();
        System.out.println("队列的长度是");
        System.out.println(myQueue.size());
        System.out.println("队列是否为空:");
        System.out.println(myQueue.isEmpty());
    }
}

运行结果

在这里插入图片描述

顺序表实现队列及其基本操作

分析

入队从队尾入,出队从头出,队满就是tail = capacity,队空就是head = tail = 0;入队一次,tail++;tail始终代表下一个入队元素的下标;出队就让head后移一位即head++。
入队:
在这里插入图片描述
出队:
在这里插入图片描述

源码

/**
 * user:ypc;
 * date:2021-05-02;
 * time: 15:05;
 */
class MyOrderQueue{
    public  int[] values;
    public int capacity = 0;
    public int head = 0;
    public int tail = 0;
    public MyOrderQueue(int capacity) {
        this.values = new int [capacity];
        this.capacity = capacity;
    }
    public Boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        this.values[this.tail] = value;
        this.tail++;
        return true;
    }
    public int deQueue() throws UnsupportedOperationException{
        if (empty()) throw  new UnsupportedOperationException("队列为空");
        int result = this.values[this.head];
        this.head++;
        return result;
    }
    public int getHead() throws UnsupportedOperationException{
        if (empty()) throw  new UnsupportedOperationException("队列为空");
        return this.values[this.head];
    }
    public int getTail() throws UnsupportedOperationException{
        if (empty()) throw  new UnsupportedOperationException("队列为空");
        return this.values[this.tail];
    }
    public boolean isFull(){
        return this.tail == this.capacity;
    }
    public boolean empty(){
        return this.head == this.tail;
    }
}
class MyQueueTest{
    public static void main(String[] args) {
        System.out.println("顺序队列:");
        MyOrderQueue myOrderQueue = new MyOrderQueue(5);
        myOrderQueue.enQueue(1);
        myOrderQueue.enQueue(2);
        myOrderQueue.enQueue(3);
        myOrderQueue.enQueue(4);
        System.out.println("顺序队列是否满了");
        System.out.println(myOrderQueue.isFull());
        myOrderQueue.enQueue(5);
        System.out.println("顺序队列是否满了");
        System.out.println(myOrderQueue.isFull());
        System.out.println("顺序队列是否为空:");
        System.out.println(myOrderQueue.empty());
        System.out.println("顺序队列的peek是:");
        System.out.println(myOrderQueue.getHead());
        myOrderQueue.deQueue();
        System.out.println("顺序队列的peek是:");
        System.out.println(myOrderQueue.getHead());
        myOrderQueue.deQueue();
        myOrderQueue.deQueue();
       // myOrderQueue.deQueue();
        System.out.println("顺序队列是否为空:");
        System.out.println(myOrderQueue.empty());
       System.out.println("=====================================");
        }
 }

以上是关于栈和队列-上的主要内容,如果未能解决你的问题,请参考以下文章

片段回栈和替换

栈和队列

栈和队列

数据结构栈和队列OJ练习(栈和队列相互实现+循环队列实现)

Java栈和队列·下

数据结构复习--栈和队列--栈