Stack order/chain storage implementation (Java version)

Definition of stack

Stack (stack), also known as stack, is a linear table with limited operations. Limit the linear table that only inserts and deletes at the end of the table. This end is called the top of the stack, while the other end is called the bottom of the stack. Inserting a new element into a stack is also called pushing, pushing or pushing. It is to put the new element on top of the top element of the stack to make it a new top element; deleting an element from a stack is also called making a stack or Unstack, it is to delete the top element of the stack, so that the adjacent element becomes the new top element of the stack.

Sequential storage implementation of the stack

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());
    }
}

Chained storage implementation of the stack

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());
    }
}

Guess you like

Origin blog.csdn.net/rj2017211811/article/details/109328728