Java代码实现栈(数组和链表)

数组实现栈

import org.apache.poi.ss.formula.functions.T;

/**
 * @Author: jerrold
 * @Date: 2021/08/02
 * @Version 1.0
 * @description: 数组实现栈
 */
public class MyStack {
    
    

    private int cnt; //当前栈的长度
    private int stackSize; //栈的总长度
    private T[] arr; //数组存储

    public MyStack(int capacity){
    
    
        this.cnt = 0;
        this.stackSize = capacity;
        this.arr = (T[]) new Object[capacity];
    }

    /*是否为空*/
    public boolean isEmpty(){
    
    
        return this.cnt == 0;
    }

    /*返回栈顶值*/
    public T peak() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        return arr[cnt-1];
    }

    /*返回栈顶元素并删除*/
    public T pop() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        T temp = arr[cnt-1];//临时存储
        this.cnt--;//当前栈长度减1
        return temp;
    }

    /*插入新元素*/
    public void push(T value) throws Exception {
    
    
        if (stackSize == cnt){
    
     //栈已满
            throw new Exception("ERROR");
        }
        arr[cnt] = value; //入栈
        this.cnt++;//当前栈长度加1
    }

    /*返回栈长度*/
    public int getLength(){
    
    
        return this.cnt;
    }

    /*清空栈*/
    public void clearStack(){
    
    
        //遍历赋值为null
        for (int i = 0; i < cnt; i++) {
    
    
            arr[cnt] = null;
        }
        this.cnt = 0;//长度置为0
    }

}

链表实现栈

链表实现push和pop其实是对表头进行操作,也就是所谓的头插法。

import org.apache.poi.ss.formula.functions.T;

/**
 * @Author: jerrold
 * @Date: 2021/08/02
 * @Version 1.0
 * @description: 链表实现栈
 */
public class MyStack {
    
    

    private int cnt;//当前栈长度
    private Node head;//头结点

    //定义结点类
    private class Node{
    
    
        private T value;
        private Node next;
        public Node(T t, Node next){
    
    
            this.value = t;
            this.next = next;
        }
        public Node(T t){
    
    
            this(t,null);
        }
    }

    public MyStack(){
    
    
        this.cnt = 0;
        this.head = null;
    }


    /*是否为空*/
    public boolean isEmpty(){
    
    
        return this.cnt == 0;
    }

    /*返回栈顶值*/
    public T peak() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        return head.value;
    }

    /*返回栈顶元素并删除*/
    public T pop() throws Exception {
    
    
        if(isEmpty()){
    
    
            throw new Exception("ERROR");
            //当然也可以直接返回null;
        }
        T res = head.value; //临时存储头结点的值
        head = head.next;//删除当前结点
        this.cnt--;//栈长度减1
        return res;
    }

    /*插入新元素*/
    public void push(T value) throws Exception {
    
    
        Node curr = new Node(value); //创建要插入的结点
        curr.next = this.head;//当前结点指向头结点
        this.head = curr; //头结点设为当前结点
        this.cnt++;//栈长度加1
    }

    /*返回栈长度*/
    public int getLength(){
    
    
        return this.cnt;
    }

    /*清空栈*/
    public void clearStack(){
    
    
        this.head = null;//头结点置为null即可 当然也可以将全部结点置为null 加快GC回收
        this.cnt = 0;//长度置为0
	}
	
}

猜你喜欢

转载自blog.csdn.net/MAKEJAVAMAN/article/details/119334737