Java实现栈(顺序栈,链栈)

这篇博客介绍了两种栈的实现方式:顺序栈和链栈,并分别提供了它们的Java实现代码。测试类展示了如何使用这两个栈进行元素的压栈、弹栈和查看栈顶元素的操作。

顺序栈:

package SeqStack;

public class Stack {
    private int top;
    private int base[];
    private int stackSize;

    public Stack()
    {
        stackSize = 100;
        base = new int[stackSize];
        top = -1;
    }

    public Stack(int n)
    {
        stackSize = n;
        base = new int[stackSize];
        top = -1;
    }

    public boolean isFull()
    {
        if (top==stackSize-1) return true;
        return false;
    }

    public boolean isEmpty()
    {
        if (top==-1) return true;
        return false;
    }

    public boolean pushStack(int e)
    {
        if (isFull()) return false;
        base[++top] = e;
        return true;

    }

    public boolean popStack()
    {
        if (isEmpty()) return false;
        --top;
        return true;
    }

    public int topStack()
    {
        if (isEmpty())
        {
            System.out.println("The Stack is empty");
            return 0;
        }
        return base[top];
    }

}

测试类:

package SeqStack;

public class TestStack {
    public static void main(String[] args)
    {
        Stack s = new Stack();
        s.pushStack(23);
        s.pushStack(12);
        s.pushStack(45);
        System.out.println(s.topStack());
        s.popStack();
        System.out.println(s.topStack());
        s.popStack();
        s.popStack();
        s.popStack();
    }
}

链栈:

package LinkStack;

public class Stack {

    private class StackNode
    {
        int data;
        StackNode next;

        public StackNode(int e)
        {
            data = e;
            next = null;
        }
    }

    private StackNode top;

    public Stack()
    {
        top = null;
    }

    public boolean isEmpty()
    {
        if (top==null) return true;
        return false;
    }

    public boolean pushStack(int e)
    {
        StackNode s = new StackNode(e);
        s.next = top;
        top = s;
        return true;
    }

    public boolean popStack()
    {
        if (isEmpty()) return false;
        StackNode p = top;
        top = top.next;
        p.next = null;
        return true;
    }

    public int topStack()
    {
        if (isEmpty())
        {
            System.out.println("The stack is empty");
            return 0;
        }

        return top.data;
    }


}

测试类:

package LinkStack;

import LinkStack.Stack;

public class TestStack {
    public static void main(String[] args)
    {
        Stack s = new Stack();
        s.pushStack(23);
        s.pushStack(12);
        s.pushStack(45);
        System.out.println(s.topStack());
        s.popStack();
        System.out.println(s.topStack());
        s.popStack();
        s.popStack();
        s.popStack();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值