Java的栈实现(数组、链表两种方式)

数组实现

import java.util.NoSuchElementException;
/**
 * @author 许湘扬
 * @email  547139255@qq.com
 * @detail 数组栈(自动扩容)
 */
public class MyArrayStack<T>
{
    private static final int DEAFAULT_CAPACITY=2;
    private T[] theStack;
    private int theSize;
    public boolean isEmpty()
    {
        return theSize==0;
    }
    public void celar()
    {
        theSize=0;
        ensureCapacity(DEAFAULT_CAPACITY);
    }
    public int size()
    {
        return theSize;
    }
    public MyArrayStack()
    {
        theSize=0;
        ensureCapacity(DEAFAULT_CAPACITY);
    }
    private void ensureCapacity(int newCapacity)
    {
        if(newCapacity<theSize)
            return;
        T[] oldItems=theStack;
        theStack=(T[])new Object[newCapacity];
        for (int i = 0; i <theSize; i++) 
            theStack[i]=oldItems[i];
    }
    public void push(T x)
    {
        if (theSize==theStack.length) 
            ensureCapacity(theSize*2+1);
        theStack[theSize++]=x;
    }
    public T pop()
    {
        if (theSize==0)
            throw new NoSuchElementException();
        theSize--;
        return theStack[size()];
    }
    public T top()
    {
        if (theSize==0)
            throw new NoSuchElementException();
        return theStack[size()-1];
    }
    public static void main(String[] args) {
        MyArrayStack<Integer> stack=new MyArrayStack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.pop();
        stack.push(10000);
        while(!stack.isEmpty())
        {
            System.out.println(stack.pop());
        }
        System.out.println(stack.isEmpty());
    }
}
/**
 * @author 许湘扬
 * @email  547139255@qq.com
 * @detail 单向链表栈  
 */
public class MyLinkedStack<T>
{
    private int theSize;
    private Node<T> top;
    private void doClear()
    {
        top=null;
        theSize=0;
    }
    public MyLinkedStack() {
        doClear();
    }
    public int size()
    {
        return theSize;
    }
    public boolean isEmpty()
    {
        return top==null;
    }
    public void push(T x)
    {
        Node<T> newNode=new Node<T>(x, top);
        top=newNode;
        theSize++;
    }
    public T pop()
    {
        if (isEmpty())
            throw new IndexOutOfBoundsException("stack is empty");
        T topData=top.data;
        top=top.next;
        theSize--;
        return topData;
    }
    public T top()
    {
        if (isEmpty())
            throw new RuntimeException("stack is empty");
        return top.data;
    }
    //节点类,静态内部类
    private static class Node<T>
    {
        T data;
        Node<T> next;
        public Node(T data, Node<T> next) {
            super();
            this.data = data;
            this.next = next;
        }
    }
    public static void main(String[] args) 
    {
        MyLinkedStack<Integer> stack=new MyLinkedStack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.pop();
        stack.push(10000);
        while(!stack.isEmpty())
        {
            System.out.println(stack.pop());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值