迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中创建通用链表

作者:迹忆客 最近更新:2023/08/09 浏览次数:

本文我们将介绍如何在 Java 中创建一个通用的单链表。


Java LinkedList 简介

LinkedList 是线性数据结构,它将数据存储在随机地址的节点中,并且意味着位于不连续的位置。

每个节点都有两部分:数据和引用(地址)。 Data/value字段存储值,reference字段存储链表的下一个节点的地址。

将作为成员函数实现的该数据结构的一些常见操作如下。

  • addNode() - 此方法用于在 LinkedList 末尾添加新元素。
  • removeNode(value) - 此方法删除具有指定值的节点。
  • addNode(position,value) - 此方法用于在特定位置添加值。
  • clear() - 此方法清除整个 LinkedList。
  • isEmpty() - 此方法用于检查 LinkedList 是否为空。
  • length() - 此方法为我们提供 LinkedList 的长度。

Java中单链表的通用实现

普通的LinkedList只能存储一种类型的值:整型、字符串、布尔型、浮点型等,所以我们在创建的时候就必须指定类型,但是如果我们要创建一个可以存储任意类型数据的LinkedList怎么办? 数据类型; 为此,我们将使用 Java 中的泛型概念。

创建通用节点类:

在下面的代码中,T表示LinkedList上存储的数据类型。

class Node<T>
{
    T value;
    Node<T> nextPtr;

    Node(T value)
    {
        this.value = value;
        this.nextPtr = null;
    }
}

现在让我们创建通用 LinkedList 类创建方法。

  1. add() 函数:

    void add(T value)
    {
     Node<T> temp = new Node<>(value);  //creating a new node
     if(this.head == null)   //checking if the list is empty
         head = temp;
     else             //if the list is not empty, we go till the end and add the node
     {
         Node<T> tr = head;
         while(tr.nextPtr!=null){
             tr = tr.nextPtr;
         }
    
         tr.nextPtr = temp;
     }
     length = length + 1;  //increasing the length of list
    }
    
  2. remove() 函数:

    void remove(T key)
    {
     Node<T> prev = new Node<>(null);
     prev.nextPtr = head;
     Node<T> next = head.nextPtr;
     Node<T> tr = head;
     boolean isNodepresent = false; // to check if node is present
     if(head.value == key ){
         head = head.nextPtr;
         isNodepresent =true;
     }
     while(tr.nextPtr!=null)
     {
         if(String.valueOf(tr.value).equals(String.valueOf(key))){  //if the node is present, we break the loop
             prev.nextPtr = next;  //we assign previous node's nextPtr to next node
             isNodepresent = true;
             break;
         }
         prev = tr;   //updating the previous and next pointers
         tr = tr.nextPtr;
         next = tr.nextPtr;
    
     }
     if(isNodepresent==false && String.valueOf(tr.value).equals(String.valueOf(key))){
         prev.nextPtr = null;
         isNodepresent = true;
     }
     if(isNodepresent)
     {
         length--;   //if the node is present, we reduce the length
     }
     else
     {
         System.out.println("The value is not present inside the LinkedList");
     }
    }
    
  3. add(position,value) 函数:

    void add(int position,T value)
    {
     if(position>length+1)   //if the position entered is more than the list length
     {
         System.out.println("Position out of bound");
         return;
     }
     if(position==1){             //if the position is one we'll just
         Node<T> temp = head;
         head = new Node<T>(value);
         head.nextPtr = temp;
         return;
     }
     Node<T> tr = head;
     Node<T> prev = new Node<T>(null);   //creating a new node prev
     while(position-1>0)  //we find the position in the list
     {
         prev = tr;
         tr = tr.nextPtr;
         position--;
     }
     prev.nextPtr = new Node<T>(value);  //update the next pointer of previous node
     prev.nextPtr.nextPtr = tr;
    }
    
  4. getLength() 函数:

    int getLength()
    {
     return this.length;  //returns the length of the list
    }
    
  5. isEmpty() 函数:

    boolean isEmpty()
    {
     if(head == null)  //if the list is empty we return true
         return true;
     else
         return false;
    }
    
  6. clear() 函数:

    void clear()
     {
         head = null;  //make head as null and length as zero
         length = 0;
     }
    
  7. toString() 函数: 在下面的代码中,我们添加并重写了 toString 方法来打印 LinkedList 的内容。

    @Override
    public String toString()
    {
    Node<T> temp = head;
    String str = "{ ";
    if(temp == null)  //if the list is empty
    {
       System.out.println( "list is empty");
    }
    while(temp.nextPtr!=null) //we keep appending data to string till the list is empty
    {
       str += String.valueOf(temp.value) +"->";
       temp = temp.nextPtr;
    }
    str += String.valueOf(temp.value);
    return str+"}";           //we finally return the string
    }
    

