linkedList

来源:互联网 发布:手机全包壳 知乎 编辑:程序博客网 时间:2024/06/07 10:25
/** * Created by Yingkan Chen on 18.01.2016. */public class singlyLinkedList {    private singlyLinkedList next;    private int contend;    singlyLinkedList(){    }    singlyLinkedList(int i){        this.contend = i;        this.next = null;    }    singlyLinkedList(int i, singlyLinkedList next){        this.contend = i;        this.next = next;    }    public void append(int content){        singlyLinkedList n = new singlyLinkedList(content);        n.next = this.next;        this.next = n;    }    public void insert( int pos, int content){        //        if (pos!=2){            pos = pos -1;            this.next.insert(pos,content);        }        else {            singlyLinkedList ss = new singlyLinkedList(content, this.next);            this.next = ss;        }    }    public void delete(int pos){        if (pos!=2){            pos = pos -1;            this.next.delete(pos);        }        else {           this.next = this.next.next;        }    }    public static void main(String[] arg){        // create a 10 nodes list        singlyLinkedList pointer = new singlyLinkedList(1);        for(int i = 0; i<10;i++) {            singlyLinkedList s = new singlyLinkedList(i+2);            s.next = pointer;            pointer = s;        }        // get the 3rd node contend        System.out.println(pointer.next.next.contend);        // insert to 3rd position        pointer.insert(2,100);        System.out.println(pointer.next.contend);        // delete 3rd position        pointer.delete(2);        System.out.println(pointer.next.contend);    }}
0 0
原创粉丝点击