当前位置:   article > 正文

java数据结构——链表实现栈_链表实现栈java

链表实现栈java

java数据结构——链表实现栈

package fwb.COllection;

/**
 * @author :yixing
 * @Have a nice day!
 * @date : 2022/5/6
 * @time :  16:39
 */

//栈的链表实现
class LLNode{
    private Object data; //存放数据
    private LLNode next; //指向下一个节点
    public LLNode(){}
    public LLNode(Object data){
        this.data =data;
    }
    public Object getData(){
        return data;
    }
    public void setData(Object data){
        this.data = data;
    }
    public LLNode getNext(){
        return next;
    }
    public void setNext(LLNode next){
        this.next = next;
    }

}
public class StackLL {
    LLNode headNode = null;
    public StackLL(){
        headNode = new LLNode(null);
    }
    //入栈
    public void push(Object data){
        if(headNode.getData() == null){
            headNode.setData(data);
        }
        else if(headNode == null){
            headNode = new LLNode(data);
        }
        else{

            LLNode newNode = new LLNode(data);
            newNode.setNext(headNode);  //头插
            headNode = newNode;

        }
    }
    public boolean isEmpty(){
        return headNode == null;
    }
    //出栈
    public Object pop(){
        Object data = null;
        if(isEmpty()){
            System.out.println("栈为空");
            return 0;
        }
        data = headNode.getData();
        headNode = headNode.getNext();
        return data;
    }
    //统计栈里元素的个数并显示
    public int getLength(){
        int count = 0;
        LLNode cur = headNode;
        if(isEmpty()||cur.getData()==null){
            count = 0;
        }
        else{
            while(cur != null){
                //System.out.println(count+"->"+cur.getData());
                count++;
                cur = cur.getNext();

            }
        }
        return count;
    }

    public static void main(String[] args) {
        StackLL stackll = new StackLL();
        stackll.push(1);
        stackll.push(2);
        stackll.push(3);
        System.out.println("个数为:"+stackll.getLength());

        Object obj = stackll.pop();
        System.out.println("弹出了元素:"+obj);
        System.out.println("弹出后个数为:"+stackll.getLength());

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/611903
推荐阅读
相关标签
  

闽ICP备14008679号