实现链表的基本操作
- 因为单链表和双链表很相似,所以我使用了MVC设计模式简化了思路,并且使用Java语言编译
- 首先在dao层抽取出节点,用于存放信息
- 然后在service层分别实现单链表和双链表的具体操作】
- 最后在客户层调用service的方法
| package com.guodaxia.dao; |
| |
| |
| |
| |
| |
| public class HeroNode { |
| public int no; |
| public String name; |
| public HeroNode next; |
| public HeroNode pre; |
| |
| public HeroNode() {} |
| public HeroNode(int no, String name) { |
| this.no = no; |
| this.name = name; |
| } |
| @Override |
| public String toString() { |
| return "HeroKing{" + |
| "no=" + no + |
| ", name='" + name + '\'' + |
| '}'; |
| } |
| } |
| package com.guodaxia.service; |
| |
| import com.guodaxia.dao.HeroNode; |
| |
| |
| |
| |
| |
| public class DoubleLinkedList { |
| HeroNode head = new HeroNode(0,""); |
| |
| |
| |
| |
| |
| public void add(HeroNode heroNode){ |
| |
| HeroNode temp =head; |
| |
| while (true){ |
| if (temp.next==null){ |
| break; |
| } |
| temp=temp.next; |
| } |
| |
| temp.next = heroNode; |
| } |
| |
| |
| |
| |
| public void list(){ |
| |
| if (head.next==null){ |
| System.out.println("链表为空!"); |
| return; |
| } |
| |
| HeroNode temp =head.next; |
| while (true){ |
| if (temp==null){ |
| break; |
| } |
| System.out.println(temp); |
| temp = temp.next; |
| } |
| } |
| |
| |
| |
| |
| |
| 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; |
| }else { |
| System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no); |
| } |
| } |
| |
| |
| |
| |
| |
| public void del(int no){ |
| |
| if (head.next==null){ |
| System.out.println("链表为空,无法删除!"); |
| return; |
| } |
| HeroNode temp = head.next; |
| boolean flag = false; |
| while (true){ |
| if (temp==null){ |
| break; |
| } |
| if (temp.no==no){ |
| flag=true; |
| break; |
| } |
| temp=temp.next; |
| } |
| if (flag){ |
| |
| temp.pre.next=temp.next; |
| if (temp.next!=null){ |
| temp.next.pre=temp.pre; |
| } |
| }else { |
| System.out.printf("要删除的%d节点不存在!\n",no); |
| } |
| } |
| } |
| package com.guodaxia.service; |
| |
| import com.guodaxia.dao.HeroNode; |
| |
| |
| |
| |
| |
| public class SingleLinkedList { |
| HeroNode head = new HeroNode(0,""); |
| |
| |
| |
| |
| |
| public void addByOrder(HeroNode heroNode){ |
| |
| |
| HeroNode temp =head; |
| boolean flag = false; |
| |
| while (true){ |
| if (temp.next==null){ |
| break; |
| } |
| if (temp.next.no> heroNode.no){ |
| break; |
| }else if (temp.next.no ==heroNode.no){ |
| flag = true; |
| break; |
| } |
| temp = temp.next; |
| } |
| if (flag){ |
| System.out.printf("准备插入的英雄的编号%d已经存在了,不能加入\n",heroNode.no); |
| }else { |
| |
| heroNode.next=temp.next; |
| temp.next= heroNode; |
| } |
| } |
| |
| |
| |
| |
| public void list(){ |
| |
| if (head.next==null){ |
| System.out.println("链表为空!"); |
| return; |
| } |
| |
| HeroNode temp =head; |
| while (true){ |
| if (temp==null){ |
| break; |
| } |
| if (temp.next!=null) |
| System.out.println(temp.next); |
| temp = temp.next; |
| } |
| } |
| |
| |
| |
| |
| |
| 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; |
| }else { |
| System.out.printf("没有找到编号%d的节点,不能修改\n",newHeroNode.no); |
| } |
| } |
| |
| |
| |
| |
| |
| public void del(int no){ |
| HeroNode temp = head; |
| boolean flag = false; |
| while (true){ |
| if (temp.next==null){ |
| break; |
| } |
| if (temp.next.no==no){ |
| flag=true; |
| break; |
| } |
| temp=temp.next; |
| } |
| if (flag){ |
| |
| temp.next=temp.next.next; |
| }else { |
| System.out.printf("要删除的%d节点不存在!\n",no); |
| } |
| } |
| } |
| package com.guodaxia.test; |
| |
| import com.guodaxia.dao.HeroNode; |
| import com.guodaxia.service.DoubleLinkedList; |
| import com.guodaxia.service.SingleLinkedList; |
| |
| |
| |
| |
| |
| public class SingleTest { |
| public static void main(String[] args) { |
| HeroNode hero1 = new HeroNode(1, "小乔"); |
| HeroNode hero2 = new HeroNode(2, "大桥"); |
| HeroNode hero4 = new HeroNode(4, "吕布"); |
| HeroNode hero3 = new HeroNode(3, "诸葛亮"); |
| |
| SingleLinkedList list =new SingleLinkedList(); |
| list.addByOrder(hero1); |
| list.addByOrder(hero2); |
| list.addByOrder(hero3); |
| list.addByOrder(hero4); |
| list.list(); |
| |
| HeroNode newHero = new HeroNode(2, "周瑜"); |
| list.update(newHero); |
| System.out.println("修改后的节点信息:"); |
| list.list(); |
| |
| System.out.println("删除后信息"); |
| list.del(1); |
| list.del(2); |
| list.del(3); |
| list.del(4); |
| list.list(); |
| System.out.println("***********以上为单链表测试******************"); |
| DoubleLinkedList list1 = new DoubleLinkedList(); |
| list1.add(hero1); |
| list1.add(hero4); |
| list1.add(hero3); |
| list1.add(hero2); |
| list1.list(); |
| |
| list1.update(new HeroNode(2,"周瑜")); |
| list1.list(); |
| |
| list1.del(1); |
| list1.list(); |
| } |
| } |
在双向链表中也可以通过编号顺序进行添加展示:
| |
| |
| |
| |
| public void addByOrder(HeroNode heroNode){ |
| |
| HeroNode temp =head; |
| boolean flag = false; |
| |
| while (true){ |
| if (temp.next==null){ |
| break; |
| } |
| if (temp.next.no> heroNode.no){ |
| break; |
| }else if (temp.next.no ==heroNode.no){ |
| flag = true; |
| break; |
| } |
| temp = temp.next; |
| } |
| if (flag){ |
| System.out.printf("准备插入的英雄的编号%d已经存在了,不能加入\n",heroNode.no); |
| }else { |
| |
| heroNode.next=temp.next; |
| temp.next= heroNode; |
| heroNode.pre=temp; |
| } |
| } |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)