Java数据结构与算法_04_链表
链表
- 链表是一种有序列表,有的是带头节点的链表,还有的是不带头节点的链表。根据实际的需求来确定。
- 链表是以节点的方式来存储数据的。
- 链表的一个节点分为data域和next域分别是存放数据的域和存放下一个节点地址的域。
- 链表的各个节点不一定是连续存放的。
代码:(我使用递归实现的)
package org.example;
public class C003 {
public static void main(String[] args) {
LinkedList1 linkedList = new LinkedList1();
linkedList.add("宋江");
linkedList.add("吴用");
linkedList.add("李逵");
linkedList.add("卢俊义");
linkedList.add("张顺");
linkedList.add("鲁智深");
linkedList.add("武松");
linkedList.select();
linkedList.delete("张顺");
linkedList.select();
linkedList.update("李逵", "时迁");
linkedList.select();
}
}
// 单链表
class LinkedList1 {
// 数据
public Object data = null;
// 下一个
public LinkedList1 next = null;
// 添加数据
public void add(Object data) {
// 判断添加的数据是否为空
if (data == null) {
System.out.println("不可以添加空数据!!!");
}
if (data != null) {
// 判断下一个节点是否有数据
if (next != null) {
next.add(data);
}
if (next == null) {
next = new LinkedList1();
next.data = data;
}
}
}
public void delete(Object data) {
if (next == null) {
System.out.println("未找到要删除的元素!!!!");
return;
}
if (!next.data.equals(data)) {
next.delete(data);
}
if (next.data.equals(data)) {
next = next.next;
}
}
public void update(Object index, Object data) {
if (next == null) {
System.out.println("未找到要修改的元素!!!!");
return;
}
if (!next.data.equals(index)) {
next.update(index, data);
}
if (next.data.equals(index)) {
next.data = data;
}
}
public void select(int num) {
System.out.println(num + "\t" + data);
if (next != null) {
next.select(++num);
}
}
public void select() {
int num = 0;
System.out.println(num + "\t" + data);
if (next != null) {
next.select(++num);
}
}
}
代码案例使用循环实现的:
需求:
- 元素以链表的形式添加
- 元素按序号插入对应的位置
- 元素序号重复需要提示添加不成功
- 元素可以遍历
package org.example;
public class C004 {
public static void main(String[] args) {
//进行测试
//先创建节点
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
//创建要给链表
SingleLinkedList singleLinkedList = new SingleLinkedList();
//加入
singleLinkedList.add(hero1);
singleLinkedList.add(hero3);
singleLinkedList.add(hero4);
singleLinkedList.add(hero2);
singleLinkedList.list();
// 测试删除
// singleLinkedList.delete(1);
// singleLinkedList.delete(4);
// System.out.println();
// System.out.println();
// System.out.println();
// System.out.println();
// singleLinkedList.list();
// 测试修改
HeroNode hero31 = new HeroNode(3, "吴用?", "智多※");
singleLinkedList.update(hero31);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
singleLinkedList.list();
}
}
//定义SingleLinkedList 管理我们的英雄
class SingleLinkedList {
//先初始化一个头节点, 头节点不要动, 不存放具体的数据
private HeroNode head = new HeroNode(0, "", "");
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
temp.next = heroNode;
break;
}
if (temp.no < heroNode.no) {
if (temp.next.no == heroNode.no) {
System.out.println("添加失败!!该位置已有成员!!!");
break;
}
if (temp.next.no > heroNode.no) {
heroNode.next = temp.next;
temp.next = heroNode;
break;
}
}
temp = temp.next;
}
}
public void delete(int no) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == no) {
temp.next = temp.next.next;
break;
}
temp = temp.next;
}
}
public void update(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == heroNode.no) {
temp.next.name = heroNode.name;
temp.next.nickname = heroNode.nickname;
break;
}
temp = temp.next;
}
}
public void list() {
System.out.println("\t\t|\t\t序号\t\t|\t\t 姓名 \t\t|\t\t 花名 \t\t|");
System.out.println("\t\t—————————————————————————————————————————————————————————");
HeroNode temp = head;
do {
if (temp.next != null) {
temp = temp.next;
}
// System.out.println(temp);
System.out.printf("\t\t|\t\t%d\t\t|\t\t%s\t\t|\t\t%s\t\t|\n", temp.no, temp.name.length() > 2 ? temp.name : " " + temp.name + " ", temp.nickname);
} while (temp.next != null);
}
}
//定义HeroNode , 每个HeroNode 对象就是一个节点
class HeroNode {
public int no;
public String name;
public String nickname;
public HeroNode next; //指向下一个节点
//构造器
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
//为了显示方法,我们重新toString
@Override
public String toString() {
return "\t\t|\t\t" + no + "\t\t|\t\t" + name + "\t\t|\t\t" + nickname + "\t\t|";
}
}
PS:
- 该笔记为网络课程笔记,课程地址:https://www.bilibili.com/video/BV1E4411H73v/?spm_id_from=333.337.search-card.all.click
- 如有疑问或有错误之处,欢迎讨论指正;