带有主类的完整工作代码:

class Node<T>
{
    T value;
    Node<T> nextPtr;

    Node(T value)
    {
        this.value = value;
        this.nextPtr = null;
    }
}

class LinkedList<T>
{
    Node<T> head;
    private int length = 0;

    LinkedList()
    {
        this.head = null;
    }

    void add(T value)
    {
        Node<T> temp = new Node<>(value);

        if(this.head == null)
            head = temp;
        else
        {
            Node<T> tr = head;
            while(tr.nextPtr!=null){
                tr = tr.nextPtr;
            }

            tr.nextPtr = temp;
        }
        length = length + 1;
    }

    void remove(T key)
    {
        Node<T> prev = new Node<>(null);
        prev.nextPtr = head;
        Node<T> next = head.nextPtr;
        Node<T> tr = head;

        boolean isNodepresent = false;

        if(head.value == key ){
            head = head.nextPtr;
            isNodepresent =true;
        }

        while(tr.nextPtr!=null)
        {
            if(String.valueOf(tr.value).equals(String.valueOf(key))){
                prev.nextPtr = next;
                isNodepresent = true;
                break;
            }

            prev = tr;
            tr = tr.nextPtr;
            next = tr.nextPtr;

        }

        if(isNodepresent==false && String.valueOf(tr.value).equals(String.valueOf(key))){
            prev.nextPtr = null;
            isNodepresent = true;
        }

        if(isNodepresent)
        {
            length--;
        }

        else
        {
            System.out.println("The value is not present inside the LinkedList");
        }
    }

    void add(int position,T value)
    {
        if(position>length+1)
        {
            System.out.println("Position out of bound");
            return;
        }

        if(position==1){
            Node<T> temp = head;
            head = new Node<T>(value);
            head.nextPtr = temp;
            return;
        }

        Node<T> tr = head;
        Node<T> prev = new Node<T>(null);

        while(position-1>0)
        {
            prev = tr;
            tr = tr.nextPtr;
            position--;
        }

        prev.nextPtr = new Node<T>(value);
        prev.nextPtr.nextPtr = tr;
    }

    int getLength()
    {
        return this.length;
    }

    boolean isEmpty()
    {
        if(head == null)
            return true;

        else
            return   false;
    }

    void clear()
    {
        head = null;
        length = 0;
    }

    @Override
     public String toString()
    {
      Node<T> temp = head;
      String str = "{ ";
      if(temp == null)
      {
          System.out.println( "list is empty");
      }

      while(temp.nextPtr!=null)
      {
          str += String.valueOf(temp.value) +"->";
          temp = temp.nextPtr;
      }

      str += String.valueOf(temp.value);
      return str+"}";
    }
}


public class Example
{
    public static void main(String[] args) {

       LinkedList<Integer> ll = new LinkedList<>();
       ll.add(1);
       ll.add(2);
       ll.add(3);

       System.out.println(ll);

       ll.remove(3);
       System.out.println(ll);

       ll.add(2,800);
       System.out.println(ll);
    }
}

输出:

{ 1->2->3}
{ 1->2}
{ 1->800->2}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便