双向链表
Posted bingbug
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双向链表相关的知识,希望对你有一定的参考价值。
双向链表中不同于单向链表,有两个指针域,一个负责向后连接,一个负责向前连接
相对于单向链表来说:查找更加方便,但添加删除相对复杂
package linkedlist; public class DoubleLinkedListDemo { public static void main(String[] args) { HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨"); HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟"); HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星"); HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头"); DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.addByOrder(hero2); doubleLinkedList.addByOrder(hero3); doubleLinkedList.addByOrder(hero1); doubleLinkedList.addByOrder(hero4); doubleLinkedList.list(); // doubleLinkedList.add(hero1); // doubleLinkedList.add(hero2); // doubleLinkedList.add(hero3); // doubleLinkedList.add(hero4); // doubleLinkedList.list(); // // System.out.println("修改后"); // // HeroNode2 heroNode2 = new HeroNode2(4, "魔*林冲", "豹子头"); // doubleLinkedList.update(heroNode2); // doubleLinkedList.list(); // // System.out.println("删除后"); // doubleLinkedList.delete(4); // doubleLinkedList.list(); } } class DoubleLinkedList{ private HeroNode2 head = new HeroNode2(0, "", ""); public HeroNode2 getHead() { return head; } //添加排序 public void addByOrder(HeroNode2 heroNode) { HeroNode2 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 add(HeroNode2 heroNode2){ HeroNode2 temp = head; while (true) { if (temp.next == null) { break; } temp = temp.next; } temp.next = heroNode2; heroNode2.pre = temp; } public void update(HeroNode2 newHeroNode){ if(head.next == null){ System.out.println("链表为空"); return; } HeroNode2 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; } HeroNode2 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 信息,无法删除", no); } } public void list() { if (head.next == null) { System.out.println("链表为空"); return; } HeroNode2 temp = head.next; while (true) { if (temp == null) { break; } System.out.println(temp); temp = temp.next; } } } class HeroNode2 { public int no; public String name; public String nickname; public HeroNode2 next; public HeroNode2 pre; public HeroNode2(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 + ‘‘‘ + ‘]‘; } }
以上是关于双向链表的主要内容,如果未能解决你的问题,请参考以下文章