链表(Linked List)
链表是有序的列表,但是它在内存中是存储如下的:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w5a2WQqd-1669549256447)(https://gitee.com/lmr0226/imgs/raw/master/202211271723841.png)]
小结:
- 链式是以节点的方式来存储的
- 每个节点包含一个data域,next域:指向下一个节点
- 链表的各个节点不一定是连续的
- 链表分为带头节点和没有头结点的链表,根据实际的需求来确定
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a4t2A50M-1669549256448)(https://gitee.com/lmr0226/imgs/raw/master/202211271734922.png)]
代码实现
- 添加数据
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WfBcsGGI-1669549256449)(https://gitee.com/lmr0226/imgs/raw/master/202211271903130.png)]
- 根据编号的排名进行插入操作
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SFbSUL6j-1669549256450)(https://gitee.com/lmr0226/imgs/raw/master/202211271917133.png)]
- 修改单链表的修改
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N6hlVEpj-1669549256451)(https://gitee.com/lmr0226/imgs/raw/master/202211271924893.png)]
- 删除节点
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HrtukKnR-1669549256453)(https://gitee.com/lmr0226/imgs/raw/master/202211271932809.png)]
代码
package com.wiselee.linkedlist;
/**
* @PROJECT_NAME: DataStruct
* @DESCRIPTION:
* @USER: 28416
* @DATE: 2022/11/27 17:40
* 单向链表
*/
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
// singleLinkedList.add(heroNode1);
// singleLinkedList.add(heroNode2);
// singleLinkedList.add(heroNode3);
// singleLinkedList.add(heroNode4);
//加入是按编号的顺序
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode4);
singleLinkedList.addByOrder(heroNode2);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList.list();
HeroNode newHead = new HeroNode(2, "小路", "玉麒麟~~~");
singleLinkedList.update(newHead);
singleLinkedList.list();
int res = singleLinkedList.deleteNode(1);
System.out.println(res);
singleLinkedList.list();
//
}
}
//定义SingleLinkedList 来管理我们的英雄
class SingleLinkedList{
private HeroNode head = new HeroNode(0,"","");//不存放具体的数据
/**
* 添加链表
* @param heroNode
*/
//添加节点到单向链表
public void add(HeroNode heroNode){
HeroNode tem = head;
while (true){
if (tem.next == null){
break;
}
//如果没有找到最后,就将tem后移
tem = tem.next;
}
tem.next = heroNode;
}
/**
* 遍历链表
*/
public void list(){
if (head.next == null){
System.out.println("链表为空");
return;
}
//因为头结点不能动
HeroNode tem = head.next;
while (true){
if (tem == null){
break;
}
System.out.println(tem);
tem = tem.next;
}
}
/**
* 根据编号的顺序进行插入
* @param heroNode
*/
public void addByOrder(HeroNode heroNode){
HeroNode tem = head;
boolean flag = false;
while (true){
if (tem.next == null){//说明已经在链表的最后
break;
}
if (tem.next.no > heroNode.no){
break;
}else if(tem.next.no == heroNode.no){
flag = true;//说明标号存在
break;
}
tem = tem.next;
}
if (flag) {
System.out.printf("准备插入的英雄编号%d已经存在了,不能再添加了\n",heroNode.no);
return;//不能添加,说明标号已经存在
}else {
heroNode.next = tem.next;
tem.next = heroNode;
}
}
/**
* 更新节点的信息
* @param newheroNode
*/
public void update(HeroNode newheroNode){
if (head.next == null){
System.out.println("链表为空");
return;
}
//定义一个辅助变量
HeroNode temp = head.next;
boolean flag = false;//表示是否找到该节点
while (true){
if (temp == null){
break;//遍历完链表
}
if (temp.no == newheroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newheroNode.name;
temp.nickName = newheroNode.nickName;
}else {
System.out.printf("没有找到编号为%d\n",newheroNode.no);
}
}
public int deleteNode(int n ){
HeroNode temp = head;
boolean flag = false;
while (true){
if (temp.next == null){
break;
}
if (temp.next.no == n){
flag =true;
break;
}
temp =temp.next;
}
if (flag) {
temp.next = temp.next.next;
return 1;
}else {
System.out.printf("我们要删除的编号%d不存在\n",n);
return -1;
}
}
}
//定义HeroNode,每个HeroNode对象就是一个节点
class HeroNode{
public int no;
public String name;
public String nickName;
public HeroNode next;//指向下一个节点
public HeroNode(int no, String hName,String hNickName){
this.no = no;
this.name = hName;
this.nickName = hNickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
源码链接:https://gitee.com/lmr0226/DataStruct/blob/master/src/com/wiselee/linkedlist/SingleLinkedListDemo.java