程序里有几个注释的地方还是需要特别注意的,根据单链表自己的逻辑来写一遍双向链表,可能有几个细节处理的不到位。
package DataStructures.LinkedList;public class DoubleLinkedListDemo {public static void main(String []args){System.out.println("双向链表的测试");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.add(hero1);doubleLinkedList.add(hero2);doubleLinkedList.add(hero3);doubleLinkedList.add(hero4);doubleLinkedList.list();HeroNode2 newHeroNode = new HeroNode2(4,"公孙胜","入云龙");doubleLinkedList.updata(newHeroNode);doubleLinkedList.list();doubleLinkedList.delete(1);doubleLinkedList.list();}
}
class DoubleLinkedList{private HeroNode2 head = new HeroNode2(0,"","");public HeroNode2 getHead() {return head;}public void delete(int no){if (head.next == null){System.out.println("链表为空,无法删除");}HeroNode2 temp = head.next;boolean flag = false;while (temp != null){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;}System.out.println("节点删除成功\n");}else {System.out.printf("没找到编号为 %d 的节点!删除失败\n",no);}}public void updata(HeroNode2 heroNode){if (head.next == null){System.out.println("链表为空位,无法修改");return;}HeroNode2 temp = head.next;boolean flag = false;while(true){if (temp == null){break;}if (temp.no == heroNode.no){flag = true;break;}temp = temp.next;}if (flag){temp.name = heroNode.name;temp.nickname = heroNode.nickname;}else {System.out.printf("没找到 编号为 %d 的节点\n",heroNode.no);}}public void add(HeroNode2 newHeroNode){HeroNode2 temp = head;while(true){if (temp.next == null){break;}temp = temp.next;}temp.next = newHeroNode;newHeroNode.pre = temp;}public void list(){if (head.next == null){System.out.println("链表为空");return;}HeroNode2 temp = head.next;while (temp != null){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;}@Overridepublic String toString() {return "HeroNode2{" +"no=" + no +", name='" + name + '\'' +", nickname='" + nickname + '\'' +'}';}
}
加一个添加的时候根据no排序
public void addByOrder(HeroNode2 newHeroNode){HeroNode2 temp = head;boolean flag = false;while (true){if (temp.next == null){break;}if (temp.next.no > newHeroNode.no){break;}else if (temp.next.no == newHeroNode.no){flag = true;break;}temp = temp.next;}if (flag){System.out.printf("编号 %d 已存在,不能添加\n",newHeroNode.no);}else {if (temp.next != null) {temp.next.pre = newHeroNode;newHeroNode.next = temp.next;}temp.next = newHeroNode;newHeroNode.pre = temp;}}
测试
doubleLinkedList.addByOrder(hero1);
doubleLinkedList.addByOrder(hero4);
doubleLinkedList.addByOrder(hero2);
doubleLinkedList.addByOrder(hero3);doubleLinkedList.list();
结果:双向链表的测试
HeroNode2{no=1, name='宋江', nickname='及时雨'}
HeroNode2{no=2, name='卢俊义', nickname='玉麒麟'}
HeroNode2{no=3, name='吴用', nickname='智多星'}
HeroNode2{no=4, name='林冲', nickname='豹子头'}