java实现单链表、双向链表

 更新时间:2021年08月01日 11:01:54   作者:New_Null  
这篇文章主要为大家详细介绍了java实现单链表、双向链表的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现单链表、双向链表的相关代码,供大家参考,具体内容如下

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));
 
 }
}

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;
 }
 }
 

}
/*值得注意的是,每一次插入一个新的对象的时候,需要注意指针指向的改变。
首先是这个新的对象两边的指向(左和右),其次是时左边的对象向右的指向
和右边对象向左的指向。
这四个指针的指向必须正确,否则可能导致正序或者倒序遍历无法实现。
*/
/*对比单链表,单链表只能从一个方向遍历,因为只有一个头,而双向链表,有头和尾,可以从
 * 头遍历,也可以从尾遍历,而且其中一个对象因为有两个方向的指针,所以他可以获得左边的
 * 对象也可以获得右边的对象。
 * 但是单链表的话,因为只有一个方向,所以只能向左或右。添加对象的时候,双向也可以从头添加,也可以从尾添加。
 * 如果单链表要实现两个方向添加比较难得,或者说不行,因为他只有向左或向右的一个方向的指针
 * 而双向链表每个对象都有两个方向的指针没这样更灵活,但是这同样有缺点,因为这样的话每个对象
 * 都会包含两个指针,这同样内存会消耗更多。
 * 
 * */

以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。

相关文章

  • SpringBoot实现缓存组件配置动态切换的步骤详解

    SpringBoot实现缓存组件配置动态切换的步骤详解

    现在有多个springboot项目,但是不同的项目中使用的缓存组件是不一样的,有的项目使用redis,有的项目使用ctgcache,现在需要用同一套代码通过配置开关,在不同的项目中切换这两种缓存,本文介绍了SpringBoot实现缓存组件配置动态切换的步骤,需要的朋友可以参考下
    2024-07-07
  • EL表达式简介_动力节点Java学院整理

    EL表达式简介_动力节点Java学院整理

    EL全名为Expression Language,这篇文章主要给大家介绍EL表达式的主要作用及内容简介,感兴趣的朋友一起看看
    2017-07-07
  • idea2020最新版配置maven的方法

    idea2020最新版配置maven的方法

    这篇文章主要介绍了idea2020最新版配置maven的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Springmvc自定义参数转换实现代码解析

    Springmvc自定义参数转换实现代码解析

    这篇文章主要介绍了Springmvc自定义参数转换实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringMVC中的@RequestMapping注解解析

    SpringMVC中的@RequestMapping注解解析

    这篇文章主要介绍了SpringMVC中的@RequestMapping注解解析,SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些 URL 请求,在控制器的类定义及方法定义处都可标注@RequestMapping,需要的朋友可以参考下
    2023-12-12
  • Spring MVC打印@RequestBody、@Response日志的方法

    Spring MVC打印@RequestBody、@Response日志的方法

    这篇文章主要介绍了Spring MVC打印@RequestBody、@Response日志的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Sleuth(Micrometer)+ZipKin分布式链路问题小结

    Sleuth(Micrometer)+ZipKin分布式链路问题小结

    在微服务架构中,分布式链路追踪技术成为了解决系统复杂调用问题的关键,本文介绍了其他链路追踪方案,如Cat、Pinpoint和Skywalking,展示了分布式链路追踪技术的多样化,感兴趣的朋友一起看看吧
    2024-10-10
  • 浅谈spring-boot-rabbitmq动态管理的方法

    浅谈spring-boot-rabbitmq动态管理的方法

    这篇文章主要介绍了浅谈spring-boot-rabbitmq动态管理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • IDEA 端口占用的解决方法(推荐)

    IDEA 端口占用的解决方法(推荐)

    这篇文章主要介绍了IDEA 端口占用的解决方法,本文通过两种方法给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • SpringBoot项目中配置application.yml中server.port不生效的问题

    SpringBoot项目中配置application.yml中server.port不生效的问题

    这篇文章主要介绍了SpringBoot项目中配置application.yml中server.port不生效的问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论