算法(Algorithms)第4版 练习 1.3.31

双向链表实现:

//1.3.31
package com.qiusongde.linkedlist;

public class DoublyLinkedList<Item> {

    private DoublyNode<Item> first;
    private DoublyNode<Item> last;

    //can be accessed outside this class
    public static class DoublyNode<E>    {
        public E item;
        public DoublyNode<E> next;
        public DoublyNode<E> pre;
    }

    public DoublyLinkedList() {
        first = null;
        last = null;
    }

    /**
     * insert item at the beginning of the list
     *
     * @param list the list to insert
     * @param item the item to be inserted
     */
    public static <T> void insertAtBeginning(DoublyLinkedList<T> list, T item) {

        DoublyNode<T> oldfirst = list.first;//save old first node

        list.first = new DoublyNode<T>();//new first node
        list.first.item = item;//save item
        list.first.next = oldfirst;//point to oldfirst
        list.first.pre = null;//pre initialize to null

        if(oldfirst != null) {
            oldfirst.pre = list.first;
        } else {//oldfirst is null
            list.last = list.first;
        }

    }

    /**
     * insert item at the end of the list
     *
     * @param list the list to insert
     * @param item the item to be inserted
     */
    public static <T> void insertAtTheEnd(DoublyLinkedList<T> list, T item) {

        DoublyNode<T> oldlast = list.last;

        list.last = new DoublyNode<T>();
        list.last.item = item;
        list.last.next = null;
        list.last.pre = oldlast;

        if(oldlast == null) {
            list.first = list.last;
        } else {
            oldlast.next = list.last;
        }

    }

    /**
     * remove the first node in the list. If the first node in the list is null, then do nothing.
     *
     * @param list the list whose first node will be removed
     */
    public static <T> void removeFromBeginning(DoublyLinkedList<T> list) {

        if(list.first == null) {
            return;//do nothing
        }

        list.first = list.first.next;//new position for first
        if(list.first == null) {//not more leave
            list.last = null;
        } else {
            list.first.pre.next = null;
            list.first.pre = null;
        }

    }

    /**
     * remove the last node in the list. If the last node in the list is null, then do nothing.
     *
     * @param list the list whose last node will be removed
     */
    public static <T> void removeFromTheEnd(DoublyLinkedList<T> list) {

        if(list.last == null) {
            return;//do nothing
        }

        list.last = list.last.pre;
        if(list.last == null) {
            list.first = null;
        } else {
            list.last.next.pre = null;
            list.last.next = null;
        }

    }

    public static <T> DoublyNode<T> findDoublyNode(DoublyLinkedList<T> list, T item) {
        DoublyNode<T> current = list.first;

        while(current != null) {
            if(current.item.equals(item)) {
                break;
            }
            current = current.next;
        }

        return current;
    }

    /**
     * insert the item before the node in the list. if node is null or node isn‘t in the list, then do nothing.
     *
     * @param list the list in which the node is
     *
     * @param node the node before which the item will be inserted
     *
     * @param item the item to be inserted
     */
    public static <T> void insertBeforeNode(DoublyLinkedList<T> list, DoublyNode<T> node, T item) {

        if(node == null) {//do nothing
            return;
        }

        if(isInList(list, node)) {//node is in list
            DoublyNode<T> newnode = new DoublyNode<T>();
            newnode.item = item;

            newnode.next = node;
            if(node.pre != null) {//not first node in the list
                node.pre.next = newnode;
            } else {//first node in the list
                list.first = newnode;//change first node
            }
            newnode.pre = node.pre;
            node.pre = newnode;//should be last assign
        }

    }

    /**
     * insert the item after the node in the list. if node is null or node isn‘t in the list, then do nothing.
     *
     * @param list the list in which the node is
     *
     * @param node the node after which the item will be inserted
     *
     * @param item the item to be inserted
     */
    public static <T> void insertAfterNode(DoublyLinkedList<T> list, DoublyNode<T> node, T item) {

        if(node == null) {//do nothing
            return;
        }

        if(isInList(list, node)) {
            DoublyNode<T> newnode = new DoublyNode<T>();
            newnode.item = item;

            newnode.pre = node;
            if(node.next != null) {//not last node in the list
                node.next.pre = newnode;
            } else {//last node in the list
                list.last = newnode;
            }
            newnode.next = node.next;
            node.next = newnode;//should be last assign
        }

    }

    /**
     * remove the node in the list
     *
     * @param list the list in which the node is
     * @param node the node to be removed
     */
    public static <T> void removeNode(DoublyLinkedList<T> list, DoublyNode<T> node) {

        if(node == null) {
            return;
        }

        if(isInList(list, node)) {
            if(list.first == node) {
                removeFromBeginning(list);
            }
            else if(list.last == node) {
                removeFromTheEnd(list);
            }
            else {
                node.pre.next = node.next;
                node.next.pre = node.pre;
            }
        }

    }

    /**
     * see if the node is in the list
     *
     * @param list the list
     * @param node the node
     * @return {@code true} the node is in the list
     *            {@code false} the node isn‘t in the list
     */
    public static <T> boolean isInList(DoublyLinkedList<T> list, DoublyNode<T> node) {

        DoublyNode<T> current = list.first;

        while(current != null) {
            if(current == node) {
                return true;
            }
            current = current.next;
        }

        return false;
    }

    @Override
    public String toString() {
        String s = "";

        DoublyNode<Item> current = first;
        while(current != null) {
            s += current.item + " ";
            current = current.next;
        }

        s += first == null ? "first:null " : "first:" + first.item + " ";
        s += last == null ? "last:null " : "last:" + last.item + " ";

        return s;
    }

}

测试用例:

package com.qiusongde.linkedlist;

import com.qiusongde.linkedlist.DoublyLinkedList.DoublyNode;

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

public class Exercise1331 {

    public static void main(String[] args) {

        DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();
        StdOut.println("Initial doublylist:");
        StdOut.println(list);
        StdOut.println();

        //test insertAtBeginning function
        StdOut.println("Test insertAtBeginning function");
        for(int i = 1; i <= 10; i++) {
            DoublyLinkedList.insertAtBeginning(list, i);
            StdOut.println("insertAtBeginning success: " + i);
            StdOut.println(list);
        }
        StdOut.println();

        //test removeFromBeginning function
        StdOut.println("Test removeFromBeginning function");
        for(int i = 1; i <= 11; i++) {
            DoublyLinkedList.removeFromBeginning(list);
            StdOut.println("removeFromBeginning success: ");
            StdOut.println(list);
        }
        StdOut.println();

        //test insertAtTheEnd function
        StdOut.println("Test insertAtTheEnd function");
        for(int i = 1; i <= 10; i++) {
            DoublyLinkedList.insertAtTheEnd(list, i);
            StdOut.println("insertAtTheEnd success: " + i);
            StdOut.println(list);
        }
        StdOut.println();

        //test removeFromTheEnd function
        StdOut.println("Test removeFromTheEnd function");
        for(int i = 1; i <= 11; i++) {
            DoublyLinkedList.removeFromTheEnd(list);
            StdOut.println("removeFromTheEnd success: ");
            StdOut.println(list);
        }
        StdOut.println();

        //test insertBeforeNode function
        StdOut.println("Test insertBeforeNode function");
        for(int i = 1; i <= 10; i++) {
            DoublyLinkedList.insertAtBeginning(list, i);
        }
        StdOut.println("Initial list:");
        StdOut.println(list);
        for(int i = 0; i < 10; i++) {
            int number = StdRandom.uniform(10 + i) + 1;
            DoublyNode<Integer> node = DoublyLinkedList.findDoublyNode(list, number);
            DoublyLinkedList.insertBeforeNode(list, node, 11 + i);
            StdOut.println("insert " + (11+i) +  " BeforeNode " +  node.item + " success");
            StdOut.println(list);
        }
        StdOut.println();

        //test remove
        StdOut.println("Test removeNode function");
        for(int i = 0; i < 20; i++) {
            DoublyNode<Integer> node = DoublyLinkedList.findDoublyNode(list, i + 1);
            DoublyLinkedList.removeNode(list, node);
            StdOut.println("removeNode success:" + (i+1));
            StdOut.println(list);
        }
        StdOut.println();

        //test insertAfterNode function
        StdOut.println("Test insertAfterNode function");
        for(int i = 1; i <= 10; i++) {
            DoublyLinkedList.insertAtBeginning(list, i);
        }
        StdOut.println("Initial list:");
        StdOut.println(list);
        for(int i = 0; i < 10; i++) {
            int number = StdRandom.uniform(10 + i) + 1;
            DoublyNode<Integer> node = DoublyLinkedList.findDoublyNode(list, number);
            DoublyLinkedList.insertAfterNode(list, node, 11 + i);
            StdOut.println("insert " + (11+i) +  " AfterNode " +  node.item + " success");
            StdOut.println(list);
        }
        StdOut.println();

    }

}

结果输出:

Initial doublylist:
first:null last:null 

Test insertAtBeginning function
insertAtBeginning success: 1
1 first:1 last:1
insertAtBeginning success: 2
2 1 first:2 last:1
insertAtBeginning success: 3
3 2 1 first:3 last:1
insertAtBeginning success: 4
4 3 2 1 first:4 last:1
insertAtBeginning success: 5
5 4 3 2 1 first:5 last:1
insertAtBeginning success: 6
6 5 4 3 2 1 first:6 last:1
insertAtBeginning success: 7
7 6 5 4 3 2 1 first:7 last:1
insertAtBeginning success: 8
8 7 6 5 4 3 2 1 first:8 last:1
insertAtBeginning success: 9
9 8 7 6 5 4 3 2 1 first:9 last:1
insertAtBeginning success: 10
10 9 8 7 6 5 4 3 2 1 first:10 last:1 

Test removeFromBeginning function
removeFromBeginning success:
9 8 7 6 5 4 3 2 1 first:9 last:1
removeFromBeginning success:
8 7 6 5 4 3 2 1 first:8 last:1
removeFromBeginning success:
7 6 5 4 3 2 1 first:7 last:1
removeFromBeginning success:
6 5 4 3 2 1 first:6 last:1
removeFromBeginning success:
5 4 3 2 1 first:5 last:1
removeFromBeginning success:
4 3 2 1 first:4 last:1
removeFromBeginning success:
3 2 1 first:3 last:1
removeFromBeginning success:
2 1 first:2 last:1
removeFromBeginning success:
1 first:1 last:1
removeFromBeginning success:
first:null last:null
removeFromBeginning success:
first:null last:null 

Test insertAtTheEnd function
insertAtTheEnd success: 1
1 first:1 last:1
insertAtTheEnd success: 2
1 2 first:1 last:2
insertAtTheEnd success: 3
1 2 3 first:1 last:3
insertAtTheEnd success: 4
1 2 3 4 first:1 last:4
insertAtTheEnd success: 5
1 2 3 4 5 first:1 last:5
insertAtTheEnd success: 6
1 2 3 4 5 6 first:1 last:6
insertAtTheEnd success: 7
1 2 3 4 5 6 7 first:1 last:7
insertAtTheEnd success: 8
1 2 3 4 5 6 7 8 first:1 last:8
insertAtTheEnd success: 9
1 2 3 4 5 6 7 8 9 first:1 last:9
insertAtTheEnd success: 10
1 2 3 4 5 6 7 8 9 10 first:1 last:10 

Test removeFromTheEnd function
removeFromTheEnd success:
1 2 3 4 5 6 7 8 9 first:1 last:9
removeFromTheEnd success:
1 2 3 4 5 6 7 8 first:1 last:8
removeFromTheEnd success:
1 2 3 4 5 6 7 first:1 last:7
removeFromTheEnd success:
1 2 3 4 5 6 first:1 last:6
removeFromTheEnd success:
1 2 3 4 5 first:1 last:5
removeFromTheEnd success:
1 2 3 4 first:1 last:4
removeFromTheEnd success:
1 2 3 first:1 last:3
removeFromTheEnd success:
1 2 first:1 last:2
removeFromTheEnd success:
1 first:1 last:1
removeFromTheEnd success:
first:null last:null
removeFromTheEnd success:
first:null last:null 

Test insertBeforeNode function
Initial list:
10 9 8 7 6 5 4 3 2 1 first:10 last:1
insert 11 BeforeNode 7 success
10 9 8 11 7 6 5 4 3 2 1 first:10 last:1
insert 12 BeforeNode 8 success
10 9 12 8 11 7 6 5 4 3 2 1 first:10 last:1
insert 13 BeforeNode 10 success
13 10 9 12 8 11 7 6 5 4 3 2 1 first:13 last:1
insert 14 BeforeNode 10 success
13 14 10 9 12 8 11 7 6 5 4 3 2 1 first:13 last:1
insert 15 BeforeNode 2 success
13 14 10 9 12 8 11 7 6 5 4 3 15 2 1 first:13 last:1
insert 16 BeforeNode 4 success
13 14 10 9 12 8 11 7 6 5 16 4 3 15 2 1 first:13 last:1
insert 17 BeforeNode 12 success
13 14 10 9 17 12 8 11 7 6 5 16 4 3 15 2 1 first:13 last:1
insert 18 BeforeNode 7 success
13 14 10 9 17 12 8 11 18 7 6 5 16 4 3 15 2 1 first:13 last:1
insert 19 BeforeNode 15 success
13 14 10 9 17 12 8 11 18 7 6 5 16 4 3 19 15 2 1 first:13 last:1
insert 20 BeforeNode 9 success
13 14 10 20 9 17 12 8 11 18 7 6 5 16 4 3 19 15 2 1 first:13 last:1 

Test removeNode function
removeNode success:1
13 14 10 20 9 17 12 8 11 18 7 6 5 16 4 3 19 15 2 first:13 last:2
removeNode success:2
13 14 10 20 9 17 12 8 11 18 7 6 5 16 4 3 19 15 first:13 last:15
removeNode success:3
13 14 10 20 9 17 12 8 11 18 7 6 5 16 4 19 15 first:13 last:15
removeNode success:4
13 14 10 20 9 17 12 8 11 18 7 6 5 16 19 15 first:13 last:15
removeNode success:5
13 14 10 20 9 17 12 8 11 18 7 6 16 19 15 first:13 last:15
removeNode success:6
13 14 10 20 9 17 12 8 11 18 7 16 19 15 first:13 last:15
removeNode success:7
13 14 10 20 9 17 12 8 11 18 16 19 15 first:13 last:15
removeNode success:8
13 14 10 20 9 17 12 11 18 16 19 15 first:13 last:15
removeNode success:9
13 14 10 20 17 12 11 18 16 19 15 first:13 last:15
removeNode success:10
13 14 20 17 12 11 18 16 19 15 first:13 last:15
removeNode success:11
13 14 20 17 12 18 16 19 15 first:13 last:15
removeNode success:12
13 14 20 17 18 16 19 15 first:13 last:15
removeNode success:13
14 20 17 18 16 19 15 first:14 last:15
removeNode success:14
20 17 18 16 19 15 first:20 last:15
removeNode success:15
20 17 18 16 19 first:20 last:19
removeNode success:16
20 17 18 19 first:20 last:19
removeNode success:17
20 18 19 first:20 last:19
removeNode success:18
20 19 first:20 last:19
removeNode success:19
20 first:20 last:20
removeNode success:20
first:null last:null 

Test insertAfterNode function
Initial list:
10 9 8 7 6 5 4 3 2 1 first:10 last:1
insert 11 AfterNode 10 success
10 11 9 8 7 6 5 4 3 2 1 first:10 last:1
insert 12 AfterNode 9 success
10 11 9 12 8 7 6 5 4 3 2 1 first:10 last:1
insert 13 AfterNode 8 success
10 11 9 12 8 13 7 6 5 4 3 2 1 first:10 last:1
insert 14 AfterNode 10 success
10 14 11 9 12 8 13 7 6 5 4 3 2 1 first:10 last:1
insert 15 AfterNode 10 success
10 15 14 11 9 12 8 13 7 6 5 4 3 2 1 first:10 last:1
insert 16 AfterNode 4 success
10 15 14 11 9 12 8 13 7 6 5 4 16 3 2 1 first:10 last:1
insert 17 AfterNode 8 success
10 15 14 11 9 12 8 17 13 7 6 5 4 16 3 2 1 first:10 last:1
insert 18 AfterNode 11 success
10 15 14 11 18 9 12 8 17 13 7 6 5 4 16 3 2 1 first:10 last:1
insert 19 AfterNode 14 success
10 15 14 19 11 18 9 12 8 17 13 7 6 5 4 16 3 2 1 first:10 last:1
insert 20 AfterNode 3 success
10 15 14 19 11 18 9 12 8 17 13 7 6 5 4 16 3 20 2 1 first:10 last:1 
时间: 2024-10-10 20:47:10

算法(Algorithms)第4版 练习 1.3.31的相关文章

1.3 Bags, Queues, and Stacks(算法 Algorithms 第4版)(一)

