單鏈表

一、基本操作

二睡互、編碼思路

1根竿,按照普適思路編寫陵像;2,注意判空保證程序健壯性(實(shí)際上判空也是考慮全面)

三寇壳、實(shí)現(xiàn)了上述基本操作的單鏈表

1醒颖,按照普適思路編寫;2壳炎,注意判空

/**
     * 插入到單鏈表尾部
     * 【增】
     * @param data
     * @return
     */
    public boolean addNode(E data){
        Node newNode=new Node(data);
        // 找到最后一個(gè)節(jié)點(diǎn)
        Node p=head;
        while(p!=null && p.next!=null){
            p=p.next;
        }// 找到尾節(jié)點(diǎn)
        if(p==null){
            // 說明原來鏈表為空
            head=newNode;
        }else{
            // p.next==null;
            // 原來鏈表不為空
            p.next=newNode;
        }
        return true;
        
    }
    
    /*public boolean addNode2(E data){
        Node newNode=new Node(data);
        if(head==null){
            head=newNode;
        }else{
            // 找到最后一個(gè)節(jié)點(diǎn)
            Node p=head;
            while(p.next!=null){
                p=p.next;
            }
            p.next=newNode;
        }
        return true;
    }*/
    
    /**
     * 將數(shù)據(jù)插入后使得其索引為index
     * 鏈表的插入需要知道前一節(jié)點(diǎn)
     * @param index:[0,size]區(qū)間內(nèi)
     * @param data
     * @return
     */
    public boolean add(int index,E data){
        if(index <0 || index>length()){
            throw new ArrayIndexOutOfBoundsException();
        }
        Node newNode=new Node(data);
        if(index==0){
            newNode.next=head;
            head=newNode;
        }else{
            // 找到第index-個(gè)節(jié)點(diǎn)
            Node p=node(index-1);
            newNode.next=p.next;
            p.next=newNode;
        }
        return true;
    }

1泞歉,按照普適思路編寫;2匿辩,注意判空

/**
     * 刪除元素值為data的元素
     * @param data
     * @return
     */
    public boolean removeElement(E e){
        if(e==null){
            Node pre=null;
            Node p=head;
            // 遍歷整個(gè)鏈表
            while(p!=null){
                if(p.data==null){
                    // 找到這個(gè)元素了
                    if(pre!=null){
                        pre.next=p.next;
                    }else{
                        head=p.next;
                    }
                    return true;
                }else{
                    // 未找到
                    pre=p;
                    p=p.next;
                }
            }
        }else{
            Node pre=null;
            Node p=head;
            // 遍歷整個(gè)鏈表
            while(p!=null){
                if(e.equals(p.data)){
                    // 刪除
                    if(pre!=null){
                        pre.next=p.next;
                    }else{
                        head=head.next;
                    }
                    return true;
                }else{
                    // 未找到
                    pre=p;
                    p=p.next;
                }
            }
        }
        return false;
    }
    
    /**
     * 刪除索引為index的元素
     * @param index
     * @return
     */
    public boolean removeForIndex(int index){
        checkElementIndex(index);
        if(index==0){
            head=head.next;
        }else{
            // 找出第index個(gè)元素腰耙,及其pre元素
            Node pre=head;
            Node p=head.next;
            for(int i=1;i<index;i++){
                pre=p;
                p=p.next;
            }
            // 跳出循環(huán)時(shí):p指向第index個(gè)元素
            pre.next=p.next;
            
        }
        return true;
    }

注意其中找出第index節(jié)點(diǎn)的方法

/**
     * 修改索引值為index的元素值
     * @param index
     * @param e
     * @return
     */
    public boolean set(int index,E e){
        checkElementIndex(index);
        // 找出第index個(gè)元素
        Node p=head;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        p.data=e;
        return true;
    }

/**
     * 得到索引值為index處的元素值
     * @param index
     * @return
     */
    public E get(int index){
        checkElementIndex(index);
        // 找到第index個(gè)元素
        Node p=head;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        return p.data;
    }

源碼

/**
 * 
* <p>Description: 單鏈表</p>  
* @author 楊麗金  
* @date 2018-9-9  
* @version 1.0
 */
public class MySingleList<E> {
    
    class Node{
        E data;
        Node next;
        Node(E data){
            this.data=data;
        }
    }
    
    // 鏈表頭
    private Node head=null;
    
    /**
     * 插入到單鏈表尾部
     * 【增】
     * @param data
     * @return
     */
    public boolean addNode(E data){
        Node newNode=new Node(data);
        // 找到最后一個(gè)節(jié)點(diǎn)
        Node p=head;
        while(p!=null && p.next!=null){
            p=p.next;
        }// 找到尾節(jié)點(diǎn)
        if(p==null){
            // 說明原來鏈表為空
            head=newNode;
        }else{
            // p.next==null;
            // 原來鏈表不為空
            p.next=newNode;
        }
        return true;
        
    }
    
    /*public boolean addNode2(E data){
        Node newNode=new Node(data);
        if(head==null){
            head=newNode;
        }else{
            // 找到最后一個(gè)節(jié)點(diǎn)
            Node p=head;
            while(p.next!=null){
                p=p.next;
            }
            p.next=newNode;
        }
        return true;
    }*/
    
