算法練習(37): 鏈表的增刪查改3(1.3.24-1.3.27)

本系列博客習題來自《算法(第四版)》设哗,算是本人的讀書筆記柳弄,如果有人在讀這本書的舶胀,歡迎大家多多交流概说。為了方便討論,本人新建了一個微信群(算法交流)嚣伐,想要加入的糖赔,請?zhí)砑游业奈⑿盘枺簔hujinhui207407 謝謝。另外轩端,本人的個人博客 http://www.kyson.cn 也在不停的更新中放典,歡迎一起討論

算法(第4版)

知識點

  • 鏈表節(jié)點增刪查改

題目

1.3.24 編寫一個方法removeAfter(),接受一個鏈表結點作為參數(shù)并刪除該結點的后續(xù)結點(如果參數(shù)結點或參數(shù)結點的后續(xù)結點為空則什么也不做)基茵。


1.3.24 Write a method removeAfter() that takes a linked-list Node as argument and removes the node following the given one (and does nothing if the argument or the next field in the argument node is null).

答案

public class LinkedListExecise4<Item> {

    private static class Node<Item> {
        Node next;
        Item item;
        
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:"+item;
        }
    }

    public Node<Item> removeAfter(Node node, Node first) {
        if (first == null) {
            return null;
        }

        Node current = first;
        while (current != null) {
            if (current.item.equals(node.item)) 
            {
                if (current.next != null) 
                {
                    current.next = current.next.next;
                    return first;
                } else {
                    return first;
                }
            }
            current = current.next;
        }

        return null;
    }

    /**
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 創(chuàng)建鏈表
         * */
        Node<String> first = new Node<String>();
        Node<String> second = new Node<String>();
        Node<String> third = new Node<String>();
        Node<String> forth = new Node<String>();
        Node<String> fifth = new Node<String>();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "頂級程序員不穿女裝";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise4<String> linkedListExercise4 = new LinkedListExecise4<String>();
        Node targetNode = first;
        System.out.println("即將移除節(jié)點:"+targetNode+"之后的節(jié)點");
        Node resultNode = linkedListExercise4.removeAfter(targetNode, first);

        System.out.println("新鏈表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");
    }
}

代碼索引

LinkedListExecise4.java

題目

1.3.25 編寫一個方法insertAfter()奋构,接受兩個鏈表結點作為參數(shù),將第二結點插入鏈表并使之成為第一個結點的后續(xù)結點(如果兩個參數(shù)為空則什么也不做)拱层。


1.3.25 1.3.25 Write a method insertAfter() that takes two linked-list Node arguments and inserts the second after the first on its list (and does nothing if either argument is null).

答案

public class LinkedListExecise5<Item> {

    private static class Node<Item> {
        Node next;
        Item item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }
    }

    public Node<Item> insertAfter(Node<Item> targetNode, Node<Item> node,
            Node<Item> first) {
        if (targetNode == null || node == null) {
            return first;
        }
        Node<Item> current = first;
        while (current != null) {
            if (current.equals(targetNode)) {
                Node<Item> t = current.next;
                current.next = node;
                node.next = t;
                return first;
            }
            current = current.next;
        }

        return null;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 創(chuàng)建鏈表
         * */
        Node<String> first = new Node<String>();
        Node<String> second = new Node<String>();
        Node<String> third = new Node<String>();
        Node<String> forth = new Node<String>();
        Node<String> fifth = new Node<String>();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "頂級程序員不穿女裝";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise5<String> linkedListExercise5 = new LinkedListExecise5<String>();
        Node targetNode = second;
        System.out.println("即將移除節(jié)點:" + targetNode + "之后的節(jié)點");
        Node<String> insertedNode = new Node<String>();
        insertedNode.item = "天天開心笑哈哈";
        Node resultNode = linkedListExercise5.insertAfter(targetNode,
                insertedNode, first);

        System.out.println("新鏈表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");
    }

}

代碼索引

LinkedListExecise5.java

題目

1.3.26 編寫一個方法remove()弥臼,接受一個鏈表和一個字符串key作為參數(shù),刪除鏈表中所有item域為key的結點根灯。


1.3.26 Write a method remove() that takes a linkedlist and a string key as arguments and removes all of the nodes in the list that have key as its item field.

答案

public class LinkedListExecise6 {

