search
HomeJavajavaTutorialJava implements singly linked list and doubly linked list

The examples in this article share with you the relevant codes for implementing singly linked lists and doubly linked lists in java for your reference. The specific contents are as follows

implementation of singly linked lists in java:

package code;
 
class Node
{
 Node next;
 int data;
 public Node(int data)
 {
  this.data=data;
 }
  
}
class LinkList
{
 Node first;
 //头部
 public LinkList()
 {
  this.first=null;
 }
 public void addNode(Node no)
 {
  no.next=first;
  first=no;//在头部添加
 }
 public void delectNode()
 {
  Node n=first.next;
  first=null;
  first=n;//在头部删除
 }
 //删除指定位置
 public int Number()
 {
  int count=1;
  //查看有多少元素
  Node nd=first;
  while(nd.next!=null)
  {
   nd=nd.next;
   count++;
  }
  return count;
 }
 public void delectExact(int n)
 {
  //删除指定位置
  if(n>1)
  {
   int count=1;
   Node de=first;
   while(count<n-1)
   {
    de=de.next;
    count++;
     
   }
   de.next=de.next.next;
  }
  else
   first=first.next;
   
 }
 public void addExact(int n,Node nd)
 {
  if(n>1)//添加指定位置
  {
   int count=1;
   Node de=first;
   while(count<n-1)
   {
    de=de.next;
    count++;
     
   }
   nd.next=de.next;
   de.next=nd;
 
  }
  else
   first=first.next;
 }
 public int findNode(int n)
 {
  int count=1;//查找一个数对应的位置
  Node de=first;
  while(de.data!=n)
  {
   de=de.next;
   count++;
   if(de==null)
   {
    return -1;
   }
  }
  return count;
 }
 public void print()
 {
  Node no=first;//打印所有
  while(no!=null)
  {
   System.out.println(no.data);
   no=no.next;
  }
 }
}
public class TextNode
{
 public static void main(String[] args)
 {
  LinkList ll=new LinkList();
  ll.addNode(new Node(12));
  ll.addNode(new Node(15));
  ll.addNode(new Node(18));
  ll.addNode(new Node(19));
  ll.addNode(new Node(20));
  /*System.out.println(ll.first.data);
 
  ll.delectNode();
  System.out.println(ll.first.data);*/
  System.out.println(ll.Number());
  ll.delectExact(3);
  ll.addExact(3, new Node(100));
  System.out.println(ll.Number());
//  ll.print();
  System.out.println(ll.findNode(112));
   
 }
}

implementation of doubly linked lists in java:

public class DoubleLink
{
 public static void main(String[]args)
 {
  Node2 no=new Node2(5);
  no.addLeft(new Node2(6));
  no.addRight(new Node2(7));
  /*no.print();
  no.print2();*/
  no.addExact2(1, new Node2(8));
  no.print();
  System.out.println("--------------");
  no.print2();
 }
}
class Node2
{
 public Node2 first;
 public Node2 end;
 public Node2 left;
 public Node2 right;
 int data=0;
 public Node2(int n)
 {
   
  first=this;
  end=this;
   
  first.data=n;
 }
 //从头部添加
 public void addLeft(Node2 before)
 {
  first.left=before;
  before.right=first;
  first=before;
 }
 //从尾部添加
 public void addRight(Node2 after)
 {
  end.right=after;
  after.left=end;
  end=after;
 }
 //插入正数(第三声)的第几个
 public void addExact(int n,Node2 no)
 {
  int count=0;
  if(n==0)
  {
   addLeft(no);
  }
  else
  { 
   Node2 f=first;
   while(true)
   {
    f=f.right;
    count++;
    if(count==n)
    {
     //此处为四个指针的指向的变化
     no.left=f.left;
     f.left.right=no;
 //    first.left=no;
     no.right=f;
     f.left=no;
     break;
    }
  
   }
  }
 }
 //插入倒数的第几个
 public void addExact2(int n,Node2 no)
 {
  int count=0;
  if(n==0)
  {
   addRight(no);
  }
  else
  {
   Node2 f=end;
   while(true)
   {
    f=f.left;
    count++;
    if(count==n)
    {
      
     no.left=f;
     no.right=f.right;
     f.right.left=no;
     f.right=no;
     break;
      
    }
   }
  }
 }
 //正序遍历
 public void print()
 {
  System.out.println(first.data);
  while(first.right!=null)
  {
   System.out.println(first.right.data);
   first=first.right;
  }
//  System.out.println(end.data);
 }
 //倒序遍历
 public void print2()
 {
  System.out.println(end.data);
  while(end.left!=null)
  {
   System.out.println(end.left.data);
   end=end.left;
  }
 }
  
 
}
/*值得注意的是,每一次插入一个新的对象的时候,需要注意指针指向的改变。
首先是这个新的对象两边的指向(左和右),其次是时左边的对象向右的指向
和右边对象向左的指向。
这四个指针的指向必须正确,否则可能导致正序或者倒序遍历无法实现。
*/
/*对比单链表,单链表只能从一个方向遍历,因为只有一个头,而双向链表,有头和尾,可以从
 * 头遍历,也可以从尾遍历,而且其中一个对象因为有两个方向的指针,所以他可以获得左边的
 * 对象也可以获得右边的对象。
 * 但是单链表的话,因为只有一个方向,所以只能向左或右。添加对象的时候,双向也可以从头添加,也可以从尾添加。
 * 如果单链表要实现两个方向添加比较难得,或者说不行,因为他只有向左或向右的一个方向的指针
 * 而双向链表每个对象都有两个方向的指针没这样更灵活,但是这同样有缺点,因为这样的话每个对象
 * 都会包含两个指针,这同样内存会消耗更多。
 * 
 * */

