码迷,mamicode.com
首页 > 其他好文 > 详细

单链表

时间:2019-04-24 00:35:01      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:list()   color   col   his   str   main   system   class   ati   

public class Element {
    public Object object;
    public Element next = null;

    public Element(Object object){
        this.object = object;
    }
}
public class SingleLinkList {
    private Element element;

    public void init(){
        element = new Element(null);
        element.next = null;
    }

    public void add(int data){
        Element node = new Element(data);
        Element tmp = element;
        while (tmp.next != null){
            tmp=tmp.next;
        }
        tmp.next = node;
    }

    public void display(){
        Element tmp = element.next;
        while (tmp != null){
            System.out.println("链表的值:" + tmp.object);
            tmp = tmp.next;
        }
    }
}
public class Demo {
    public static void main(String[] args){
        SingleLinkList sll = new SingleLinkList();
        sll.init();
        sll.add(1);
        sll.add(2);
        sll.add(3);
        sll.add(4);
        sll.add(5);
        sll.display();
    }
}

 

单链表

标签:list()   color   col   his   str   main   system   class   ati   

原文地址:https://www.cnblogs.com/mutong1228/p/10759752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!