`
bcyy
  • 浏览: 1832211 次
文章分类
社区版块
存档分类
最新评论

MyLinkedList(链表的实现)

 
阅读更多
  1. package Day12;
  2. import java.util.*;
  3. public class MyLinkedList implements List {
  4. public MyLinkedList() {
  5. head = new Node(0);
  6. }
  7. public MyLinkedList(Collection c) {
  8. this();
  9. for (Object obj : c) {
  10. this.add(obj);
  11. }
  12. }
  13. public void add(int index, Object element) {
  14. if (index < 0 || index > ((Integer) (head.data))) {
  15. System.out.println("index不合法!");
  16. return;
  17. }
  18. Node node = new Node(element);
  19. Node temp = head.next;
  20. if (temp == null) {
  21. head.next = node;
  22. head.data = (Integer) (head.data) + 1;
  23. return;
  24. }
  25. int i = 0;
  26. while (i < index - 1) {
  27. temp = temp.next;
  28. i++;
  29. }
  30. node.next = temp.next;
  31. temp.next = node;
  32. int count = ((Integer) (head.data));
  33. count++; head.data = count;
  34. }
  35. public boolean add(Object e) {
  36. int count = ((Integer) (head.data));
  37. this.add(count, e);
  38. return true;
  39. }
  40. public boolean addAll(Collection arg0) {
  41. // TODO Auto-generated method stub
  42. return false;
  43. }
  44. public boolean addAll(int arg0, Collection arg1) {
  45. // TODO Auto-generated method stub
  46. return false;
  47. }
  48. public void clear() {
  49. int count = 0;
  50. head.data = count;
  51. head.next = null;
  52. System.gc();//
  53. }
  54. public boolean contains(Object o) {
  55. Node temp = head;
  56. while (temp.next != null) {
  57. temp = temp.next;
  58. if (temp.data.equals(o)) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. public boolean containsAll(Collection arg0) {
  65. // TODO Auto-generated method stub
  66. return false;
  67. }
  68. public Object get(int index) {
  69. int count = ((Integer) (head.data));
  70. if (index < 0 || index > count - 1) {
  71. System.out.println("index不合法");
  72. return null;
  73. }
  74. Node temp = head.next;
  75. int i = 0;
  76. while (i < index) {
  77. temp = temp.next;
  78. i++;
  79. } return temp.data;
  80. }
  81. public int indexOf(Object e) {
  82. int count = ((Integer) (head.data));
  83. Node temp = head.next;
  84. for (int i = 0; i < count; i++) {
  85. if (temp.data.equals(e)) {
  86. return i;
  87. }
  88. temp = temp.next;
  89. }
  90. return -1;
  91. }
  92. public boolean isEmpty() {
  93. int count = ((Integer) (head.data));
  94. return count == 0;
  95. }
  96. public Iterator iterator() { return new Iterator() {
  97. int count = ((Integer) (head.data));
  98. int i = 0; public boolean hasNext() {
  99. return i < count;
  100. } public Object next() {
  101. Node n = head.next;
  102. int temp = 0;
  103. while (temp < i) {
  104. n = n.next;
  105. temp++;
  106. }
  107. i++;
  108. return n.data;
  109. } public void remove() {
  110. // TODO Auto-generated method stub } };
  111. }
  112. public int lastIndexOf(Object arg0) {
  113. // TODO Auto-generated method stub
  114. return 0;
  115. }
  116. public ListIterator listIterator() {
  117. // TODO Auto-generated method stub
  118. return null;
  119. }
  120. public ListIterator listIterator(int arg0) {
  121. // TODO Auto-generated method stub
  122. return null;
  123. }
  124. public Object remove(int index) {
  125. int count = ((Integer) (head.data));
  126. if (index < 0 || index > count - 1) {
  127. System.out.println("index不合法");
  128. return null;
  129. }
  130. head.data = (Integer) head.data - 1;
  131. Node temp = head.next;
  132. Object obj = 0.0;
  133. if (index==0) {
  134. obj = temp.data;
  135. head.next=temp.next;
  136. } else {
  137. int i = 0;
  138. while (i < index-1 ) {
  139. temp = temp.next;
  140. i++;
  141. }
  142. obj=temp.next.data;
  143. temp.next=temp.next.next;
  144. }
  145. return obj;
  146. }
  147. public boolean remove(Object e) {
  148. int count = ((Integer) (head.data));
  149. int i = this.indexOf(e);
  150. if (i == -1) {
  151. System.out.println("连表为空或者连表中不存在参数 e");
  152. return false;
  153. }
  154. this.remove(i);
  155. return true;
  156. }
  157. public boolean removeAll(Collection arg0) {
  158. // TODO Auto-generated method stub
  159. return false;
  160. }
  161. public boolean retainAll(Collection arg0) {
  162. // TODO Auto-generated method stub
  163. return false;
  164. }
  165. public Object set(int index, Object e) {
  166. int count = ((Integer) (head.data));
  167. if (index < 0 || index > count - 1) {
  168. System.out.println("index不合法");
  169. return null;
  170. }
  171. Node temp = head.next;
  172. int i = 0;
  173. while (i < index) {
  174. temp = temp.next;
  175. i++;
  176. }
  177. Object obj = temp.data;
  178. temp.data = e;
  179. return obj;
  180. }
  181. public int size() {
  182. int count = ((Integer) (head.data));
  183. return count;
  184. }
  185. public List subList(int arg0, int arg1) {
  186. // TODO Auto-generated method stub
  187. return null;
  188. }
  189. public Object[] toArray() {
  190. //
  191. return null;
  192. }
  193. public Object[] toArray(Object[] arg0) {
  194. // TODO Auto-generated method stub
  195. return null;
  196. } /**
  197. *头节点
  198. */
  199. public Node head; /**
  200. * 静态内部类,代表节点类型
  201. *
  202. * @author jsjx1
  203. *
  204. */
  205. private static class Node {
  206. public Object data;
  207. public Node next; public Node(Object obj) {
  208. this.data = obj;
  209. }
  210. }}
  211. ////////////////////////////////////////////////package Day12;import java.util.ArrayList;
  212. import java.util.Iterator;
  213. import java.util.LinkedList;
  214. import java.util.List;public class ListTest { /**
  215. * @param args
  216. */
  217. public static void main(String[] args) {
  218. // List list=new MyArrayList(5);
  219. List list = new MyLinkedList();
  220. list.add("hello");
  221. list.add("haha");
  222. list.add("hehe");
  223. list.add(5);
  224. list.add(3.14);
  225. list.add("hehe");
  226. list.remove(3.14);
  227. System.out.println("contains[3.14]?" + list.contains(3.14));
  228. System.out.println("contains[haha]?" + list.contains("haha"));
  229. list.set(4, "world");
  230. System.out.println("get[2]:" + list.get(2)); for (int i = 0; i < list.size(); i++) {
  231. System.out.println(list.get(i));
  232. }
  233. System.out.println("size:" + list.size());
  234. System.out.println("======================");
  235. list.clear();
  236. System.out.println("size:" + list.size()); Iterator it = list.iterator();
  237. while (it.hasNext()) {
  238. System.out.println(it.next());
  239. } }}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics