算法-第四版-练习1.3.31解答

时间:2023-02-13 13:03:41

问题

实现一个嵌套类DoubleNode用来构造双向链表,其中每个结点都含有一个指向前驱元素的引用和一个指向后续元素的引用(如果不存在则为null)。为以下任务实现若干静态方法:在头插入结点、在表尾插入结点、从表头删除结点、从表尾删除结点、在指定结点前插入新结点、在指定结点之后插入新结点、删除指定结点。

解决思路

一切按照常规方法处理就可以了。

代码

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Oct 26, 2016 9:48:18 AM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

/**
 * ClassName    : DoubleList <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Oct 26, 2016 9:48:18 AM <br>
 * 
 * @version 
 */
public class DoubleList<Item>
{
    private DoubleNode<Item> first;
    private DoubleNode<Item> last;
    
    public static class DoubleNode<E>
    {
        public E item;
        public DoubleNode<E> next;
        public DoubleNode<E> prev;
    }
    
    public DoubleList()
    {
        first = null;
        last = null;
    }
    
    public static <T> void insertAsFirst(DoubleList<T> dl, T e)
    {
        DoubleNode<T> node = new DoubleNode<T>();
        node.item = e;
        node.prev = null;
        node.next = dl.first;
        if (dl.first == null)
        {
            dl.last = node;
        }
        else
        {
            dl.first.prev = node;
        }
        dl.first = node;
    }
    
    public static <T> void insertAsLast(DoubleList<T> dl, T e)
    {
        DoubleNode<T> node = new DoubleNode<T>();
        node.item = e;
        node.prev = dl.last;
        node.next = null;
        if (dl.last == null)
        {
            dl.first = node;
        }
        else
        {
            dl.last.next = node;
        }
        dl.last = node;
    }
    
    public static <T> void deleteFirst(DoubleList<T> dl)
    {
        if (dl.first == null)
        {
            return;
        }
        if (dl.first == dl.last)
        {
            dl.first = null;
            dl.last = null;
        }
        else
        {
            dl.first = dl.first.next;
            dl.first.prev.next = null;  // 
            dl.first.prev = null;
        }
    }
    
    public static <T> void deleteLast(DoubleList<T> dl)
    {
        if (dl.last == null)
        {
            return;
        }
        if (dl.last == dl.first)
        {
            dl.first = null;
            dl.last = null;
        }
        else
        {
            dl.last = dl.last.prev;
            dl.last.next.prev = null;
            dl.last.next = null;
        }
    }
    
    public static <T> void insertBefore(DoubleList<T> dl, DoubleNode<T> node, T e)
    {
        if (node == null)
        {
            return;
        }
        if (dl.first == node)
        {
            insertAsFirst(dl, e);
        }
        else
        {
            DoubleNode<T> newNode = new DoubleNode<T>();
            newNode.item = e;
            newNode.prev = node.prev;
            newNode.next = node;
            node.prev.next = newNode;
            node.prev = newNode;
        }
    }
    
    public static <T> void insertAfter(DoubleList<T> dl, DoubleNode<T> node, T e)
    {
        if (node == null)
        {
            return;
        }
        if (dl.last == node)
        {
            insertAsLast(dl, e);
        }
        else
        {
            DoubleNode<T> newNode = new DoubleNode<T>();
            newNode.item = e;
            newNode.prev = node;
            newNode.next = node.next;
            node.next.prev = newNode;
            node.next = newNode;
        }
    }
    
    public static <T> void deleteNode(DoubleList<T> dl, DoubleNode<T> node)
    {
        if (node == null)
        {
            return;
        }
        else if (node == dl.first)
        {
            deleteFirst(dl);
        }
        else if (node == dl.last)
        {
            deleteLast(dl);
        }
        else
        {
            node.prev.next = node.next;
            node.next.prev = node.prev;
            node.prev = null;
            node.next = null;
        }
    }
    
