import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

public class MyLinkedList implements List{

private Node head;

private int count;

public MyLinkedList(){

head=new Node();

}

private static class Node{

private Object data;

private Node next;

public Node(Object obj){

this.data=obj;

}

public Node(){

}

public Object getData() {

return data;

}

public void setData(Object data) {

this.data = data;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

public void add(int index, Object element) {

// 1,判断index是否有效,如果无效则抛出异常。

//2,生成Node,数据域是element

//3,找到待插入的位置

if(index<0 || index >count){

throw new IndexOutOfBoundsException("无效的下标:"+index);

}

Node n=new Node(element);

Node p=head;

for(int i=0;i<index;i++){

p=p.next;

}

n.next=p.next;

p.next=n;

count++;

}

public boolean add(Object e) {

add(count,e);

return true;

}

public boolean addAll(Collection c) {

// TODO Auto-generated method stub

return false;

}

public boolean addAll(int index, Collection c) {

// TODO Auto-generated method stub

return false;

}

public void clear() {

count=0;

head.next=null;

}

public boolean contains(Object o) {

Node p=head.next;

while(p!=null){

if(p.data.equals(o)){

return true;

}

p=p.next;

}

return false;

}

public boolean containsAll(Collection c) {

// TODO Auto-generated method stub

return false;

}

public Object get(int index) {

if(index<0 || index >=count){

throw new IndexOutOfBoundsException("无效的下标:"+index);

}

Node p=head;

for(int i=0;i<index;i++){

p=p.next;

}

return p.next.data;

}

public int indexOf(Object o) {

Node p=head.next;

int index=0;

while(p!=null){

if(p.data.equals(o)){

return index;

}

p=p.next;

index++;

}

return -1;

}

public boolean isEmpty() {

// ?????????????????

return count==0 && head.next==null;

}

public Iterator iterator() {

return new Iterator(){

Node p=head.next;

public boolean hasNext() {

return p!=null;

}

public Object next() {

Node q=p;

p=p.next;

return q.data;

}

public void remove() {

// ???????????

}

};

}

public int lastIndexOf(Object o) {

Node p=head.next;

int index =-1;

int i=0;

while(p!=null){

if(p.data.equals(o)){

index=i;

}

p=p.next;

i++;

}

return index;

}

public ListIterator listIterator() {

// TODO Auto-generated method stub

return null;

}

public ListIterator listIterator(int index) {

// TODO Auto-generated method stub

return null;

}

public Object remove(int index) {

if(index<0 || index>=count){

throw new IndexOutOfBoundsException("无效的下标:"+index);

}

Node p=head;

for(int i=0;i<index;i++){

p=p.next;

}

Node q=p.next;

p.next=q.next;

q.next=null;

return q.data;

}

public boolean remove(Object o) {

int index=indexOf(o);

if(index>=0){

remove(index);

return true;

}

return false;

}

public boolean removeAll(Collection c) {

// TODO Auto-generated method stub

return false;

}

public boolean retainAll(Collection c) {

// TODO Auto-generated method stub

return false;

}

public Object set(int index, Object element) {

if(index<0 || index>=count){

throw new IndexOutOfBoundsException("无效的下标:"+index);

}

Node p=head.next;

for(int i=0;i<index;i++){

p=p.next;

}

Object temp=p.data;

p.data=element;

return temp;

}

public int size() {

return count;

}

public List subList(int fromIndex, int toIndex) {

// TODO Auto-generated method stub

return null;

}

public Object[] toArray() {

// TODO Auto-generated method stub

return null;

}

public Object[] toArray(Object[] a) {

// TODO Auto-generated method stub

return null;

}

}