一、单链表
(1)链表是以节点的方式存储
(2)每个节点包含date域,next域:指向下一个结点
(3)链表的结点不一定是连续存储
(4)链表分带头结点的的链表和没有带头节点的链表,根据实际需求来确定
使用带头的单
package LinkList; class HeroNode1 { private int no; private String name; private String nickName; public HeroNode1 next;//指向下一个节点 public HeroNode1 prior;//指向前一个节点 public HeroNode1(int no, String name, String nickName) { this.no = no; this.name = name; this.nickName = nickName; } @Override public String toString() { return "HeroNode1{" + "no=" + no + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public HeroNode1 getNext() { return next; } public void setNext(HeroNode1 next) { this.next = next; } public HeroNode1 getPrior() { return prior; } public void setPrior(HeroNode1 prior) { this.prior = prior; } } class DoubleLinkedList{ private HeroNode1 head=new HeroNode1 (0,"",""); public DoubleLinkedList() { } //添加 public void add(HeroNode1 hero){ HeroNode1 temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=hero; hero.prior=temp; } //删除 public void delete(int no){ if(head.next==null){ System.out.println ("该链表为空,删除失败!" ); return; } HeroNode1 temp=head.next; while(temp.getNo ()!=no){ temp=temp.next; } temp.prior.next=temp.next; if (temp!=null){ temp.next.prior=temp.prior; } } //修改 public void update(HeroNode1 newHero){ HeroNode1 temp=head; while(true){ if(temp.next==null){ System.out.println ("未找到要修改的节点" ); return; } if(temp.getNo ()==newHero.getNo ()){ break; } temp=temp.next; } temp.setNo (newHero.getNo ()); temp.setName (newHero.getName ()); temp.setNickName (newHero.getNickName ()); System.out.println ("修改成功!" ); } //遍历 public void list(){ if(head.next==null){ System.out.println ("此链表为空!" ); return; } HeroNode1 temp=head.next; while(temp!=null){ System.out.println (temp ); temp=temp.next; } } } public class DoubleLinkedListDemo { public static void main(String[] args) { DoubleLinkedList doubleList = new DoubleLinkedList ( ); HeroNode1 hero1 = new HeroNode1 (1, "宋江", "及时雨"); HeroNode1 hero2 = new HeroNode1 (2, "卢俊义", "玉麒麟"); HeroNode1 hero3 = new HeroNode1 (3, "吴用", "智多星"); HeroNode1 hero4 = new HeroNode1 (4, "林冲", "豹子头"); doubleList.add (hero1); doubleList.add (hero2); doubleList.add (hero3); doubleList.add (hero4); doubleList.list (); doubleList.delete (2); HeroNode1 newHero = new HeroNode1 (3, "凌不疑", "081"); doubleList.update (newHero); doubleList.list (); } }
链表实现水浒英雄排行榜管理,要求如下:
(1)完成对英雄人物的增删改查操作
(2)第一种方法在添加英雄时,直接添加到链表的尾部
(3)第二种方式在添加英雄时,根据排名蒋英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)
示意图:
二、双向链表
双向链表相比单链表存在以下优点:
(1)单链表查找的方向只能是一个方向,而双向链表可以向前或向后进行查找
(2)单链表不能自我删除,需要辅助节点,而双向链表可以进行自我删除
示意图为:
使用双向链表实现水浒英雄排行榜进行增删改查操作:
package LinkList; class HeroNode1 { private int no; private String name; private String nickName; public HeroNode1 next;//指向下一个节点 public HeroNode1 prior;//指向前一个节点 public HeroNode1(int no, String name, String nickName) { this.no = no; this.name = name; this.nickName = nickName; } @Override public String toString() { return "HeroNode1{" + "no=" + no + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public HeroNode1 getNext() { return next; } public void setNext(HeroNode1 next) { this.next = next; } public HeroNode1 getPrior() { return prior; } public void setPrior(HeroNode1 prior) { this.prior = prior; } } class DoubleLinkedList{ private HeroNode1 head=new HeroNode1 (0,"",""); public DoubleLinkedList() { } //添加 public void add(HeroNode1 hero){ HeroNode1 temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=hero; hero.prior=temp; } //删除 public void delete(int no){ if(head.next==null){ System.out.println ("该链表为空,删除失败!" ); return; } HeroNode1 temp=head.next; while(temp.getNo ()!=no){ temp=temp.next; } temp.prior.next=temp.next; if (temp!=null){ temp.next.prior=temp.prior; } } //修改 public void update(HeroNode1 newHero){ HeroNode1 temp=head; while(true){ if(temp.next==null){ System.out.println ("未找到要修改的节点" ); return; } if(temp.getNo ()==newHero.getNo ()){ break; } temp=temp.next; } temp.setNo (newHero.getNo ()); temp.setName (newHero.getName ()); temp.setNickName (newHero.getNickName ()); System.out.println ("修改成功!" ); } //遍历 public void list(){ if(head.next==null){ System.out.println ("此链表为空!" ); return; } HeroNode1 temp=head.next; while(temp!=null){ System.out.println (temp ); temp=temp.next; } } } public class DoubleLinkedListDemo { public static void main(String[] args) { DoubleLinkedList doubleList = new DoubleLinkedList ( ); HeroNode1 hero1 = new HeroNode1 (1, "宋江", "及时雨"); HeroNode1 hero2 = new HeroNode1 (2, "卢俊义", "玉麒麟"); HeroNode1 hero3 = new HeroNode1 (3, "吴用", "智多星"); HeroNode1 hero4 = new HeroNode1 (4, "林冲", "豹子头"); doubleList.add (hero1); doubleList.add (hero2); doubleList.add (hero3); doubleList.add (hero4); doubleList.list (); doubleList.delete (2); HeroNode1 newHero = new HeroNode1 (3, "凌不疑", "081"); doubleList.update (newHero); doubleList.list (); } }