在Java中使用关联列表的介绍

140 阅读7分钟

数据结构使用不同的预定义方法来存储、检索和删除数据,这最终导致了高效程序的产生。链接列表是一种流行的数据结构,它由一个连接(或链接)的节点列表组成。

但你如何在Java中创建一个链表呢?让我们来看一看。

链接表是如何工作的?

每个链接列表都以一个特殊的节点开始,这个节点通常被称为 "头",它负责在任何时候都指向列表的起点。头部很重要,因为链接列表中的每个节点不需要在物理上跟随其后续节点(意味着前任和后任不需要在物理上相邻)。

像每一个数据结构一样,链表通过一组预定义的函数来促进创建、检索、插入和销毁,这些函数可以被任何开发人员使用。

在Java中创建一个链接列表

一个旨在创建和操作链表的Java程序将有三个不同的部分;节点类、链表类和驱动器。尽管这三个部分可以合并在一个文件中,但在计算机科学中,有一个被称为 "关注点分离 "的设计原则,每个开发者都应该知道。

关注点分离原则规定,解决特定关注点的每一节代码都应该被分开。这一原则将帮助你创建更干净(更可读)的代码,是创建数据结构的理想选择。

在Java中创建一个链表的第一步是创建一个节点类。一个节点类应该有两个属性;其中一个属性将代表节点的数据部分,而另一个属性将代表链接部分。一个节点类还应该有一个构造函数、获取器和设置器。

相关的。学习如何在Java中创建类

获取器和设置器将允许其他类(如链接列表类)访问链接列表中的各个节点。

节点类实例

下面是一个节点类的例子,让你了解我们的意思。

public class Node {       private int Data;       private Node NextNode;       //constructor       public Node() {              Data = 0;              NextNode = null;       }       //getters and setters       public int getData() {              return Data;       }       public void setData(int data) {              Data = data;       }       public Node getNextNode() {              return NextNode;       }       public void setNextNode(Node nextNode) {              NextNode = nextNode;       }}

在这个例子中,数据属性将存储整数值。现在你已经有了节点类,是时候转向链接列表了。

链表实例

下面是一个Java中的链接列表的例子。

public class LinkedList {       private Node Head;       //constructor       public LinkedList() {              Head = null;       }}

上面的代码将创建一个链表类,然而,如果没有它的各种操作,这个类可以被看作是相当于一个空壳。链接列表的数据结构有几个操作可以用来填充它。

  • 在前面插入。
  • 在中间插入。
  • 在后面插入。

相关的。如何用JavaScript ES6类构建数据结构

链接列表的插入方法集合是开发者可能选择使用这种数据结构而不是另一种数据结构如堆栈(只允许从顶部插入和删除)的原因之一。

使用前面的插入方法

前面插入方法,顾名思义,在链表的前面插入新数据(或新节点)。

在前面插入方法的例子

下面是一个如何在列表前面插入新数据的例子。