The above is the entire content of this article. I hope it will be helpful to everyone in learning java programming.

For more articles related to Java implementation of singly linked lists and doubly linked lists, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What aspects of Java development are platform-dependent?What aspects of Java development are platform-dependent?Apr 26, 2025 am 12:19 AM

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Are there performance differences when running Java code on different platforms? Why?Are there performance differences when running Java code on different platforms? Why?Apr 26, 2025 am 12:15 AM

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

What are some limitations of Java's platform independence?What are some limitations of Java's platform independence?Apr 26, 2025 am 12:10 AM

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"

Explain the difference between platform independence and cross-platform development.Explain the difference between platform independence and cross-platform development.Apr 26, 2025 am 12:08 AM

Platformindependenceallowsprogramstorunonanyplatformwithoutmodification,whilecross-platformdevelopmentrequiressomeplatform-specificadjustments.Platformindependence,exemplifiedbyJava,enablesuniversalexecutionbutmaycompromiseperformance.Cross-platformd

How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?How does Just-In-Time (JIT) compilation affect Java's performance and platform independence?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

Why is Java a popular choice for developing cross-platform desktop applications?Why is Java a popular choice for developing cross-platform desktop applications?Apr 25, 2025 am 12:23 AM

Javaispopularforcross-platformdesktopapplicationsduetoits"WriteOnce,RunAnywhere"philosophy.1)ItusesbytecodethatrunsonanyJVM-equippedplatform.2)LibrarieslikeSwingandJavaFXhelpcreatenative-lookingUIs.3)Itsextensivestandardlibrarysupportscompr

Discuss situations where writing platform-specific code in Java might be necessary.Discuss situations where writing platform-specific code in Java might be necessary.Apr 25, 2025 am 12:22 AM

Reasons for writing platform-specific code in Java include access to specific operating system features, interacting with specific hardware, and optimizing performance. 1) Use JNA or JNI to access the Windows registry; 2) Interact with Linux-specific hardware drivers through JNI; 3) Use Metal to optimize gaming performance on macOS through JNI. Nevertheless, writing platform-specific code can affect the portability of the code, increase complexity, and potentially pose performance overhead and security risks.

What are the future trends in Java development that relate to platform independence?What are the future trends in Java development that relate to platform independence?Apr 25, 2025 am 12:12 AM

Java will further enhance platform independence through cloud-native applications, multi-platform deployment and cross-language interoperability. 1) Cloud native applications will use GraalVM and Quarkus to increase startup speed. 2) Java will be extended to embedded devices, mobile devices and quantum computers. 3) Through GraalVM, Java will seamlessly integrate with languages ​​such as Python and JavaScript to enhance cross-language interoperability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools