单向链表
Posted bingbug
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单向链表相关的知识,希望对你有一定的参考价值。
单向链表是链表的一种,特点是链表的方向是单向的,对链表的访问要通过顺序读取从头部开始;链表是使用指针进行构造的列表;又称为结点列表,因为链表是由一个个结点组装起来的;其中每个结点都有指针成员变量指向列表中的下一个结点;
优点:单个节点创建方便,节点删除方便,节点访问方便
public class SingleLinkedListDemo { 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(hero2); singleLinkedList.add(hero3); singleLinkedList.add(hero4); // singleLinkedList.delete(1); singleLinkedList.list(); // System.out.println(hero1); /* singleLinkedList.addByOrder(hero4); singleLinkedList.addByOrder(hero3); singleLinkedList.addByOrder(hero2); // singleLinkedList.addByOrder(hero2); singleLinkedList.addByOrder(hero1); singleLinkedList.list(); //HeroNode newHeroNode = new HeroNode(2,"cs","cs"); //singleLinkedList.update(newHeroNode); System.out.println(); // singleLinkedList.delete(2); // singleLinkedList.delete(3); singleLinkedList.delete(4); singleLinkedList.list(); */ } } class SingleLinkedList { private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() { return head; } public void add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; } public void mergeByOrder(HeroNode head, HeroNode head2) { if (head.next == null) { return; } } 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 添加已存在 ", heroNode.no); } else { heroNode.next = temp.next; temp.next = heroNode; } } public void update(HeroNode newHeroNode) { if (head.next == null) { System.out.println("链表为空"); return; } HeroNode temp = head; 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 信息,无法修改", newHeroNode.no); } } public void delete(int no) { if (head.next == null) { System.out.println("链表为空"); return; } HeroNode temp = head; boolean flag = false; while (true) { if (temp == 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 信息,无法删除", no); } } 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; } } } 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; } @Override public String toString() { return "HeroNode[" + "no=" + no + ", name=‘" + name + ‘‘‘ + ", nickname=‘" + nickname + ‘‘‘ + ‘]‘; } }
以上是关于单向链表的主要内容,如果未能解决你的问题,请参考以下文章