鱼C论坛

 找回密码
 立即注册
查看: 886|回复: 2

[已解决]数据结构

[复制链接]
发表于 2023-11-13 17:20:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请编程完成泛型类DoubleLinkedList(双向链表),DoubleLinkedList的基本功能设计要求:创建双向链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表,并写一个全部测试类
最佳答案
2023-11-13 17:21:06
  1. public class DoubleLinkedList<T> {
  2.     private Node<T> head;
  3.     private Node<T> tail;

  4.     public void insert(T data) {
  5.         Node<T> newNode = new Node<>(data);
  6.         if (head == null) {
  7.             head = newNode;
  8.             tail = newNode;
  9.         } else {
  10.             tail.next = newNode;
  11.             newNode.prev = tail;
  12.             tail = newNode;
  13.         }
  14.     }

  15.     public void delete(T data) {
  16.         Node<T> current = head;
  17.         while (current != null) {
  18.             if (current.data.equals(data)) {
  19.                 if (current.prev != null) {
  20.                     current.prev.next = current.next;
  21.                 } else {
  22.                     head = current.next;
  23.                 }
  24.                 if (current.next != null) {
  25.                     current.next.prev = current.prev;
  26.                 } else {
  27.                     tail = current.prev;
  28.                 }
  29.                 return;
  30.             }
  31.             current = current.next;
  32.         }
  33.     }

  34.     public Node<T> search(T data) {
  35.         Node<T> current = head;
  36.         while (current != null) {
  37.             if (current.data.equals(data)) {
  38.                 return current;
  39.             }
  40.             current = current.next;
  41.         }
  42.         return null;
  43.     }

  44.     public void modify(int index, T newData) {
  45.         Node<T> current = head;
  46.         int i = 0;
  47.         while (current != null && i < index) {
  48.             current = current.next;
  49.             i++;
  50.         }
  51.         if (current != null) {
  52.             current.data = newData;
  53.         }
  54.     }

  55.     public void traverse() {
  56.         Node<T> current = head;
  57.         while (current != null) {
  58.             System.out.print(current.data + " ");
  59.             current = current.next;
  60.         }
  61.     }

  62.     private static class Node<T> {
  63.         private T data;
  64.         private Node<T> prev;
  65.         private Node<T> next;

  66.         public Node(T data) {
  67.             this.data = data;
  68.         }
  69.     }
  70. }
复制代码

  1. public class DoubleLinkedListTest {
  2.     public static void main(String[] args) {
  3.         DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
  4.         list.insert(1);
  5.         list.insert(2);
  6.         list.insert(3);
  7.         list.insert(4);
  8.         list.traverse(); // 输出:1 2 3 4
  9.         list.delete(3);
  10.         list.traverse(); // 输出:1 2 4
  11.         System.out.println(list.search(2).data); // 输出:2
  12.         list.modify(1, 5);
  13.         list.traverse(); // 输出:1 5 4
  14.     }
  15. }
复制代码


在上面的代码中,我们定义了一个泛型类 DoubleLinkedList ,它实现了双向链表的基本功能。我们可以通过 insert 方法插入新的结点,通过 delete 方法删除指定元素结点,通过 search 方法查找指定元素结点,通过 modify 方法修改指定位置结点,以及通过 traverse 方法遍历循环链表。同时,我们编写了一个测试类 DoubleLinkedListTest 来测试这些功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-13 17:21:06 | 显示全部楼层    本楼为最佳答案   
  1. public class DoubleLinkedList<T> {
  2.     private Node<T> head;
  3.     private Node<T> tail;

  4.     public void insert(T data) {
  5.         Node<T> newNode = new Node<>(data);
  6.         if (head == null) {
  7.             head = newNode;
  8.             tail = newNode;
  9.         } else {
  10.             tail.next = newNode;
  11.             newNode.prev = tail;
  12.             tail = newNode;
  13.         }
  14.     }

  15.     public void delete(T data) {
  16.         Node<T> current = head;
  17.         while (current != null) {
  18.             if (current.data.equals(data)) {
  19.                 if (current.prev != null) {
  20.                     current.prev.next = current.next;
  21.                 } else {
  22.                     head = current.next;
  23.                 }
  24.                 if (current.next != null) {
  25.                     current.next.prev = current.prev;
  26.                 } else {
  27.                     tail = current.prev;
  28.                 }
  29.                 return;
  30.             }
  31.             current = current.next;
  32.         }
  33.     }

  34.     public Node<T> search(T data) {
  35.         Node<T> current = head;
  36.         while (current != null) {
  37.             if (current.data.equals(data)) {
  38.                 return current;
  39.             }
  40.             current = current.next;
  41.         }
  42.         return null;
  43.     }

  44.     public void modify(int index, T newData) {
  45.         Node<T> current = head;
  46.         int i = 0;
  47.         while (current != null && i < index) {
  48.             current = current.next;
  49.             i++;
  50.         }
  51.         if (current != null) {
  52.             current.data = newData;
  53.         }
  54.     }

  55.     public void traverse() {
  56.         Node<T> current = head;
  57.         while (current != null) {
  58.             System.out.print(current.data + " ");
  59.             current = current.next;
  60.         }
  61.     }

  62.     private static class Node<T> {
  63.         private T data;
  64.         private Node<T> prev;
  65.         private Node<T> next;

  66.         public Node(T data) {
  67.             this.data = data;
  68.         }
  69.     }
  70. }
