标签:char add etc temp 链表 ati pen obj ext
第一版:增加add方法
package cn.zxg.collection;
public class Node {
Node previous;//上个节点
Node next;//下个节点
Object element;//自己的数据
public Node(Node previous,Node next,Object element){
this.previous=previous;
this.next=next;
this.element=element;
}
public Node(Object element){
this.element=element;
}
}
package cn.zxg.collection;
/**
* 自定义一个链表,增加一个add方法
*/
public class SxtLinkList {
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;
}
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
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) {
SxtLinkList list=new SxtLinkList();
list.add("a");
list.add("b");
list.add("c");
System.out.println(list.toString());
}
}
第二版、添加get方法,传入索引返回对应的内容
java容器---------手工实现Linkedlist 链表
标签:char add etc temp 链表 ati pen obj ext
原文地址:https://www.cnblogs.com/zzzao/p/10924985.html