首页 > 其他 > 详细

第二篇 集合与容器(二)

时间:2018-08-20 23:47:02      阅读:279      评论:0      收藏:0      [点我收藏+]

第一版

package com.zzp.demo.myCollection;

public class Node {
    
    Node previous; //上一节点
    Node next; //下一节点
    Object element; //数据
    
    public Node(Object element) {
        super();
        this.element = element;
    }

    public Node(Node previous, Node next, Object element) {
        super();
        this.previous = previous;
        this.next = next;
        this.element = element;
    }
}
package com.zzp.demo.myCollection;
/**
 * 
 * 自定义链表
 * @author java
 *
 */
public class LinkedList01 {
    private Node first;
    private Node last;
    private int size;
    
    //添加元素
    public void add(Object obj){
        Node node = new Node(obj);
        if(first == null){
            first = node;
            last = node;
        }else{
            node.previous = last;
            node.next = null;
            last.next = node;
            last = node;
        }
    }
    
    //打印字符串
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        while(temp != null){
            sb.append(temp.element + ",");
            temp = temp.next;
        }
        sb.setCharAt(sb.length() - 1, ‘]‘);
        return sb.toString();
    }
    
    public static void main(String[] args) {
        LinkedList01 ls = new LinkedList01();
        ls.add("sa");
        ls.add("sdsf");
        ls.add("kajs");
        System.out.println(ls.toString());
    }
}

 

第二篇 集合与容器(二)

原文:https://www.cnblogs.com/zhangzhipeng001/p/9508840.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!