    private static class Node {
        Node next;
        String item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }
    }
    
    public Node remove(Node first, String key) {
        Node newFirst = new Node();
        newFirst.next = first;
        
        Node current = newFirst;
        while (current.next != null) {
            if (current.next.item.equals(key)) {
                current.next = current.next.next;
            }else {
                current = current.next;
            }
        }
        
        return newFirst.next;
    
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        /**
         * 創(chuàng)建鏈表
         * */
        Node first = new Node();
        Node second = new Node();
        Node third = new Node();
        Node forth = new Node();
        Node fifth = new Node();
        first.item = "我的";
        first.next = second;
        second.item = "名字";
        second.next = third;
        third.item = "叫";
        third.next = forth;
        forth.item = "頂級程序員不穿女裝";
        forth.next = fifth;
        fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
        fifth.next = null;

        LinkedListExecise6 linkedListExercise6 = new LinkedListExecise6();
        Node resultNode = linkedListExercise6.remove(first, "我的");

        System.out.println("新鏈表:\n-------");
        Node current2 = resultNode;
        while (current2.next != null) {
            System.out.println(current2.item);
            current2 = current2.next;
        }
        System.out.println(current2.item);
        System.out.println("-------");

    }
}

代碼索引

LinkedListExecise6.java

題目

1.3.27 編寫一個方法max()径缅,接受一個鏈表的首結點作為參數(shù),返回鏈表中鍵最大的節(jié)點的值烙肺。假設所有鍵均為正整數(shù)纳猪,如果鏈表為空則返回0。


1.3.27 Write a method max() that takes a reference to the first node in a linkedlist as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty.

答案

public class LinkedListExecise7 {

    private static class Node {
        Node next;
        Integer item;

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "item:" + item;
        }

    }

    public Integer max(Node first) {
        if (null == first) {
            return 0;
        }

        if (first.next == null) {
            return first.item;
        }

        Node current = first;
        Integer max = current.item;
        while (current != null) {
            if (current.item > max) {
                max = current.item;
            }
            current = current.next;
        }

        return max;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
         * 創(chuàng)建鏈表
         * */
        Node first = new Node();
        Node second = new Node();
        Node third = new Node();
        Node forth = new Node();
        Node fifth = new Node();
        first.item = 1;
        second.item = 3;
        third.item = 999;
        forth.item = 33;
        fifth.item = 21;

        first.next = second;
        second.next = third;
        third.next = forth;
        forth.next = fifth;
        fifth.next = null;

        System.out.println("原鏈表:\n-------");
        Node current1 = first;
        while (current1.next != null) {
            System.out.println(current1.item);
            current1 = current1.next;
        }
        System.out.println(current1.item);
        System.out.println("-------");

        System.out.println("正在求最大值...");
        LinkedListExecise7 linkedListExercise7 = new LinkedListExecise7();
        Integer result = linkedListExercise7.max(first);
        System.out.println("result:" + result);
    }
}

代碼索引

LinkedListExecise7.java

廣告

我的首款個人開發(fā)的APP壁紙寶貝上線了桃笙,歡迎大家下載氏堤。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市搏明,隨后出現(xiàn)的幾起案子鼠锈,更是在濱河造成了極大的恐慌,老刑警劉巖熏瞄,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脚祟,死亡現(xiàn)場離奇詭異谬以,居然都是意外死亡强饮,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門为黎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來邮丰,“玉大人,你說我怎么就攤上這事铭乾〖袅” “怎么了?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵炕檩,是天一觀的道長斗蒋。 經(jīng)常有香客問我捌斧,道長,這世上最難降的妖魔是什么泉沾? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任捞蚂,我火速辦了婚禮,結果婚禮上跷究,老公的妹妹穿的比我還像新娘姓迅。我一直安慰自己,他們只是感情好俊马,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布丁存。 她就那樣靜靜地躺著,像睡著了一般柴我。 火紅的嫁衣襯著肌膚如雪解寝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天屯换,我揣著相機與錄音编丘,去河邊找鬼。 笑死彤悔,一個胖子當著我的面吹牛嘉抓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播晕窑,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼抑片,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了杨赤?” 一聲冷哼從身側響起敞斋,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎疾牲,沒想到半個月后植捎,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡阳柔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年焰枢,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片舌剂。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡济锄,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出霍转,到底是詐尸還是另有隱情荐绝,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布避消,位于F島的核電站低滩,受9級特大地震影響召夹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恕沫,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一戳鹅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧昏兆,春花似錦枫虏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至跑筝,卻和暖如春死讹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背曲梗。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工赞警, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人虏两。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓愧旦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親定罢。 傳聞我的和親對象是個殘疾皇子笤虫,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

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