    public static <T> DoubleNode<T> search(DoubleList<T> dl, T e)
    {
        DoubleNode<T> node = dl.first;
        while (node != null)
        {
            if (node.item.equals(e))
            {
                return node;
            }
            node = node.next;
        }
        return null;
    }
    
    public static <T> void print(DoubleList<T> dl)
    {
        DoubleNode<T> node = dl.first;
        while (node != null)
        {
            System.out.println(node.item);
            node = node.next;
        }
    }
    
}

测试代码:

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Oct 26, 2016 9:47:54 AM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

/**
 * ClassName    : E10331 <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Oct 26, 2016 9:47:54 AM <br>
 * 
 * @version 
 */
public class E10331
{
    
    public static void testInsertAsFirst()
    {
        System.out.println("--------------------");
        System.out.println("testInsertAsFirst:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsFirst(dl, "a");
        DoubleList.insertAsFirst(dl, "b");
        DoubleList.insertAsFirst(dl, "c");
        DoubleList.insertAsFirst(dl, "d");
        System.out.println("After insert a, b, c, d:");
        DoubleList.print(dl);
    }
     
    public static void testInsertAsLast()
    {
        System.out.println("--------------------");
        System.out.println("testInsertAsLast:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        System.out.println("After insert a, b, c, d:");
        DoubleList.print(dl);
    }
      
    public static void testInsertAsFirstAndLast()
    {
        System.out.println("--------------------");
        System.out.println("testInsertAsFirstAndLast:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        DoubleList.insertAsFirst(dl, "1");
        DoubleList.insertAsFirst(dl, "2");
        DoubleList.insertAsFirst(dl, "3");
        DoubleList.insertAsFirst(dl, "4");
        System.out.println("After insertAsLast a, b, c, d and insertAsFirst 1, 2, 3, 4:");
        DoubleList.print(dl);
    }
      
    public static void testDeleteFirst()
    {
        System.out.println("--------------------");
        System.out.println("testDeleteFirst:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        System.out.println("list is: ");
        DoubleList.print(dl);
        System.out.println("After deleteFrist:");
        DoubleList.deleteFirst(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteFrist again:");
        DoubleList.deleteFirst(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteFrist again:");
        DoubleList.deleteFirst(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteFrist again:");
        DoubleList.deleteFirst(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteFrist again:");
        DoubleList.deleteFirst(dl);
        DoubleList.print(dl);
    }
    
    public static void testDeleteLast()
    {
        System.out.println("--------------------");
        System.out.println("testDeleteLast:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        System.out.println("list is: ");
        DoubleList.print(dl);
        System.out.println("After deleteLast:");
        DoubleList.deleteLast(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteLast again:");
        DoubleList.deleteLast(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteLast again:");
        DoubleList.deleteLast(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteLast again:");
        DoubleList.deleteLast(dl);
        DoubleList.print(dl);
        
        System.out.println("After deleteLast again:");
        DoubleList.deleteLast(dl);
        DoubleList.print(dl);
    }
     
    public static void testSearch()
    {
        System.out.println("--------------------");
        System.out.println("testSearch:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsFirst(dl, "a");
        DoubleList.insertAsFirst(dl, "b");
        DoubleList.insertAsFirst(dl, "c");
        DoubleList.insertAsFirst(dl, "d");
        
        System.out.println("List is :");
        DoubleList.print(dl);
        
        DoubleList.DoubleNode<String> node = DoubleList.search(dl, "c");
        System.out.println("Search c: ");
        if (node != null)
        {
            System.out.println("Find " + node.item);
        }
        else
        {
            System.out.println("Not found");
        }
                
        node = DoubleList.search(dl, "d");
        System.out.println("Search d: ");
        if (node != null)
        {
            System.out.println("Find " + node.item);
        }
        else
        {
            System.out.println("Not found");
        }
        
        node = DoubleList.search(dl, "a");
        System.out.println("Search a: ");
        if (node != null)
        {
            System.out.println("Find " + node.item);
        }
        else
        {
            System.out.println("Not found");
        }
         
        node = DoubleList.search(dl, "x");
        System.out.println("Search x: ");
        if (node != null)
        {
            System.out.println("Find " + node.item);
        }
        else
        {
            System.out.println("Not found");
        }
    }
    
    public static void testInsertBeforeAndAfter()
    {
        System.out.println("--------------------");
        System.out.println("testInsertBeforeAndAfter:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        
        System.out.println("List is :");
        DoubleList.print(dl);
        
        DoubleList.DoubleNode<String> node = DoubleList.search(dl, "c");
        DoubleList.insertAfter(dl, node, "C");
        System.out.println("After insert C after c:");
        DoubleList.print(dl);
        
        node = DoubleList.search(dl, "d");
        DoubleList.insertAfter(dl, node, "D");
        System.out.println("After insert D after d:");
        DoubleList.print(dl);
        
        node = DoubleList.search(dl, "c");
        DoubleList.insertBefore(dl, node, "B");
        System.out.println("After insert B before c:");
        DoubleList.print(dl);
        
        node = DoubleList.search(dl, "a");
        DoubleList.insertBefore(dl, node, "A");
        System.out.println("After insert A before a:");
        DoubleList.print(dl);
    }
     
    public static void testDeleteNode()
    {
        System.out.println("--------------------");
        System.out.println("testDeleteNode:");
        DoubleList<String> dl = new DoubleList<String>();
        DoubleList.insertAsLast(dl, "a");
        DoubleList.insertAsLast(dl, "b");
        DoubleList.insertAsLast(dl, "c");
        DoubleList.insertAsLast(dl, "d");
        
        System.out.println("List is :");
        DoubleList.print(dl);
        
        DoubleList.DoubleNode<String> node = DoubleList.search(dl, "c");
        DoubleList.deleteNode(dl, node);
        System.out.println("After delete c:");
        DoubleList.print(dl);
        
        node = DoubleList.search(dl, "a");
        DoubleList.deleteNode(dl, node);
        System.out.println("After delete a:");
        DoubleList.print(dl);

        node = DoubleList.search(dl, "d");
        DoubleList.deleteNode(dl, node);
        System.out.println("After delete d:");
        DoubleList.print(dl);
    }
   
    
    public static void main(String[] args)
    {
        testInsertAsFirst();
        testInsertAsLast();
        testInsertAsFirstAndLast();
        testDeleteFirst();
        testDeleteLast();
        testSearch();
        testInsertBeforeAndAfter();
        testDeleteNode();
    }
}

结果:

--------------------
testInsertAsFirst:
After insert a, b, c, d:
d
c
b
a
--------------------
testInsertAsLast:
After insert a, b, c, d:
a
b
c
d
--------------------
testInsertAsFirstAndLast:
After insertAsLast a, b, c, d and insertAsFirst 1, 2, 3, 4:
4
3
2
1
a
b
c
d
--------------------
testDeleteFirst:
list is: 
a
b
c
d
After deleteFrist:
b
c
d
After deleteFrist again:
c
d
After deleteFrist again:
d
After deleteFrist again:
After deleteFrist again:
--------------------
testDeleteLast:
list is: 
a
b
c
d
After deleteLast:
a
b
c
After deleteLast again:
a
b
After deleteLast again:
a
After deleteLast again:
After deleteLast again:
--------------------
testSearch:
List is :
d
c
b
a
Search c: 
Find c
Search d: 
Find d
Search a: 
Find a
Search x: 
Not found
--------------------
testInsertBeforeAndAfter:
List is :
a
b
c
d
After insert C after c:
a
b
c
C
d
After insert D after d:
a
b
c
C
d
D
After insert B before c:
a
b
B
c
C
d
D
After insert A before a:
A
a
b
B
c
C
d
D
--------------------
testDeleteNode:
List is :
a
b
c
d
After delete c:
a
b
d
After delete a:
b
d
After delete d:
b



算法-第四版-1.3 背包、队列和栈-习题索引汇总

算法-第四版习题索引汇总