1.3.1 package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; public class FixedCapacityStackOfStrings implements Iterable<String> { privat

ubuntu命令行下java工程编辑与算法(第四版)环境配置

ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是出各种错误,特在此总结一下. ubuntu下java环境配置 由于网上教程比较多,而且也较全面,特此摆放一个链接,跟着此教程总就可以配置好oracle的java jdk,如果想更加省事,直接在命令行下键入java,会提示安装各种开源java jdk,只需要一个命令即可: sudo apt-get i

算法(第4版)PDF下载

网盘下载地址:算法(第4版)PDF下载 – 易分享电子书PDF资源网 作者: 塞奇威克 (Robert Sedgewick) / 韦恩 (Kevin Wayne) 出版社: 人民邮电出版社 原作名: Algorithms 4th edition 译者: 谢路云 出版年: 2012-10-1 页数: 636 定价: 99.00元 装帧: 平装 内容简介 · · · · · · 本书全面讲述算法和数据结构的必备知识,具有以下几大特色. ? 算法领域的经典参考书 Sedgewick畅销著作的最新版,反

排序算法总结(C语言版)

1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择排序 3.1     直接选择排序 3.2     堆排序 4.    归并排序 4.1     二路归并排序 4.2     自然合并排序 5.    分布排序 5.1     基数排序 1.插入排序 1.1      直接插入排序 将已排好序的部分num[0]~num[i]后的一个元素num[i+1]插入到之前已排好序的

算法(第四版)C#题解&mdash;&mdash;1.3.49 用 6 个栈实现一个 O(1) 队列

因为这个解法有点复杂,因此单独开一贴介绍. <算法(第四版)>中的题目是这样的: 1.3.49 栈与队列.用有限个栈实现一个队列,保证每个队列操作(在最坏情况下)都只需要常数次的栈操作. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识--用两个栈模拟一个队列 如何使用两个栈来模拟一个队列操作? 这是一道很经典的题目,答案也有很多种,这里只介绍之后会用到的一种方法. 首先我们有两个栈,H 和 T,分别

算法(第4版)-1.4.9 内存

总结:本小节讲述了Java的内存分配机制以及各种数据结构所使用的内存量. 重点: 1. 计算机中的电路很大一部分的作用就是帮助程序保存一些值并在稍后取出它们. 2. 计算机上的Java对内存的使用经过了精心的设计(程序的每个值在每次运行时所需的内存量都是一样的),但实现了Java的设备非常多,而内存的使用是和实现相关的. 3. 原始数据类型的常见内存.需求见算法(第4版)-1.1.2 原始数据类型与表达式. 4. · 对象本身的开销:16字节 · 对象的引用(内存地址):8字节 · 一般内存的使

“《算法》第4版第2章‘排序’”:初级排序算法(选择、冒泡、插入、希尔)

<算法>第4版作者是Robert Sedgewick 和 Kevin Wayne. 1. 选择排序 选择排序可以说是最简单的排序方法.首先,找到数组中最小的那个元素:其次,将它与数组的第一个元素交换位置(如果第一个元素就是最小元素,那么它就和自己交换):再次,在剩下的元素中找到最小的元素,将它与数组的第二个元素交换位置.如此往复,直到将整个数组排序. 该书中提出一个命题:对于长度为N的数组,选择排序需要大约N2/2次比较和N次交换.  程序如下: 1 void SelectionSort::s

“《算法》第四版第2章‘排序’”:初级排序算法

<算法>第4版作者是Robert Sedgewick 和 Kevin Wayne. 1. 选择排序 2. 冒泡排序 3. 插入排序 4. 希尔排序

《算法》第四版 IDEA 运行环境的搭建

<算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程.我相信许多读者跟我一样,在学习Java的时候没有使用过命令行编译的形式去运行Java代码,直接使用Eclipse工具去进行开发的,因此,当看到书中 % java BinarySerach xxx.txt < xxx.txt 的时候,不免有点不知所措.笔者现在使用的IDE是IDEA,因此是想要在IDEA

算法导论 第三版 中文版

下载地址:网盘下载 算法导论 第三版 中文版 清晰 PDF,全书共8部分35章节,内容涵盖基础知识.排序和顺序统计量.数据结构.高级设计和分析技术.高级数据结构.图算法.算法问题选编.以及数学基础知识.非常实用的参考书和工程实践手册.此外,其他资源也已经上传,全部免费,欢迎大家下载! 第3版的主要变化 1.新增了van Emde Boas树和多线程算法,并且将矩阵基础移至附录. 2.修订了递归式(现在称为"分治策略")那一章的内容,更广泛地覆盖分治法. 3.移除两章很少讲授的内容:二项