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

本文介绍了两种常见的栈实现方法:一种是使用数组实现,另一种是利用链表实现。数组实现的栈通过固定大小的数组来存储元素,并使用一个变量记录当前栈的长度;链表实现的栈则采用链表作为底层数据结构,通过头插法完成元素的入栈和出栈操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组实现栈

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
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值