复制代码

  1. public class DoubleLinkedListTest {
  2.     public static void main(String[] args) {
  3.         DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
  4.         list.insert(1);
  5.         list.insert(2);
  6.         list.insert(3);
  7.         list.insert(4);
  8.         list.traverse(); // 输出:1 2 3 4
  9.         list.delete(3);
  10.         list.traverse(); // 输出:1 2 4
  11.         System.out.println(list.search(2).data); // 输出:2
  12.         list.modify(1, 5);
  13.         list.traverse(); // 输出:1 5 4
  14.     }
  15. }
复制代码


在上面的代码中,我们定义了一个泛型类 DoubleLinkedList ,它实现了双向链表的基本功能。我们可以通过 insert 方法插入新的结点,通过 delete 方法删除指定元素结点,通过 search 方法查找指定元素结点,通过 modify 方法修改指定位置结点,以及通过 traverse 方法遍历循环链表。同时,我们编写了一个测试类 DoubleLinkedListTest 来测试这些功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-13 17:25:55 | 显示全部楼层
以下是一个基于Java语言的泛型双向链表类DoubleLinkedList的实现,以及一个测试类DoubleLinkedListTest。

[/code]java
// 双向链表节点类
class Node<T> {
    T data;
    Node<T> prev;
    Node<T> next;

    public Node(T data) {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
}

// 泛型双向链表类
public class DoubleLinkedList<T> {
    private Node<T> head;
    private Node<T> tail;

    // 创建空链表
    public DoubleLinkedList() {
        head = null;
        tail = null;
    }

    // 在链表末尾插入新节点
    public void insert(T data) {
        Node<T> newNode = new Node<>(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }

    // 删除指定元素节点
    public void delete(T data) {
        Node<T> current = head;
        while (current != null) {
            if (current.data.equals(data)) {
                if (current.prev != null) {
                    current.prev.next = current.next;
                } else {
                    head = current.next;
                }
                if (current.next != null) {
                    current.next.prev = current.prev;
                } else {
                    tail = current.prev;
                }
                return;
            }
            current = current.next;
        }
    }

    // 查找指定元素节点
    public Node<T> search(T data) {
        Node<T> current = head;
        while (current != null) {
            if (current.data.equals(data)) {
                return current;
            }
            current = current.next;
        }
        return null;
    }

    // 修改指定位置节点的值
    public void modify(int position, T newData) {
        Node<T> current = head;
        for (int i = 0; i < position; i++) {
            if (current == null) {
                throw new IndexOutOfBoundsException();
            }
            current = current.next;
        }
        if (current != null) {
            current.data = newData;
        } else {
            throw new IndexOutOfBoundsException();
        }
    }

    // 遍历链表并打印元素
    public void traverse() {
        Node<T> current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
}
[/code]

以下是测试类DoubleLinkedListTest:

[/code]java
public class DoubleLinkedListTest {
    public static void main(String[] args) {
        // 创建双向链表
        DoubleLinkedList<String> list = new DoubleLinkedList<>();

        // 插入新节点
        list.insert("A");
        list.insert("B");
        list.insert("C");
        list.insert("D");

        // 遍历链表并打印元素
        System.out.println("链表元素:");
        list.traverse();

        // 查找指定元素节点
        String target = "B";
        Node<String> foundNode = list.search(target);
        if (foundNode != null) {
            System.out.println("'" + target + "' 在链表中");
        } else {
            System.out.println("'" + target + "' 不在链表中");
        }

        // 修改指定位置节点的值
        int position = 2;
        String newValue = "E";
        list.modify(position, newValue);

        // 遍历链表并打印元素
        System.out.println("修改后链表元素:");
        list.traverse();

        // 删除指定元素节点
        String toRemove = "C";
        list.delete(toRemove);

        // 遍历链表并打印元素
        System.out.println("删除后链表元素:");
        list.traverse();
    }
}
[/code]

在上述代码中,我们首先定义了一个泛型类DoubleLinkedList,它包含一个内部类Node作为链表的节点。该类实现了双向链表的基本功能,包括创建空链表、插入新节点、删除指定元素节点、查找指定元素节点、修改指定位置节点、遍历循环链表等操作。然后,我们编写了一个测试类DoubleLinkedListTest,用于测试DoubleLinkedList泛型类的基本功能。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-3 11:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表