链表实现栈

本文介绍了使用链表实现栈的方法,包括节点定义、链表的添加、删除、获取节点及统计有效值等操作。同时,展示了如何进行入栈、出栈、检查栈空、获取栈的大小和打印栈的实现。通过示例代码演示了栈的完整功能,如初始化、填充、遍历和清空栈的过程。

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

链表实现栈

一、项目结构

在这里插入图片描述

二、链表

1、节点Node
public class Node {

    private int n;

    private Node next;


    public Node(int n) {
        this.n = n;
    }


    public int getN() {
        return n;
    }


    public void setN(int n) {
        this.n = n;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
2、链表
public class LinkedList {

    private Node bottom = null;


    /**
     * 添加元素
     * @param n
     */
    public void add(int n){

        if (bottom  == null){
            bottom = new Node(n);
            return;
        }

        Node temp = bottom;

        while (true){

            if (temp.getNext() == null){
                break;
            }

            temp = temp.getNext();

        }

        temp.setNext(new Node(n));
    }


    /**
     * 删除元素
     */
    public int delete(){

        if (bottom.getNext() == null){

            int value = bottom.getN();

            bottom = null;

            return value;
        }


        Node temp = bottom;

        while (true){

            if (temp.getNext().getNext() == null){
                break;
            }

            temp = temp.getNext();

        }

        int value = temp.getNext().getN();

        temp.setNext(null);

        return value;

    }


    /**
     * 获取第一个节点
     * @return
     */
    public Node getBottom() {
        return bottom;
    }


    /**
     * 链表有效值统计
     * @return
     */
    public int size(){

        Node temp = bottom;

        int size = 0;

        while (true){

            if (temp == null){
                break;
            }

            size++;

            temp = temp.getNext();

        }

        return size;
    }


    /**
     * 打印
     */
    public void print(){

        Node temp = bottom;

        int size = 0;

        while (true){

            if (temp == null){
                break;
            }

            System.out.println(temp.getN());

            temp = temp.getNext();

        }


    }

}

三、栈

public class LinkedListStack {


    private LinkedList linkedList;


    public LinkedListStack() {
        this.linkedList = new LinkedList();
    }


    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return linkedList.getBottom() == null;
    }


    /**
     * 入栈
     * @param n
     */
    public void push(int n){

      linkedList.add(n);

    }


    /**
     * 出栈
     * @return
     */
    public int pop(){

        if (isEmpty()){
            System.out.println("栈为空");
            return -1;
        }
        return linkedList.delete();
    }

    /**
     * 栈有效值
     * @return
     */
    public int size(){
        return linkedList.size();
    }


    /**
     * 打印栈
     */
    public void print(){

        linkedList.print();

    }

}

四、测试

1、测试类
public class Test {

    public static void main(String[] args) {

        LinkedListStack stack = new LinkedListStack();

        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }

        stack.print();

        System.out.println("----------");

        for (int i = 0; i < 10; i++) {
            System.out.println(stack.pop());
        }

        System.out.println("-----------");


        stack.pop();

    }

}
2、运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值