 //insert node at front method       public void insertAtFront(int key) {              //create a new node using the node class              Node Temp = new Node();              //check if the Temp node was successfully created              //assign the data that was provides by the user to it              if(Temp != null) {                     Temp.setData(key);                     Temp.setNextNode(null);                                          //check if the head of the linked list is empty                     //assign the node that was just created to the head position                     if(Head == null) {                           Head = Temp;                     }                     //if a node is already at the head position                    //add the new node to it and set it as the head                     else {                         Temp.setNextNode(Head);                           Head = Temp;                     }              }       }

上面例子中的insertAtFront 方法允许用户在给定的链接列表中添加新节点。

应用在前面插入的例子

下面是一个关于如何在前面插入的例子。

public class Driver {       //executes the program       public static void main(String[] args) {            //create a new linked list called List             LinkedList List = new LinkedList();              //add each value to the front of the linked list as a new node              List.insertAtFront(10);              List.insertAtFront(8);              List.insertAtFront(6);              List.insertAtFront(4);              List.insertAtFront(2);     }}

驱动类(这是通常分配给Java中可执行类的名称),利用LinkedList类来创建一个包含五个偶数的链表。看一下上面的代码,应该很容易看出数字 "2 "在链表的头部位置。但是,你怎么能确认这一点呢?

使用显示所有节点的方法

显示所有节点的方法是一个重要的链表方法。没有它,开发者将无法看到链表中的节点。它穿过链表(从头部开始),打印出存储在形成链表的每个节点中的数据。

显示所有节点方法示例

下面是一个在 Java 中使用 display all notes 方法的例子。

//display all nodes method       public void displayAllNodes() {              //create a new node call Temp and assign it to the head of the linked list              //if the head has a null value then the linked list is empty              Node Temp = Head;              if (Head == null){                     System.out.println("The list is empty.");                     return;              }              System.out.println("The List:");              while(Temp != null) {                     //print the data in each node to the console(starting from the head)                     System.out.print(Temp.getData() + " ");                     Temp = Temp.getNextNode();              }                   }

现在,displayAllNodes方法已经被添加到LinkedList类中,你可以通过在驱动类中添加一行代码来查看链接列表。

使用显示所有节点方法的例子

下面,你将看到如何使用显示所有节点的方法。

//print the nodes in a linked list              List.displayAllNodes();       

执行上面的一行代码将在控制台中产生以下输出。

列表。

2 4 6 8 10

使用查找节点方法

在某些情况下,用户会想在一个链接列表中找到一个特定的节点。

例如,对于一家拥有数百万客户的银行来说,当他们只需要看到一个特定客户的细节时,打印他们数据库中的所有客户是不现实的。

因此,与其使用displayAllNodes方法,更有效的方法是找到包含所需数据的单一节点。这就是为什么在链表数据结构中,寻找单一节点方法很重要。

查找节点方法实例

下面是一个使用查找节点方法的例子。

//search for a single node using a key       public boolean findNode(int key) {              //create a new node and place it at the head of the linked list              Node Temp = Head;              //while the current node is not empty              //check if its data matches the key provided by the user              while (Temp != null) {                     if (Temp.getData() == key) {                     System.out.println("The node is in the list");                      return true;                     }                     //move to the next node                     Temp = Temp.getNextNode();              }              //if the key was not found in the linked list              System.out.println("The node is not in the list");                  return false;       }

通过displayAllNodes方法,你确认了LinkedList包含5个从2到10的偶数。上面的findNode例子可以确认这些偶数中是否有一个是数字4,只需在驱动类中调用该方法并提供数字作为参数。

使用查找节点方法的例子

下面是一个如何在实践中使用查找节点方法的例子。

//check if a node is in the linked list              List.findNode(4);

上面的代码将在控制台中产生以下输出。

The node is in the list

使用删除一个节点的方法

使用上面那个银行的例子,银行数据库中的一个客户可能希望关闭他们的账户。这时,删除一个节点的方法就很有用了。这是最复杂的链表方法。

删除一个节点方法搜索一个给定的节点,删除该节点,并将前一个节点链接到被删除的节点后面。

删除一个节点方法的例子

下面是一个删除节点方法的例子。

public void findAndDelete(int key) { Node Temp = Head; Node prev = null; //check if the head node holds the data //and delete it if (Temp != null && Temp.getData() == key) { Head = Temp.getNextNode(); return; } //search the other nodes in the list //and delete it while (Temp != null) { if (Temp.getNextNode().getData() == key ) { prev = Temp.getNextNode().getNextNode(); Temp.setNextNode(prev); return; } Temp = Temp.getNextNode(); } } 

使用删除一个节点方法的例子

下面是一个在实践中使用删除节点方法的例子。

//delete the node that holds the data 4List.findAndDelete(4);//print all nodes in the linked listList.displayAllNodes();

在预先存在的Driver类中使用上述两行代码将在控制台中产生以下输出。

The List:2 6 8 10

现在你可以在Java中创建关联列表了

如果你能坚持到这篇教程文章的结尾,你将学会。

  • 如何创建一个节点类。
  • 如何创建一个链表类。
  • 如何用预定义的方法来填充一个链表类。
  • 如何创建一个驱动类并使用不同的链表方法来实现所需的结果。

链表只是许多数据结构中的一个,你可以用它来存储、检索和删除数据。既然你已经得到了开始所需的一切,为什么不自己用Java试试这些例子呢?