鱼C论坛

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

泛型类

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

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

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

x
java编程完成泛型类DoubleLinkedList(双向链表),DoubleLinkedList的基本功能设计要求:创建双向链表、插入新的结点、删除指定元素结点、查找指定元素结点、修改指定位置结点、遍历循环链表。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 17:36:34 | 显示全部楼层
  1. public class DoubleLinkedList<T> {
  2.     private Node<T> head;
  3.     private Node<T> tail;

  4.     private class Node<T> {
  5.         T data;
  6.         Node<T> prev;
  7.         Node<T> next;

  8.         public Node(T data) {
  9.             this.data = data;
  10.         }
  11.     }

  12.     public void insert(T data) {
  13.         Node<T> newNode = new Node<>(data);
  14.         if (head == null) {
  15.             head = newNode;
  16.             tail = newNode;
  17.         } else {
  18.             tail.next = newNode;
  19.             newNode.prev = tail;
  20.             tail = newNode;
  21.         }
  22.     }

  23.     public void delete(T data) {
  24.         Node<T> current = head;
  25.         while (current != null) {
  26.             if (current.data.equals(data)) {
  27.                 if (current.prev != null) {
  28.                     current.prev.next = current.next;
  29.                 } else {
  30.                     head = current.next;
  31.                 }
  32.                 if (current.next != null) {
  33.                     current.next.prev = current.prev;
  34.                 } else {
  35.                     tail = current.prev;
  36.                 }
  37.                 return;
  38.             }
  39.             current = current.next;
  40.         }
  41.     }

  42.     public Node<T> find(T data) {
  43.         Node<T> current = head;
  44.         while (current != null) {
  45.             if (current.data.equals(data)) {
  46.                 return current;
  47.             }
  48.             current = current.next;
  49.         }
  50.         return null;
  51.     }

  52.     public void modify(int index, T newData) {
  53.         Node<T> current = head;
  54.         int i = 0;
  55.         while (current != null && i < index) {
  56.             current = current.next;
  57.             i++;
  58.         }
  59.         if (current != null) {
  60.             current.data = newData;
  61.         }
  62.     }

  63.     public void traverse() {
  64.         Node<T> current = head;
  65.         while (current != null) {
  66.             System.out.print(current.data + " ");
  67.             current = current.next;
  68.         }
  69.     }
  70. }
复制代码


这是一个泛型类DoubleLinkedList,它实现了双向链表的基本功能。在这个类中,我们使用了一个内部类Node来表示链表中的节点,其中包含数据和指向前一个节点和后一个节点的引用。类中包含了插入新节点、删除指定元素节点、查找指定元素节点、修改指定位置节点和遍历循环链表的方法。每个方法都根据具体的需求进行了实现。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-12 17:37:33 | 显示全部楼层
当编写泛型类DoubleLinkedList时,使用Java语言需要考虑以下基本功能设计要求:

1. 创建双向链表:创建一个空的双向链表,初始化头部和尾部节点为空,同时维护链表的大小。

2. 插入新的节点:可以在链表的头部、尾部或指定位置插入新的节点。

3. 删除指定元素节点:删除链表中指定元素值的节点。

4. 查找指定元素节点:查找特定元素值的节点,并返回其位置。

5. 修改指定位置节点:允许通过索引修改链表中指定位置的节点的值。

6. 遍历双向链表:提供方法来遍历整个双向链表,输出其中的所有元素。

下面是一个基本的泛型类DoubleLinkedList的实现,满足上述的基本功能设计要求:

[/code]java
public class DoubleLinkedList<T> {
    private Node<T> head;
    private Node<T> tail;
    private int size;

    private static class Node<T> {
        T data;
        Node<T> prev;
        Node<T> next;

        Node(T data) {
            this.data = data;
        }
    }

    public DoubleLinkedList() {
        head = null;
        tail = null;
        size = 0;
    }

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

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

    // 查找指定元素节点的位置
    public int findNode(T data) {
        Node<T> current = head;
        int index = 0;
        while (current != null) {
            if (current.data.equals(data)) {
                return index;
            }
            current = current.next;
            index++;
        }
        return -1;
    }

    // 修改指定位置节点的值
    public void updateNode(int index, T data) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index out of bounds");
        }
        Node<T> current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        current.data = data;
    }

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

这是一个简单的泛型双向链表类的实现,你可以根据自己的需求进一步扩展和优化这个实现。希望这个例子能够帮助你完成泛型类DoubleLinkedList的基本功能设计。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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