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

栈是一种特殊的线性表,只允许在固定的一端进行插入和删除操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守LIFO(last in first out)的原则。
压栈:栈的插入操作叫做进栈,插入的数据在栈顶
出栈:栈的删除操作叫做出栈,删除的数据也在栈顶。

栈的实现

栈的实现可以使用数组或链表实现,数组的实现更优一些。

interface IMyStack {
    /** 
    * @Description: 判断栈是否为空 
    * @Param:  
    * @return:  
    */
    boolean empty();

    /** 
    * @Description: 返回栈顶元素但是不出栈 
    * @Param:  
    * @return:  
    */
    int peek();

    /** 
    * @Description: 栈顶元素出栈 
    * @Param:  
    * @return:  
    */
    int pop();

    /** 
    * @Description: 将item压入栈 
    * @Param:  
    * @return:  
    */
    void push(int item);

    /** 
    * @Description: 返回栈中元素个数 
    * @Param:  
    * @return:  
    */
    int size();
}


public class MyStack implements IMyStack {
    private int top = 0;
    private int[] array = new int[100];


    @Override
    public boolean empty() {
        if (top == 0) {
            return true;
        }
        return false;
    }

    @Override
    public int peek() {
        if (this.top != 0) {
            int val = array[this.top - 1];
            return val;
        }
        return 0;
    }

    @Override
    public int pop() {
        if (this.top != 0) {
            return array[--this.top];
        }
        return 0;
    }

    @Override
    public void push(int item) {
        array[this.top] = item;
        this.top++;
    }

    @Override
    public int size() {
        return this.top;
    }
}

栈的应用

  • 括号匹配
  • 中缀表达式求值(逆波兰表达式)
  • 暴力走迷宫
  • 深度优先遍历

队列

队列:只允许在一段进行插入数据操作,在另一端进行数据删除操作的特殊线性表。队列具有先进先出FIFO(first in first out)原则。进行数据插入的一端称为队尾,进行数据删除操作的一端称为队头。
队列的实现采用链表

队列的实现

interface IMyQueue {
    boolean empty();
    int peek();
    int poll();
    void add(int item);
    int size();
}

public class MyQueue implements IMyQueue {
    static class Node {
        private int value;
        private Node next;

        public Node(int value) {
            this.value = value;
            this.next = null;
        }
    }

    Node head;
    Node tail;

    public MyQueue() {
        this.head = null;
        this.tail = null;
    }

    @Override
    public boolean empty() {
        if (this.head == this.tail) {
            if (this.head == null) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description: 取队首元素,但是不出队
     * @Param:
     * @return:
     */
    @Override
    public int peek() {
        if (this.head != null) {
            return this.head.value;
        }
        return 0;
    }

    /**
     * @Description: 返回队首元素并出队
     * @Param:
     * @return:
     */
    @Override
    public int poll() {
        if (this.head != null) {
            int res = this.head.value;
            this.head = head.next;
            return res;

        }
        return 0;
    }

    @Override
    public void add(int item) {
    if(this.tail==null){
    this.tail=new Node(item);
    if(this.head==null){
        this.head=this.tail;
    }
}else{
        Node node=new Node(item);
    this.tail.next=node;
    this.tail=node;
}
    }

    @Override
    public int size() {
        Node cur=head;
        int size=0;
        while(cur!=null){
            cur=cur.next;
            size++;
        }
        return size;
    }
}

队列的应用

1.实现公平
2.广度优先遍历

猜你喜欢

转载自blog.csdn.net/weixin_42962924/article/details/89421683