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());
  }
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值