当前位置: 首页 > news >正文

LinkedList和单双链表。

java中提供了双向链表的动态数据结构 --- LinkedList,它同时也实现了List接口,可以当作普通的列表来使用。也可以自定义实现链表。

单向链表:一个节点=本节点数据+下个节点地址

给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。


public class Lee {private Node head1;private Node head2;Lee(){this.head1= new Node(0);this.head2= new Node(0);}public void  insert1(int data){Node newNode = new Node(data);Node curNode = head1;while (curNode.next!=null){curNode=curNode.next;}curNode.next=newNode;}public void  insert2(int data){Node newNode = new Node(data);Node curNode = head2;while (curNode.next!=null){curNode=curNode.next;}curNode.next=newNode;}static class Node{public int value;public Node next;Node(int data){this.value=data;this.next=null;}}public void Plink(Node head1,Node head2){while (head1!=null&&head2!=null){if (head1.value<head2.value)head1=head1.next;else if (head1.value>head2.value)head2=head2.next;else{System.out.println(head1.value+" ");head1=head1.next;head2=head2.next;}}}public static void main(String[] args) {Lee lee = new Lee();lee.insert1(1);lee.insert1(3);lee.insert1(4);lee.insert2(4);lee.insert2(5);lee.insert2(6);lee.Plink(lee.head1, lee.head2);}
}

双向链表:一个节点=上个节点地址+本节点数据+下个节点地址

如:定义两个函数,实现在双向链表的头部及尾部插入节点


public class Lee {private Node head;Lee(){this.head= new Node(0);}public void  insertHead(int data){Node newNode = new Node(data);newNode.next=head;head.pre=newNode;head=newNode;}public void  insertTail(int data){Node newNode = new Node(data);Node current = head;while (current.next!=null){current=current.next;}current.next=newNode;newNode.pre=current;}public void printList(Node head) {Node current = head;// 从头到尾打印链表while (current != null) {System.out.print(current.value + " -> ");current = current.next;}System.out.println("null"); // 表示链表结尾}static class Node{public int value;public Node pre;public Node next;Node(int data){this.value=data;this.pre=null;this.next=null;}}public static void main(String[] args) {Lee lee = new Lee();lee.insertTail(2);lee.insertTail(3);lee.insertTail(4);lee.insertHead(4);lee.printList(lee.head);}
}

http://www.lryc.cn/news/480914.html

相关文章:

  • AI与OCR:数字档案馆图像扫描与文字识别技术实现与项目案例
  • Spring boot 读模块项目升级为spring cloud 项目步骤以及问题
  • 时序数据库之influxdb和倒排索引以及LSM-TREE
  • 如何避免消息的重复消费问题?(消息消费时的幂等性)
  • 【Java SE】类与对象
  • 基于springboot的公益服务平台的设计与实现
  • Tomcat(6) 什么是Servlet容器?
  • 用js去除变量里的html标签
  • Vue3+element-plus摘要
  • Android Studio 将项目打包成apk文件
  • 贪心算法day2(最长递增子序列)
  • arcgis pro 学习笔记
  • OpenGL 进阶系列06 - OpenGL变换反馈(TransformFeedback)
  • elementUI 点击弹出时间 date-picker
  • 【浙江大学大模型系列】启真医疗大模型(国内大模型)
  • NestJS 项目中如何使用 class-validator 进行数据验证
  • 【AI抠图整合包及教程】Meta SAM2:引领图像和视频分割技术的新纪元
  • 小菜家教平台(三):基于SpringBoot+Vue打造一站式学习管理系统
  • ArcGIS/QGIS按掩膜提取或栅格裁剪后栅格数据的值为什么变了?
  • Linux的基本指令(一)
  • python导入包失败 in <module> import pandas as pd
  • 不惧风雨,硬核防护!雷孜LaCie小金刚三防移动硬盘颠覆认知
  • Yocto 项目下通过网络更新内核、设备树及模块
  • Scheduled Sampling工作原理【小白记笔记】
  • C++:C++的IO流
  • 「QT」几何数据类 之 QLine 整型直线类
  • day58 图论章节刷题Part09(dijkstra(堆优化版)、Bellman_ford 算法)
  • 【计网不挂科】计算机网络期末考试——【选择题&填空题&判断题&简述题】试卷(1)
  • 智能出行助手:SpringBoot共享汽车管理平台
  • 【月之暗面kimi-注册/登录安全分析报告】