MyLinkedList(Day13)

package Day12;
import java.util.*;
public class MyLinkedList implements List {
 public MyLinkedList() {
  head = new Node(0);
 }
 public MyLinkedList(Collection c) {
  this();
  for (Object obj : c) {
   this.add(obj);
  }
 }
 public void add(int index, Object element) {
  if (index < 0 || index > ((Integer) (head.data))) {
   System.out.println("index不合法!");
   return;
  }
  Node node = new Node(element);
  Node temp = head.next;
  if (temp == null) {
   head.next = node;
   head.data = (Integer) (head.data) + 1;
   return;
  }
  int i = 0;
  while (i < index - 1) {
   temp = temp.next;
   i++;
  }
  node.next = temp.next;
  temp.next = node;
  int count = ((Integer) (head.data));
  count++;
  head.data = count;
 }
 public boolean add(Object e) {
  int count = ((Integer) (head.data));
  this.add(count, e);
  return true;
 }
 public boolean addAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }
 public boolean addAll(int arg0, Collection arg1) {
  // TODO Auto-generated method stub
  return false;
 }
 public void clear() {
  int count = 0;
  head.data = count;
  head.next = null;
  System.gc();//
 }
 public boolean contains(Object o) {
  Node temp = head;
  while (temp.next != null) {
   temp = temp.next;
   if (temp.data.equals(o)) {
    return true;
   }
  }
  return false;
 }
 public boolean containsAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }
 public Object get(int index) {
  int count = ((Integer) (head.data));
  if (index < 0 || index > count - 1) {
   System.out.println("index不合法");
   return null;
  }
  Node temp = head.next;
  int i = 0;
  while (i < index) {
   temp = temp.next;
   i++;
  }
  return temp.data;
 }
 public int indexOf(Object e) {
  int count = ((Integer) (head.data));
  Node temp = head.next;
  for (int i = 0; i < count; i++) {
   if (temp.data.equals(e)) {
    return i;
   }
   temp = temp.next;
  }
  return -1;
 }
 public boolean isEmpty() {
  int count = ((Integer) (head.data));
  return count == 0;
 }
 public Iterator iterator() {
  return new Iterator() {
   int count = ((Integer) (head.data));
   int i = 0;
   public boolean hasNext() {
    return i < count;
   }
   public Object next() {
    Node n = head.next;
    int temp = 0;
    while (temp < i) {
     n = n.next;
     temp++;
    }
    i++;
    return n.data;
   }
   public void remove() {
    // TODO Auto-generated method stub
   }
  };
 }
 public int lastIndexOf(Object arg0) {
  // TODO Auto-generated method stub
  return 0;
 }
 public ListIterator listIterator() {
  // TODO Auto-generated method stub
  return null;
 }
 public ListIterator listIterator(int arg0) {
  // TODO Auto-generated method stub
  return null;
 }
 public Object remove(int index) {
  int count = ((Integer) (head.data));
  if (index < 0 || index > count - 1) {
   System.out.println("index不合法");
   return null;
  }
  head.data = (Integer) head.data - 1;
  Node temp = head.next;
  Object obj = 0.0;
  
  if (index==0) {
   obj = temp.data;
   head.next=temp.next;
  } else {
   int i = 0;
   while (i < index-1 ) {
    temp = temp.next;
    i++;
   }
   obj=temp.next.data;
   temp.next=temp.next.next;
  }
  return obj;
 }
 public boolean remove(Object e) {
  int count = ((Integer) (head.data));
  int i = this.indexOf(e);
  if (i == -1) {
   System.out.println("连表为空或者连表中不存在参数 e");
   return false;
  }
  this.remove(i);
  return true;
 }
 public boolean removeAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }
 public boolean retainAll(Collection arg0) {
  // TODO Auto-generated method stub
  return false;
 }
 public Object set(int index, Object e) {
  int count = ((Integer) (head.data));
  if (index < 0 || index > count - 1) {
   System.out.println("index不合法");
   return null;
  }
  Node temp = head.next;
  int i = 0;
  while (i < index) {
   temp = temp.next;
   i++;
  }
  Object obj = temp.data;
  temp.data = e;
  return obj;
 }
 public int size() {
  int count = ((Integer) (head.data));
  return count;
 }
 public List subList(int arg0, int arg1) {
  // TODO Auto-generated method stub
  return null;
 }
 public Object[] toArray() {
  //
  return null;
 }
 public Object[] toArray(Object[] arg0) {
  // TODO Auto-generated method stub
  return null;
 }
 /**
  *头节点
  */
 public Node head;
 /**
  * 静态内部类,代表节点类型
  *
  * @author jsjx1
  *
  */
 private static class Node {
  public Object data;
  public Node next;
  public Node(Object obj) {
   this.data = obj;
  }
 }
}
////////////////////////////////////////////////
package Day12;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ListTest {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // List list=new MyArrayList(5);
  List list = new MyLinkedList();
  list.add("hello");
  list.add("haha");
  list.add("hehe");
  list.add(5);
  list.add(3.14);
  list.add("hehe");
  list.remove(3.14);
  System.out.println("contains[3.14]?" + list.contains(3.14));
  System.out.println("contains[haha]?" + list.contains("haha"));
  list.set(4, "world");
  System.out.println("get[2]:" + list.get(2));
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i));
  }
  System.out.println("size:" + list.size());
  System.out.println("======================");
  list.clear();
  System.out.println("size:" + list.size());
  Iterator it = list.iterator();
  while (it.hasNext()) {
   System.out.println(it.next());
  }
 }
}

你可能感兴趣的:(MyLinkedList(Day13))