Implémentation de l'ordre de pile / stockage en chaîne (version Java)

Définition de pile

Stack (stack), également connu sous le nom de stack, est une table linéaire avec des opérations limitées. Limitez la table linéaire qui insère et supprime uniquement à la fin de la table. Cette extrémité est appelée le haut de la pile, tandis que l'autre extrémité est appelée le bas de la pile. L'insertion d'un nouvel élément dans une pile s'appelle également pousser, pousser ou pousser. Il s'agit de placer le nouvel élément au-dessus de l'élément supérieur de la pile pour en faire un nouvel élément supérieur; supprimer un élément d'une pile s'appelle également faire une pile ou Unstack, c'est supprimer l'élément du haut de la pile, pour que l'élément adjacent devienne le nouvel élément du haut de la pile.

Implémentation du stockage séquentiel de la pile

package algorithm.datastructure.stack;

/*
* 栈
* 一种特殊的线性表—-操作受限的线性表,限于在表尾插入或删除元素的线性表
* 顺序栈
* 采用一段连续的空间存储(数组)
*
* */

public class SeqStack {
    
    
    private int []table;//数组
    private int top;//栈顶元素
    private int maxSize;//栈的容量
    private int size;//当前栈的元素个数
    private int incrementCapacity;//扩容时增长容量
    private static final int STACK_DEFAULT_INIT_SIZE=10;//初始容量
    private static final int STACK_DEFAULT_INCREMENT_SIZE=5;//扩容时默认增长容量

    //栈的初始化
    public SeqStack(){
    
    
        this.maxSize=STACK_DEFAULT_INIT_SIZE;
        this.table=new int[maxSize];
        this.top=-1;
        this.incrementCapacity=STACK_DEFAULT_INCREMENT_SIZE;
    }
    public SeqStack(int initialCapacity){
    
    
        this.maxSize=initialCapacity;
        this.table=new int[maxSize];
        this.incrementCapacity=STACK_DEFAULT_INCREMENT_SIZE;
        this.top=-1;

    }
    public SeqStack(int initialCapacity,int incrementCapacity){
    
    
        this.maxSize=initialCapacity;
        this.table=new int[maxSize];
        this.incrementCapacity=incrementCapacity;
        this.top=-1;
    }

    //判断栈是否为空
    public Boolean isEmpty(){
    
    
        return top==-1;
    }
    //判断栈是否满
    public Boolean isFull(){
    
    
        return (maxSize-1)==top;
    }

    //插入一个元素
    public void push(int x){
    
    
        if (isFull()){
    
    
            ensureCapacity();
            table[++top]=x;
            size++;
        } else {
    
    
            table[++top]=x;
            size++;
        }
    }
    //删除一个元素
    public Integer pop(){
    
    
        if (isEmpty()){
    
    
            try {
    
    
                throw new Exception("栈空");
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        } else {
    
    
            size--;
            return table[top--];
        }
        return null;
    }
    //得到栈顶元素
    public Integer getTop(){
    
    
        if (isEmpty()){
    
    
            try {
    
    
                throw new Exception("栈空");
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        } else {
    
    
            return table[top];
        }
        return null;
    }
    //获取当前栈中元素个数
    public int size(){
    
    
        return size;
    }

    //扩容
    private void ensureCapacity() {
    
    
        System.out.println("当前栈的容量为:"+maxSize+",容量不足,扩容");
        maxSize=maxSize+incrementCapacity;
        int []newTable=new int[maxSize];
        for (int i=0;i<table.length;i++){
    
    //将旧数组的元素移到新数组
            newTable[i]=table[i];
        }
        table=newTable;
        System.out.println("扩容成功,扩容后栈的新容量为:"+maxSize);
    }
    public static void main(String[] args) {
    
    
        //SeqStack seqStack=new SeqStack(10,2);
      //  SeqStack seqStack=new SeqStack();
        SeqStack seqStack=new SeqStack(15);
        seqStack.push(1);
        seqStack.push(2);
        seqStack.push(4);
        seqStack.push(4);
        seqStack.push(5);
        seqStack.push(1);
        seqStack.push(2);
        seqStack.push(4);
        seqStack.push(4);
        seqStack.push(5);
        seqStack.push(1);
        seqStack.push(2);
        seqStack.push(4);
        seqStack.push(4);
        seqStack.push(5);
        System.out.println(seqStack.pop());
        System.out.println(seqStack.pop());
        System.out.println(seqStack.pop());
        System.out.println(seqStack.pop());
        System.out.println(seqStack.pop());
        System.out.println(seqStack.pop());
        System.out.println(seqStack.getTop());
        System.out.println(seqStack.getTop());
        System.out.println(seqStack.getTop());
        System.out.println(seqStack.size());
    }
}

Mise en œuvre du stockage chaîné de la pile

package algorithm.datastructure.stack;
/*
* 栈
* 一种特殊的线性表—-操作受限的线性表,限于在表尾插入或删除元素的线性表
* 链栈
* 采用非连续的空间存储
* */
public class LinkedStack {
    
    

    private Node top;//栈顶指针
    private Node bottom;//栈底指针
    private Integer size;//栈的当前大小
    private static class Node{
    
    
        int data;
        Node next;
        public Node(){
    
    
        }
        public Node(int data){
    
    
            this.data=data;
            this.next=null;
        }
        public Node(int data,Node next){
    
    
            this.data=data;
            this.next=next;
        }
    }
    public LinkedStack(){
    
    
        top=new Node();
        bottom=new Node();
        top.next=bottom;
        size=0;
    }

    public Boolean isEmpty(){
    
    
       return top.next==bottom;
    }

    //插入元素
    public void push(int x){
    
    
        top= new Node(x,top);
        size++;
    }
    //删除元素
    public Integer pop(){
    
    
        if (isEmpty()){
    
    
            try {
    
    

                throw new Exception("栈空");
            }catch (Exception e){
    
    
                e.printStackTrace();
            }
        } else {
    
    
            Node x=top;
            top=x.next;
            size--;
            return x.data;
        }
        return null;
    }
    public Integer getTop(){
    
    
        if (isEmpty()){
    
    
            try {
    
    

                throw new Exception("栈空");
            }catch (Exception e){
    
    
                e.printStackTrace();
            }
        } else {
    
    
            return top.data;
        }

        return null;

    }
    //获取当前栈中元素个数
    public int size(){
    
    
        return size;
    }
    public static void main(String[] args) {
    
    

        //测试
        LinkedStack linkedStack=new LinkedStack();
        linkedStack.push(1);
        linkedStack.push(2);
        linkedStack.push(4);
        linkedStack.push(5);
        linkedStack.push(7);
        linkedStack.push(9);
        System.out.println(linkedStack.size());
        System.out.println(linkedStack.pop());
        System.out.println(linkedStack.pop());
        System.out.println(linkedStack.getTop());
        System.out.println(linkedStack.getTop());

       // System.out.println(linkedStack.pop());
    }
}

Je suppose que tu aimes

Origine blog.csdn.net/rj2017211811/article/details/109328728
conseillé
Classement