    /**
     * 將數(shù)據(jù)插入后使得其索引為index
     * 鏈表的插入需要知道前一節(jié)點(diǎn)
     * @param index:[0,size]區(qū)間內(nèi)
     * @param data
     * @return
     */
    public boolean add(int index,E data){
        if(index <0 || index>length()){
            throw new ArrayIndexOutOfBoundsException();
        }
        Node newNode=new Node(data);
        if(index==0){
            newNode.next=head;
            head=newNode;
        }else{
            // 找到第index-個(gè)節(jié)點(diǎn)
            Node p=node(index-1);
            newNode.next=p.next;
            p.next=newNode;
        }
        return true;
    }
    
    /**
     * 得到第index個(gè)節(jié)點(diǎn)
     * @param index:[0,size-1],index從0開始
     * @return
     */
    private Node node(int index) {
        Node p=head;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        return p;
    }
    
    
    /**
     * 刪除元素值為data的元素
     * @param data
     * @return
     */
    public boolean removeElement(E e){
        if(e==null){
            Node pre=null;
            Node p=head;
            // 遍歷整個(gè)鏈表
            while(p!=null){
                if(p.data==null){
                    // 找到這個(gè)元素了
                    if(pre!=null){
                        pre.next=p.next;
                    }else{
                        head=p.next;
                    }
                    return true;
                }else{
                    // 未找到
                    pre=p;
                    p=p.next;
                }
            }
        }else{
            Node pre=null;
            Node p=head;
            // 遍歷整個(gè)鏈表
            while(p!=null){
                if(e.equals(p.data)){
                    // 刪除
                    if(pre!=null){
                        pre.next=p.next;
                    }else{
                        head=head.next;
                    }
                    return true;
                }else{
                    // 未找到
                    pre=p;
                    p=p.next;
                }
            }
        }
        return false;
    }
    
    /**
     * 刪除索引為index的元素
     * @param index
     * @return
     */
    public boolean removeForIndex(int index){
        checkElementIndex(index);
        if(index==0){
            head=head.next;
        }else{
            // 找出第index個(gè)元素铲球,及其pre元素
            Node pre=head;
            Node p=head.next;
            for(int i=1;i<index;i++){
                pre=p;
                p=p.next;
            }
            // 跳出循環(huán)時(shí):p指向第index個(gè)元素
            pre.next=p.next;
            
        }
        return true;
    }
    
    // 必須為[0,size-1]范圍內(nèi)
    private void checkElementIndex(int index){
        if(index <0 || index >= length()){
            throw new IndexOutOfBoundsException();
        }
    }

    /**
     * 返回鏈表長(zhǎng)度
     * @return
     */
    public int length(){
        if(head==null){
            return 0;
        }
        Node p=head;
        int len=1;
        while(p.next!=null){
            p=p.next;
            len++;
        }
        return len;
    }
    
    public void print(){
        if(head==null){
            return ;
        }
        Node p=head;
        while(p!=null){
            System.out.print(p.data+",");
            p=p.next;
        }
        System.out.println();
    }
    
    /**
     * 修改索引值為index的元素值
     * @param index
     * @param e
     * @return
     */
    public boolean set(int index,E e){
        checkElementIndex(index);
        // 找出第index個(gè)元素
        Node p=head;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        p.data=e;
        return true;
    }
    
    /**
     * 得到索引值為index處的元素值
     * @param index
     * @return
     */
    public E get(int index){
        checkElementIndex(index);
        // 找到第index個(gè)元素
        Node p=head;
        for(int i=0;i<index;i++){
            p=p.next;
        }
        return p.data;
    }
    
    public static void main(String[] args) {
        MySingleList<Integer> list=new MySingleList<Integer>();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.addNode(2);
        list.addNode(3);
        list.addNode(5);
        list.addNode(-1);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.add(0, 0);
        list.add(1,1);
        list.add(6,99);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeElement(0);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeElement(3);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeElement(99);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeForIndex(0);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeForIndex(1);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.removeForIndex(1);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        list.set(0, 100);
        list.print();
        System.out.println("鏈表長(zhǎng)度:"+list.length());
        System.out.println("第0個(gè)元素:"+list.get(0));
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末挺庞,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子稼病,更是在濱河造成了極大的恐慌选侨,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件然走,死亡現(xiàn)場(chǎng)離奇詭異援制,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)芍瑞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門晨仑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拆檬,你說我怎么就攤上這事洪己。” “怎么了秩仆?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵码泛,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我澄耍,道長(zhǎng)噪珊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任齐莲,我火速辦了婚禮痢站,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘选酗。我一直安慰自己阵难,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布芒填。 她就那樣靜靜地躺著呜叫,像睡著了一般空繁。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上朱庆,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天盛泡,我揣著相機(jī)與錄音,去河邊找鬼娱颊。 笑死傲诵,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的箱硕。 我是一名探鬼主播拴竹,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼剧罩!你這毒婦竟也來了栓拜?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤斑响,失蹤者是張志新(化名)和其女友劉穎菱属,沒想到半個(gè)月后钳榨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舰罚,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年薛耻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了营罢。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡饼齿,死狀恐怖饲漾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情缕溉,我是刑警寧澤考传,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站证鸥,受9級(jí)特大地震影響僚楞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜枉层,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一泉褐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鸟蜡,春花似錦膜赃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽端铛。三九已至,卻和暖如春疲眷,著一層夾襖步出監(jiān)牢的瞬間沦补,已是汗流浹背吩蔑。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工镜豹, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留篮赢,地道東北人调卑。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓沛贪,卻偏偏與公主長(zhǎng)得像宠蚂,于是被迫代替她去往敵國(guó)和親檬寂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子讨跟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容