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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
interface <E> { public void add (E e); public int size(); public boolean isEmpty(); public Object [] toArray(); public E get(int index); public void set(int index,E data); public boolean contains(E data); public void remove(E e); public void clean(); }
class LinkImpl<E> implements <E> { private class Node { private E data ; private Node next; public Node (E data) { this.data =data; }
public void addNode(Node newNode) { if(this.next == null) { this.next =newNode; }else { this.next.addNode(newNode); } } public void toArrayNode() { LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data; if(this.next != null) { this.next.toArrayNode(); } } public E getNode(int index) { if(LinkImpl.this.foot++ == index) { return this.data; }else { return this.next.getNode(index); } } public void setNode(int index,E data) { if(LinkImpl.this.foot++ == index) { this.data=data; }else { this.next.setNode(index,data); } } public boolean containsNode(E data) { if(data.equals(this.data)) { return true; }else { if(this.next==null) { return false; }else{ return this.next.containsNode(data); } } } public void removeNode(Node previous,E data) { if(this.data.equals(data)) { previous.next =this.next; }else { if(this.next != null) { this.next.removeNode(this,data); } } } } private Node root; private int count; private int foot; private Object [] returnData; public void add(E e) { if(e == null) { return; } Node newNode = new Node(e); if(this.root == null) { this.root = newNode; }else { this.root.addNode(newNode); } this.count++; } public int size() { return this.count; } public boolean isEmpty() { return this.count==0; } public Object [] toArray() { if(this.isEmpty()) { return null; } this.foot=0; this.returnData=new Object[this.count]; this.root.toArrayNode(); return this.returnData; } public E get(int index) { if(index >= count) { return null; } this.foot=0; return this.root.getNode(index); } public void set(int index,E data) { if(index >= count) { return; } this.foot=0; this.root.setNode(index,data); } public boolean contains(E data) { if(data == null) { return false; } return this.root.containsNode(data); } public void remove(E data) { if(this.contains(data)) { if(this.root.data.equals(data)) { this.root=this.root.next; }else { this.root.next.removeNode(this.root, data); } this.count--; } } public void clean() { this.root=null; this.count=0; } } public class Demo {
public static void main(String[] args) { Ilink<String> all = new LinkImpl<String>(); System.out.println(all.size()); System.out.println(all.isEmpty()); System.out.println("--------------------------------------------"); all.add("hello"); all.add("hell1"); all.add("hell2"); all.set(2,"11111"); all.remove("hell1"); all.clean(); System.out.println(all.isEmpty()); System.out.println(all.size()); System.out.println("--------------------------------------------"); Object result[] = all.toArray(); if(result !=null) { for(Object obj : result) { System.out.println(obj); } } }
}
|
近期评论