在Java中查找链接的链接列表

时间:2020-02-23 14:34:10  来源:igfitidea点击:

在本教程中,我们将看到如何在Java中查找链接列表的长度。
我们可以显然使用Java链接列表类的大小()方法,但在此处,我们将在实现链接列表时查看如何查找链接列表的长度。
有两种方法可以找到链接的长度:

  • 迭代
  • 递归

迭代:

我们可以迭代下一个节点,直到节点为null并使用计数器计数迭代的数量,并且我们将在最后获取链接列表的长度。

//Find length of linked list using iterative method
public int lengthOfLinkedList()
 {
  Node temp=head;
  int count = 0;
  while(temp!=null)
  {
   temp=temp.next;
   count++;  
  }
  return count;

递归:

我们还可以使用递归来查找链接的长度。
基本情况:如果节点为null,则返回0.这将是我们迭代的基本情况。

//Find length of linked list using recursion
public int lengthOfLinkedList()
 {
  Node temp=head;
  int count = 0;
  while(temp!=null)
  {
   temp=temp.next;
   count++;  
  }
  return count;

Java程序查找LinkedList的长度:

创建名为singlylinkedlist.java的Java文件。

package org.igi.theitroad;
 
class Node {
 public int data;
 public Node next;
 
 public void displayNodeData() {
  System.out.println("{ " + data + " } ");
 }
}
 
public class SinglyLinkedList {
 private Node head;
 
 public boolean isEmpty() {
  return (head == null);
 }
 
 //used to insert a node at the start of linked list
 public void insertFirst(int data) {
  Node newNode = new Node();
  newNode.data = data;
  newNode.next = head;
  head = newNode;
 }
 
//Find length of linked list using iterative method
public int lengthOfLinkedList()
{
   Node temp=head;
   int count = 0;
   while(temp!=null)
   {
  temp=temp.next;
  count++;  
   }
    return count;
}
//Find length of linked list using recursion 
public int lengthOfLinkedListRec(Node head)
 {
   Node temp=head;
   if(temp==null)
   {
      return 0;
    }
   else
   {
      return 1+ lengthOfLinkedListRec(temp.next);
    }
}
 //used to delete node from start of linked list
 public Node deleteFirst() {
  Node temp = head;
  head = head.next;
  return temp;
 }
 
 //used to delete node from start of linked list
 public Node deleteFirst(Node node) {
  Node temp = head;
  head = head.next;
  return temp;
 }
 
 //Use to delete node after particular node
 public void deleteAfter(Node after) {
  Node temp = head;
  while (temp.next != null && temp.data != after.data) {
   temp = temp.next;
  }
  if (temp.next != null)
   temp.next = temp.next.next;
 }
 
 //used to insert a node at the start of linked list
 public void insertLast(int data) {
  Node current = head;
  while (current.next != null) {
   current = current.next; //we'll loop until current.next is null
  }
  Node newNode = new Node();
  newNode.data = data;
  current.next = newNode;
 }
 
 //For printing Linked List
 public void printLinkedList() {
  System.out.println("Printing LinkedList (head --> last) ");
  Node current = head;
  while (current != null) {
   current.displayNodeData();
   current = current.next;
  }
  System.out.println();
 }
}

让我们创建名为linkedlistmain.java的主类来创建linkedlist。

package org.igi.theitroad;
 
public class LinkedListMain {
 
 public static void main(String args[])
 {
   SinglyLinkedList myLinkedlist = new SinglyLinkedList();
   myLinkedlist.insertFirst(5);
   myLinkedlist.insertFirst(6);
   myLinkedlist.insertFirst(7);
   myLinkedlist.insertFirst(1);
   myLinkedlist.insertLast(2);
   myLinkedlist.printLinkedList();
   //Linked list will be 
   //2 -> 1 ->  7 -> 6 -> 5
   System.out.println("Length of Linked List using iteration: "+myLinkedlist.lengthOfLinkedList());
   System.out.println("Length of Linked List Using recursion: "+myLinkedlist.lengthOfLinkedListRec(myLinkedlist.getHead()));
 }
}

运行上面的程序时,我们将得到以下输出:

Printing LinkedList (head --> last) 
{ 1 } 
{ 7 } 
{ 6 } 
{ 5 } 
{ 2 } 
Length of Linked List using iteration: 5
Length of Linked List Using recursion: 5