MyLinkedList(链表的实现)

来源:互联网 发布:类似萝莉云的软件 编辑:程序博客网 时间:2024/05/01 14:57
  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;   publicboolean 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.    
  134.   if (index==0) { 
  135.    obj = temp.data; 
  136.    head.next=temp.next; 
  137.   } else
  138.    int i = 0
  139.    while (i < index-1 ) { 
  140.     temp = temp.next; 
  141.     i++; 
  142.    } 
  143.    obj=temp.next.data; 
  144.    temp.next=temp.next.next; 
  145.   } 
  146.   return obj; 
  147. }
  148. public boolean remove(Object e) { 
  149.   int count = ((Integer) (head.data)); 
  150.   int i = this.indexOf(e); 
  151.   if (i == -1) { 
  152.    System.out.println("连表为空或者连表中不存在参数 e"); 
  153.    return false
  154.   } 
  155.   this.remove(i); 
  156.   return true
  157. }
  158. public boolean removeAll(Collection arg0) { 
  159.   // TODO Auto-generated method stub
  160.   return false
  161. }
  162. public boolean retainAll(Collection arg0) { 
  163.   // TODO Auto-generated method stub
  164.   return false
  165. }
  166. public Object set(int index, Object e) { 
  167.   int count = ((Integer) (head.data)); 
  168.   if (index < 0 || index > count - 1) { 
  169.    System.out.println("index不合法"); 
  170.    return null
  171.   } 
  172.   Node temp = head.next; 
  173.   int i = 0
  174.   while (i < index) { 
  175.    temp = temp.next; 
  176.    i++; 
  177.   } 
  178.   Object obj = temp.data; 
  179.   temp.data = e; 
  180.   return obj; 
  181. }
  182. public int size() { 
  183.   int count = ((Integer) (head.data)); 
  184.   return count; 
  185. }
  186. public List subList(int arg0,int arg1) { 
  187.   // TODO Auto-generated method stub
  188.   return null
  189. }
  190. public Object[] toArray() { 
  191.   //
  192.   return null
  193. }
  194. public Object[] toArray(Object[] arg0) { 
  195.   // TODO Auto-generated method stub
  196.   return null
  197. } /**
  198.   *头节点
  199.   */
  200. public Node head;/**
  201.   * 静态内部类,代表节点类型
  202.   * 
  203.   * @author jsjx1
  204.   * 
  205.   */
  206. private staticclass Node { 
  207.   public Object data; 
  208.   public Node next; public Node(Object obj) { 
  209.    this.data = obj; 
  210.   } 
  211. }} 
  212. ////////////////////////////////////////////////package Day12;import java.util.ArrayList;
  213. import java.util.Iterator; 
  214. import java.util.LinkedList; 
  215. import java.util.List;publicclass ListTest { /**
  216.   * @param args
  217.   */
  218. public staticvoid main(String[] args) { 
  219.   // List list=new MyArrayList(5);
  220.   List list = new MyLinkedList(); 
  221.   list.add("hello"); 
  222.   list.add("haha"); 
  223.   list.add("hehe"); 
  224.   list.add(5); 
  225.   list.add(3.14); 
  226.   list.add("hehe"); 
  227.   list.remove(3.14); 
  228.   System.out.println("contains[3.14]?" + list.contains(3.14)); 
  229.   System.out.println("contains[haha]?" + list.contains("haha")); 
  230.   list.set(4, "world"); 
  231.   System.out.println("get[2]:" + list.get(2)); for (int i =0; i < list.size(); i++) { 
  232.    System.out.println(list.get(i)); 
  233.   } 
  234.   System.out.println("size:" + list.size()); 
  235.   System.out.println("======================"); 
  236.   list.clear(); 
  237.   System.out.println("size:" + list.size());  Iterator it = list.iterator(); 
  238.   while (it.hasNext()) { 
  239.    System.out.println(it.next()); 
  240.   } }} 

 

原创粉丝点击