java实现单链表、双向链表
作者:New_Null 发布时间:2023-02-09 03:15:59
标签: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;
}
}
}
/*值得注意的是,每一次插入一个新的对象的时候,需要注意指针指向的改变。
首先是这个新的对象两边的指向(左和右),其次是时左边的对象向右的指向
和右边对象向左的指向。
这四个指针的指向必须正确,否则可能导致正序或者倒序遍历无法实现。
*/
/*对比单链表,单链表只能从一个方向遍历,因为只有一个头,而双向链表,有头和尾,可以从
* 头遍历,也可以从尾遍历,而且其中一个对象因为有两个方向的指针,所以他可以获得左边的
* 对象也可以获得右边的对象。
* 但是单链表的话,因为只有一个方向,所以只能向左或右。添加对象的时候,双向也可以从头添加,也可以从尾添加。
* 如果单链表要实现两个方向添加比较难得,或者说不行,因为他只有向左或向右的一个方向的指针
* 而双向链表每个对象都有两个方向的指针没这样更灵活,但是这同样有缺点,因为这样的话每个对象
* 都会包含两个指针,这同样内存会消耗更多。
*
* */
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- Kotlin开发Android应用实例详解我们简单的知道了Kotlin这门新语言的优势,也接触了一些常见的语法及其简单的使用,相信你会对它有
- 在使用Gateway 调用一个文件上传服务时 前端传来的File的base64字符串怎么都接受不到 但是用Body方式请求就能接收到后来经过
- 免责声明:本教程所有资源均来源于网络;仅用于学习交流,请勿用于任何商业行为;如需要,请使用正版授权;侵权联删。推荐最新 IntelliJ I
- 其实这个表示有点不太对,应该是 Druid 动态切换数据源的方法,只是应用在了 springboot 框架中,准备代码准备了半天,之前在一次
- 为什么要写这篇文章经过了若干年的发展,Java逐步从java8升级为java11,java17。让我们对比学习一下最新一版的LTS版本和ja
- GET请求不支持对象传参问题@GetMapping("/getByParam")String hello(Student
- 有人问我,怎么判断一个点是不是在多边形内,本来想着把这个多边形分成一个又一个三角形,如图, 然后判断这个点是不是在某个三角形中,如
- 配置不生效的解决办法注意:如果配置不生效,则说明spring优先加载了其他配置:解决办法:添加启动参数 -Dlogging.config=c
- 问题原因Springboot get请求是参数过长抛出异常:Request header is too large 的问题错误描述java.
- 使用IDEA开发微服务项目,需要启动多个微服务,可以开启IDEA的Run DashBoard窗口,需要对IDEA中指定工程的父工程进行配置进
- 在我们日常开发过程中,通常会涉及到数据权限问题,下面以我们常见的一种场景举例:一个公司有很多部门,每个人所处的部门和角色也不同,所以数据权限
- ArrayList和LinkedList区别、扩容机制及底层实现ArrayListArrayList的底层实现为数组存储在内存中,线程不同步
- 什么是JMMJMM全称Java Memory Model, 中文翻译Java内存模型,一种符合内存模型规范的,屏蔽了各种硬件和操作系统的访问
- Java为什么不浪(long)学而时习之不亦说乎,继续温习Java。今天使用switch时,不小心写了如下代码,报错如下。 public s
- 近日于LeetCode看题遇1114 按序打印,获悉一解法使用了Semaphore,顺势研究,记心得于此。此解视Semaphore为锁,以保
- URL(Uniform Resource Locator)是统一资源 * ,它是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,
- 为什么要重复造轮子你可能会问,Spring已经自带了全局异常拦截,为什么还要重复造轮子呢?这是个好问题,我觉得有以下几个原因装逼Spring
- 1.editplus1.1 官方下载https://www.editplus.com/官方下载最新的64位2 .解压就可以使用2.1 vsc
- 具体可见http://developer.android.com/tools/debugging/ddms.html。 DDMS为IDE和e
- 这篇文章介绍了Java+Nginx实现POP、IMAP、SMTP邮箱代理服务,我们本次使用的环境为Centos7下,java程序我们通过ec