java带头结点的单链表_【Java数据结构】带头节点的单链表的增删改查

/**

* @author ZhiYi Li

* @create 2020/8/25 11:37

* 带头节点的单链表

* 头节点不存放数据

*/

//管理单链表

class SingleLinkedList {

//初始化一个头节点

private final HeroNode head = new HeroNode(0,"");

//添加节点到单项链表,无顺序添加

public void add(HeroNode heroNode){

HeroNode temp = head;

while (temp.next!=null){

temp = temp.next;

}

temp.next = heroNode;

}

//按顺序添加,编号可重复

public void addNodeByNo(HeroNode heroNode){

HeroNode temp = head;

while (temp.next != null&&temp.next.no <= heroNode.no) {

temp = temp.next;

}

heroNode.next = temp.next;

temp.next = heroNode;

}

//按顺序添加,编号不可重复

public void addNodeByNo2(HeroNode heroNode){

HeroNode temp = head;

boolean flag = false;

while (temp.next != null) {

if(temp.next.no == heroNode.no){

flag = true;

break;

}else if(temp.next.no > heroNode.no){

break;

}

temp = temp.next;

}

if(flag){

System.out.println("数据编号重复!");

return;

}

heroNode.next = temp.next;

temp.next = heroNode;

}

//根据编号修改节点的信息

public void update(HeroNode newHeroNode){

if(head.next == null){

System.out.println("链表为空!");

return;

}

HeroNode temp = head.next;

while(temp!=null&&temp.no!=newHeroNode.no){

temp = temp.next;

}

if(temp!=null){

temp.name = newHeroNode.name;

}else {

System.out.println("节点不存在!");

}

}

//根据编号删除节点

public void deleteByNo(int no){

if(head.next==null){

System.out.println("链表为空!");

return;

}

HeroNode temp = head;

while (temp.next!=null&&temp.next.no!=no){

temp = temp.next;

}

if(temp.next==null){

System.out.println("节点不存在!");

return;

}

temp.next = temp.next.next;

}

//根据编号显示节点

public HeroNode getNodeByNo(int no){

if(head.next==null){

System.out.println("链表为空!");

return null;

}

HeroNode temp = head;

while (temp.next!=null&&temp.next.no!=no){

temp = temp.next;

}

if(temp.next==null){

System.out.println("节点不存在!");

return null;

}

return temp.next;

}

//查找第k个节点

public HeroNode getNode(int k){

if(head.next==null){

System.out.println("链表为空!");

return null;

}

HeroNode temp = head;

int num = 1;

while (num!=k){

temp = temp.next;

num++;

}

return temp.next;

}

//删除单链表的第k个节点

public void deleteNo(int k){

if(head.next==null){

System.out.println("链表为空!");

return;

}

HeroNode temp = head;

int num = 1;

while (num!=k){

temp = temp.next;

num++;

}

temp.next = temp.next.next;

}

//显示链表

public void show(){

HeroNode temp =head.next;

if(temp == null){

System.out.println("链表无数据!");

}

while (temp != null) {

System.out.println(temp);

temp = temp.next;

}

}

}

//数据结构

class HeroNode {

public int no;

public String name;

public HeroNode next;

public HeroNode(int no, String name) {

this.no = no;

this.name = name;

}

@Override

public String toString() {

return "HeroNode{" +

"no=" + no +

", name='" + name + '\'' +

'}';

}

}

//测试

public class SingleLinked {

public static void main(String[] args) {

SingleLinkedList linkedList =new SingleLinkedList();

linkedList.show();

linkedList.addNodeByNo2(new HeroNode(0,"张三"));

linkedList.addNodeByNo2(new HeroNode(1,"李四"));

linkedList.addNodeByNo2(new HeroNode(4,"王五"));

linkedList.addNodeByNo2(new HeroNode(2,"赵六"));

linkedList.addNodeByNo2(new HeroNode(2,"赵六"));

linkedList.addNodeByNo2(new HeroNode(3,"吴七"));

linkedList.addNodeByNo2(new HeroNode(2,"丽丽"));

linkedList.show();

linkedList.update(new HeroNode(7,"莉莉"));

linkedList.show();

linkedList.deleteByNo(4);

linkedList.show();

linkedList.deleteNo(4);

linkedList.show();

System.out.println(linkedList.getNodeByNo(2));

System.out.println(linkedList.getNode(2));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值