歐克!歐克!小劉今天帶你們來學習一下鏈表 ,你要是學不會,你來捶我java
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 + "]"; } }
class SingleLinkedList { private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() { return head; }
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 add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; }
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.list(); }
HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头]
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 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.addByOrder(hero1); singleLinkedList.addByOrder(hero4); singleLinkedList.addByOrder(hero2); singleLinkedList.addByOrder(hero3); singleLinkedList.list(); }
HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头]
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 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.addByOrder(hero1); singleLinkedList.addByOrder(hero4); singleLinkedList.addByOrder(hero2); singleLinkedList.addByOrder(hero3); HeroNode newHeroNode = new HeroNode(2, "小盧", "玉麒麟~~"); singleLinkedList.update(newHeroNode); singleLinkedList.list(); }
HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=小卢, nickName=玉麒麟~~] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头]
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); } }
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.del(1); singleLinkedList.del(4); singleLinkedList.list(); }
HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星]
public static int getLength(HeroNode head) { if (head.next == null) { return 0; } int length = 0; HeroNode cur = head.next; while (cur != null) { length++; cur = cur.next; } return length; }
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.list(); System.out.println("有效的節點個數=" + getLength(singleLinkedList.getHead())); }
HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头] 有效的节点个数=4
public static HeroNode findLastIndexNode(HeroNode head, int index) { if (head.next == null) { return null; } int size = getLength(head); if (index 0 || index > size) { return null; } HeroNode cur = head.next; for (int i = 0; i < size - index; i++) { cur = cur.next; } return cur; }
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.list(); HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 2); System.out.println("res=" + res); }
HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头] res=HeroNode [no=3, name=吴用, nickName=智多星]
public static void reversetList(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode cur = head.next; HeroNode next = null; HeroNode reverseHead = new HeroNode(0, "", ""); while (cur != null) { next = cur.next; cur.next = reverseHead.next; reverseHead.next = cur; cur = next; } head.next = reverseHead.next; }
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); System.out.println("原來鏈表的狀況~~"); singleLinkedList.list(); System.out.println("反轉單鏈表~~"); reversetList(singleLinkedList.getHead()); singleLinkedList.list(); }
原来链表的情况~~ HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头] 反转单链表~~ HeroNode [no=4, name=林冲, nickName=豹子头] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=1, name=宋江, nickName=及时雨]
public static void myReversetList(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode cur = head.next; HeroNode next = cur.next; cur.next = null; while (next != null) { HeroNode nnext = next.next; next.next = cur; cur = next; next = nnext; } head.next = cur; }
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); System.out.println("原來鏈表的狀況~~"); singleLinkedList.list(); System.out.println("反轉單鏈表~~"); reversetList(singleLinkedList.getHead()); singleLinkedList.list(); }
原来链表的情况~~ HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头] 反转单链表~~ HeroNode [no=4, name=林冲, nickName=豹子头] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=1, name=宋江, nickName=及时雨]
public static void main(String[] args) { Stack<String> stack = new Stack(); stack.add("jack"); stack.add("tom"); stack.add("smith"); while (stack.size() > 0) { System.out.println(stack.pop()); } }
smith tom jack
public static void reversePrint(HeroNode head) { if (head.next == null) { return; } Stack<HeroNode> stack = new Stack<HeroNode>(); HeroNode cur = head.next; while (cur != null) { stack.push(cur); cur = cur.next; } while (stack.size() > 0) { System.out.println(stack.pop()); } }
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); System.out.println("原來鏈表的狀況~~"); singleLinkedList.list(); System.out.println("測試逆序打印單鏈表, 沒有改變鏈表的結構~~"); reversePrint(singleLinkedList.getHead()); }
原来链表的情况~~ HeroNode [no=1, name=宋江, nickName=及时雨] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=4, name=林冲, nickName=豹子头] 测试逆序打印单链表, 没有改变链表的结构~~ HeroNode [no=4, name=林冲, nickName=豹子头] HeroNode [no=3, name=吴用, nickName=智多星] HeroNode [no=2, name=卢俊义, nickName=玉麒麟] HeroNode [no=1, name=宋江, nickName=及时雨]
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(hero4); singleLinkedList.add(hero2); singleLinkedList.add(hero3); System.out.println("原來鏈表的狀況~~"); singleLinkedList.list(); System.out.println("反轉單鏈表~~"); reversetList(singleLinkedList.getHead()); singleLinkedList.list(); System.out.println("測試逆序打印單鏈表, 沒有改變鏈表的結構~~"); reversePrint(singleLinkedList.getHead()); singleLinkedList.addByOrder(hero1); singleLinkedList.addByOrder(hero4); singleLinkedList.addByOrder(hero2); singleLinkedList.addByOrder(hero3); singleLinkedList.list(); HeroNode newHeroNode = new HeroNode(2, "小盧", "玉麒麟~~"); singleLinkedList.update(newHeroNode); System.out.println("修改後的鏈表狀況~~"); singleLinkedList.list(); singleLinkedList.del(1); singleLinkedList.del(4); System.out.println("刪除後的鏈表狀況~~"); singleLinkedList.list(); System.out.println("有效的節點個數=" + getLength(singleLinkedList.getHead())); HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 3); System.out.println("res=" + res); } public static void reversePrint(HeroNode head) { if (head.next == null) { return; } Stack<HeroNode> stack = new Stack<HeroNode>(); HeroNode cur = head.next; while (cur != null) { stack.push(cur); cur = cur.next; } while (stack.size() > 0) { System.out.println(stack.pop()); } } public static void reversetList(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode cur = head.next; HeroNode next = null; HeroNode reverseHead = new HeroNode(0, "", ""); while (cur != null) { next = cur.next; cur.next = reverseHead.next; reverseHead.next = cur; cur = next; } head.next = reverseHead.next; } public static void myReversetList(HeroNode head) { if (head.next == null || head.next.next == null) { return; } HeroNode cur = head.next; HeroNode next = cur.next; cur.next = null; while (next != null) { HeroNode nnext = next.next; next.next = cur; cur = next; next = nnext; } head.next = cur; } public static HeroNode findLastIndexNode(HeroNode head, int index) { if (head.next == null) { return null; } int size = getLength(head); if (index 0 || index > size) { return null; } HeroNode cur = head.next; for (int i = 0; i < size - index; i++) { cur = cur.next; } return cur; } public static int getLength(HeroNode head) { if (head.next == null) { return 0; } int length = 0; HeroNode cur = head.next; while (cur != null) { length++; cur = cur.next; } return length; } } 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 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 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 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); } } 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 + "]"; } }
class HeroNode { public int no; public String name; public String nickname; public HeroNode next; public HeroNode pre; 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 + "]"; } }
class DoubleLinkedList { private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() { return head; }
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 add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; heroNode.pre = temp; }
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; if(temp.next != null) { temp.next.pre = heroNode; } temp.next = heroNode; heroNode.pre = temp; } }
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 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); } }
public static void main(String[] args) { System.out.println("雙向鏈表的測試"); HeroNode hero1 = new HeroNode(1, "宋江", "及時雨"); HeroNode hero2 = new HeroNode(2, "盧俊義", "玉麒麟"); HeroNode hero3 = new HeroNode(3, "吳用", "智多星"); HeroNode hero4 = new HeroNode(5, "林沖", "豹子頭"); DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(hero1); doubleLinkedList.add(hero2); doubleLinkedList.add(hero3); doubleLinkedList.add(hero4); doubleLinkedList.list(); doubleLinkedList.addByOrder(new HeroNode(4, "Heygo", "Heygogo")); doubleLinkedList.addByOrder(new HeroNode(6, "Oneby", "Onebyone")); System.out.println("按順序插入後的狀況"); doubleLinkedList.list(); HeroNode newHeroNode = new HeroNode(5, "公孫勝", "入雲龍"); doubleLinkedList.update(newHeroNode); System.out.println("修改後的鏈表狀況"); doubleLinkedList.list(); doubleLinkedList.del(3); System.out.println("刪除後的鏈表狀況~~"); doubleLinkedList.list(); }
双向链表的测试 HeroNode [no=1, name=宋江, nickname=及时雨] HeroNode [no=2, name=卢俊义, nickname=玉麒麟] HeroNode [no=3, name=吴用, nickname=智多星] HeroNode [no=5, name=林冲, nickname=豹子头] 按顺序插入后的情况 HeroNode [no=1, name=宋江, nickname=及时雨] HeroNode [no=2, name=卢俊义, nickname=玉麒麟] HeroNode [no=3, name=吴用, nickname=智多星] HeroNode [no=4, name=Heygo, nickname=Heygogo] HeroNode [no=5, name=林冲, nickname=豹子头] HeroNode [no=6, name=Oneby, nickname=Onebyone] 修改后的链表情况 HeroNode [no=1, name=宋江, nickname=及时雨] HeroNode [no=2, name=卢俊义, nickname=玉麒麟] HeroNode [no=3, name=吴用, nickname=智多星] HeroNode [no=4, name=Heygo, nickname=Heygogo] HeroNode [no=5, name=公孙胜, nickname=入云龙] HeroNode [no=6, name=Oneby, nickname=Onebyone] 删除后的链表情况~~ HeroNode [no=1, name=宋江, nickname=及时雨] HeroNode [no=2, name=卢俊义, nickname=玉麒麟] HeroNode [no=4, name=Heygo, nickname=Heygogo] HeroNode [no=5, name=公孙胜, nickname=入云龙] HeroNode [no=6, name=Oneby, nickname=Onebyone]
public class DoubleLinkedListDemo { public static void main(String[] args) { System.out.println("雙向鏈表的測試"); HeroNode hero1 = new HeroNode(1, "宋江", "及時雨"); HeroNode hero2 = new HeroNode(2, "盧俊義", "玉麒麟"); HeroNode hero3 = new HeroNode(3, "吳用", "智多星"); HeroNode hero4 = new HeroNode(5, "林沖", "豹子頭"); DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.add(hero1); doubleLinkedList.add(hero2); doubleLinkedList.add(hero3); doubleLinkedList.add(hero4); doubleLinkedList.list(); doubleLinkedList.addByOrder(new HeroNode(0, "Kobe", "BlackMamba")); doubleLinkedList.addByOrder(new HeroNode(4, "Heygo", "Heygogo")); doubleLinkedList.addByOrder(new HeroNode(6, "Oneby", "Onebyone")); System.out.println("按順序插入後的狀況"); doubleLinkedList.list(); HeroNode newHeroNode = new HeroNode(5, "公孫勝", "入雲龍"); doubleLinkedList.update(newHeroNode); System.out.println("修改後的鏈表狀況"); doubleLinkedList.list(); doubleLinkedList.del(3); System.out.println("刪除後的鏈表狀況~~"); doubleLinkedList.list(); } } class DoubleLinkedList { private HeroNode head = new HeroNode(0, "", ""); public HeroNode getHead() { return head; } 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 add(HeroNode heroNode) { HeroNode temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode; heroNode.pre = temp; } 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; if(temp.next != null) { temp.next.pre = heroNode; } temp.next = heroNode; heroNode.pre = temp; } } 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 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); } } } class HeroNode { public int no; public String name; public String nickname; public HeroNode next; public HeroNode pre; 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 + "]"; } }
class Boy { private int no; private Boy next; public Boy(int no) { this.no = no; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public Boy getNext() { return next; } public void setNext(Boy next) { this.next = next; } }
class CircleSingleLinkedList { private Boy first = null;
public void addBoy(int nums) { if (nums < 1) { System.out.println("nums的值不正確"); return; } Boy curBoy = null; for (int i = 1; i nums; i++) { Boy boy = new Boy(i); if (i == 1) { first = boy; first.setNext(first); curBoy = first; } else { curBoy.setNext(boy); boy.setNext(first); curBoy = boy; } } }
public void showBoy() { if (first == null) { System.out.println("沒有任何小孩~~"); return; } Boy curBoy = first; while (true) { System.out.printf("小孩的編號 %d \n", curBoy.getNo()); if (curBoy.getNext() == first) { break; } curBoy = curBoy.getNext(); } }
public void countBoy(int startNo, int countNum, int nums) { if (first == null || startNo < 1 || startNo > nums) { System.out.println("參數輸入有誤, 請從新輸入"); return; } Boy helper = first; while (true) { if (helper.getNext() == first) { break; } helper = helper.getNext(); } for (int j = 0; j < startNo - 1; j++) { first = first.getNext(); helper = helper.getNext(); } while (true) { if (helper == first) { break; } for (int j = 0; j < countNum - 1; j++) { first = first.getNext(); helper = helper.getNext(); } System.out.printf("小孩%d出圈\n", first.getNo()); first = first.getNext(); helper.setNext(first); } System.out.printf("最後留在圈中的小孩編號%d \n", first.getNo()); }
public static void main(String[] args) { CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList(); circleSingleLinkedList.addBoy(5); circleSingleLinkedList.showBoy(); circleSingleLinkedList.countBoy(1, 2, 3); }
小孩的编号 1 小孩的编号 2 小孩的编号 3 小孩的编号 4 小孩的编号 5 小孩2出圈 小孩4出圈 小孩1出圈 小孩5出圈 最后留在圈中的小孩编号3
public class Josepfu { public static void main(String[] args) { CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList(); circleSingleLinkedList.addBoy(5); circleSingleLinkedList.showBoy(); circleSingleLinkedList.countBoy(1, 2, 3); } } class CircleSingleLinkedList { private Boy first = null; public void addBoy(int nums) { if (nums < 1) { System.out.println("nums的值不正確"); return; } Boy curBoy = null; for (int i = 1; i nums; i++) { Boy boy = new Boy(i); if (i == 1) { first = boy; first.setNext(first); curBoy = first; } else { curBoy.setNext(boy); boy.setNext(first); curBoy = boy; } } } public void showBoy() { if (first == null) { System.out.println("沒有任何小孩~~"); return; } Boy curBoy = first; while (true) { System.out.printf("小孩的編號 %d \n", curBoy.getNo()); if (curBoy.getNext() == first) { break; } curBoy = curBoy.getNext(); } } public void countBoy(int startNo, int countNum, int nums) { if (first == null || startNo < 1 || startNo > nums) { System.out.println("參數輸入有誤, 請從新輸入"); return; } Boy helper = first; while (true) { if (helper.getNext() == first) { break; } helper = helper.getNext(); } for (int j = 0; j < startNo - 1; j++) { first = first.getNext(); helper = helper.getNext(); } while (true) { if (helper == first) { break; } for (int j = 0; j < countNum - 1; j++) { first = first.getNext(); helper = helper.getNext(); } System.out.printf("小孩%d出圈\n", first.getNo()); first = first.getNext(); helper.setNext(first); } System.out.printf("最後留在圈中的小孩編號%d \n", first.getNo()); } } class Boy { private int no; private Boy next; public Boy(int no) { this.no = no; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public Boy getNext() { return next; } public void setNext(Boy next) { this.next = next; } }