java linked list

public class LList implements LinearList {

private ListNode header;

private int count;

public LList() {header = null; count = 0;}

public void addHead(T item){}//把項目插入到這張線性表的開始

public void addTail(T item){}//把項目插入到這張線性表的末端

public void add(T item,int index){}//作為這個線性表的索引個元素插入項目

public boolean contain(T item){}//返回true如果項目存在這種線性列表;假的,否則

public T delHead(){}//刪除這張線性表的第一條並且 返回被刪除的項目 如果這張線性表是空的,返回空行

public T delTail(){}//刪除這張線性表的末項和 返回被刪除的項目 如果這張線性表是空的,返回空行

public T del(int index){}//刪除這張線性表的索引次項目並且返回被刪除的項目

//如果這張線性表是空的或者索引缺乏是範圍,返回空行

public boolean equals(LinearList list){}//返回true如果在這所有的元素線性表是等於那些在名單中的條款

//每個元素的內容和排序;假的,否則

public boolean equalLength( LinearList list ){}//返回true如果該列表的長度和

//這種線性表的長度是平等的,假的,否則

public T get(int index){}//返回指數個元素,如果索引不大於這個線性表的長度;否則,NULL

public int indexOf(T item){}//在這張線性表裡返回項目的索引 如果項目不在這張線性表裡,返回0

public boolean isEmpty(){return header.next == null;}//返回true如果這種線性是空的,假的,否則

public boolean set(T item,int index){}//更換項目指數個元素

//返回true如果索引是合法的,假的,否則

public int size(){return count;}//回來,要素的數量儲存在裡 這張線性表

private class ListNode {

T data;

ListNode next;

public ListNode( T data, ListNode next) {

this.data = data;

this.next = next;

}

}

}

public interface LinearList {

public void addHead(T item);

public void addTail(T item);

public void add(T item,int index);

public boolean contain(T item);

public T delHead();

public T delTail();

public T del(int index);

public boolean equals(LinearList list);

public boolean equalLength( LinearList list );

public T get(int index);

public int indexOf(T item);

public boolean isEmpty();

public boolean set(T item,int index);

public int size();

}

1 個解答

評分
  • 9 年前
    最佳解答

    請參考我的做法

    public class LList<T> implements LinearList<T> {

    private ListNode header;

    private int count;

    public LList() {header = null; count = 0;}

    public void addHead(T it){

    this.add(it, 0);

    }

    public void addTail(T it){

    this.add(it, count);

    }

    public void add(T it,int index){

    if (index < 0 || index > count) return;

    int x = 0;

    ListNode t = header;

    while (x++ < index) t = t.next;

    ListNode ln = new ListNode(it, t.next);

    t.next = ln;

    count++;

    }

    public boolean contain(T it){

    ListNode t = header.next;

    while (t != null) {

    if (t.data.equals(it)) return true;

    t = t.next;

    }

    return false;

    }

    public T delHead(){

    if (count == 0) return null;

    return this.del(0);

    }

    public T delTail(){

    if (count == 0) return null;

    return this.del(count - 1);

    }

    public T del(int index){

    if (index >= count) return null;

    ListNode t = header;

    int x = 0;

    while (x++ < index) t = t.next;

    ListNode t2 = t.next;

    t.next = null;

    count--;

    return t2.data;

    }

    public boolean equals(LinearList<T> list){

    if (count != list.size()) return false;

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

    T t1 = this.get(i);

    T t2 = list.get(i);

    if (!t1.equals(t2)) return false;

    }

    return true;

    }

    public boolean equalLength( LinearList<T> list ){

    return count == list.size();

    }

    public T get(int index){

    if (index >= count) return null;

    ListNode t = header;

    int x = -1;

    while (x++ < index) t = t.next;

    return t.data;

    }

    public int indexOf(T it){

    ListNode t = header.next;

    int x = 0;

    while (t != null) {

    if (t.data.equals(it)) return x;

    x++;

    t = t.next;

    }

    return 0;

    }

    public boolean isEmpty(){return header.next == null;}

    public boolean set(T it,int index){

    if (index < 0 || index >= count) return false;

    ListNode t = header;

    int x = 0;

    while (x++ < index) t = t.next;

    ListNode ln = new ListNode(it, t.next.next);

    t.next = ln;

    return true;

    }

    public int size(){return count;}

    private class ListNode {

    ...

    }

    }

    interface LinearList<T> {

    ...

    public boolean equals(LinearList<T> list);

    public boolean equalLength( LinearList<T> list );

    ...

    }

還有問題?馬上發問,尋求解答。