Java 程序实现 LinkedList

javacampus interviewserver side programmingprogramming

在本文中,我们将了解如何实现链表。java.util.LinkedList 类操作执行的操作与双向链表相同。索引到列表中的操作将从开头或结尾遍历列表,以更接近指定索引为准。

下面是相同的演示 −

假设我们的输入是

Run the program

期望的输出将是

The elements of the linked list are:
100 150 200 250

算法

步骤 1 - 开始
步骤 2 - 创建具有所需成员的类。
步骤 3 - 定义 ‘insert’ 函数以将元素添加到列表中。
步骤 4 - 在 ‘main’ 方法中,创建该类的新实例。
步骤 5 - 创建一个列表,并使用 ‘insert’ 方法向其中添加元素。
步骤 6 - 遍历列表,并显示当前节点中的值。
步骤 7 - 移动到下一个节点并执行相同操作。
步骤 8 - 执行此操作,直到到达列表末尾。
步骤 9 - 显示结果
步骤 10 - 停止

示例 1

在这里,我们将所有操作都绑定在‘main’函数下。

public class Demo {
   Node head;
   static class Node {
      int data;
      Node next_element;
      Node(int element){
         data = element;
         next_element = null;
      }
   }
   public static Demo insert(Demo input_list, int data){
      Node new_node = new Node(data);
      new_node.next_element = null;
      if (input_list.head == null) {
         input_list.head = new_node;
      }
      else {
         Node last = input_list.head;
         while (last.next_element != null) {
            last = last.next_element;
         }
         last.next_element = new_node;
      }
      return input_list;
   }
   public static void main(String[] args){
      Demo input_list = new Demo();
      System.out.print("A linked list is declared: \n");
      input_list = insert(input_list, 100);
      input_list = insert(input_list, 150);
      input_list = insert(input_list, 200);
      input_list = insert(input_list, 250);
      Node current_node = input_list.head;
      System.out.print("The elements of the linked list are: \n");
      while (current_node != null) {
         System.out.print(current_node.data + " ");
         current_node = current_node.next_element;
      }
   }
}

输出

A linked list is declared:
The elements of the linked list are:
100 150 200 250

示例 2

在这里,我们将操作封装成展现面向对象编程的函数。

public class Demo {
   Node head;
   static class Node {
      int data;
      Node next_element;
      Node(int element){
         data = element;
         next_element = null;
      }
   }
   public static Demo insert(Demo input_list, int data){
      Node new_node = new Node(data);
      new_node.next_element = null;
      if (input_list.head == null) {
         input_list.head = new_node;
      }
      else {
         Node last = input_list.head;
         while (last.next_element != null) {
            last = last.next_element;
         }
         last.next_element = new_node;
      }
      return input_list;
   }
   public static void print_list(Demo input_list){
      Node current_node = input_list.head;
      System.out.print("The elements of the linked list are: \n");
      while (current_node != null) {
         System.out.print(current_node.data + " ");
         current_node = current_node.next_element;
      }
   }
   public static void main(String[] args){
      Demo input_list = new Demo();
      System.out.print("A linked list is declared: \n");
      input_list = insert(input_list, 100);
      input_list = insert(input_list, 150);
      input_list = insert(input_list, 200);
      input_list = insert(input_list, 250);
      print_list(input_list);
   }
}

输出

A linked list is declared:
The elements of the linked list are:
100 150 200 250

相关文章