****
链表
双链表
结点类
先创建结点类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class HeroNode2 { public int no; public String name; public String nikeName; public HeroNode2 next; public HeroNode2 pre; public HeroNode2(int no, String name, String nikeName){ this.no = no; this.name = name; this.nikeName = nikeName; } @Override public String toString(){ return "HeroNo:" + no + "HeroName" + name + "nikeName" + nikeName; } }
|
构建双链表类
里面构造方法
1 2 3 4 5 6 7 8 9 10
| public class doubleLinked{ private HeroNode2 head = new HeroNode2(0,"",""); public HeroNode2 getHead() { return head; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class DoubleLinkedList { public static void main(String[] args) { HeroNode2 hero1 = new HeroNode2(1,"Marisa","mar"); HeroNode2 hero2 = new HeroNode2(2,"Reimu","rei"); HeroNode2 hero3 = new HeroNode2(3,"Cirno","qii"); HeroNode2 hero4 = new HeroNode2(4,"Meirin","hon"); System.out.println("--------Inilization--------"); DoubleLinked doubleLinked = new DoubleLinked(); doubleLinked.add(hero1); doubleLinked.add(hero2); doubleLinked.add(hero3); doubleLinked.add(hero4); doubleLinked.add(new HeroNode2(5,"ABC","A"));
doubleLinked.list(); System.out.println(doubleLinked.listSingle(2)); doubleLinked.ModifyPos(3,"新改的"); doubleLinked.list();
HeroNode2 hero7 = new HeroNode2(5,"Yukari","zima"); doubleLinked.insert(1,hero7); System.out.println("----------Insert-----------"); doubleLinked.list();
System.out.println("-----------Del-------------"); doubleLinked.del(5); doubleLinked.list(); } }
|
遍历
可以直接使用head
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public void list() { if(head.next = null){ System.out.println("The doublieLink is null"); return; } HeroNode2 temp = head.next; while(true){ if(temp == null){ break; } System.out.print(temp); temp = temp.next; } }
|
查找特定值
1 2 3 4 5 6 7 8 9 10 11 12
| public String findSingle(int pos){ if(head.next == null){ System.out.println("null"); return; } HeroNode2 tem = head.next; for(int i = 1; i < pos; i++){ tem = tem.next; } return tem.name; }
|
修改结点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public void ModifyPos(int pos, String name){ HeroNode2 tem = head.next; if(tem.next == null){ System.out.println("The link is null"); return; } while(tem.no != pos ){ tem = tem.next; }
tem.name = name; }
|
初始化
1 2 3 4 5 6 7 8
| public void add(HeroNode2 heroNode){ HeroNode2 temp = head.next; while(temp.next != null){ temp = temp.next; } temp.next = heroNode; heroNode.pre = temp; }
|
添加结点
1 2 3 4 5 6 7 8 9 10 11
| public void insert(int pos, HeroNode2 newNode){ HeroNode2 temp = head.next; for(int index = 1; index < pos; index++){ temp = temp.next; } HeroNode2 last = temp.next; temp.next = newNode; newNode.next = last; last.pre = newNode; newNode.pre = temp; }
|
删除结点
要考虑删除时不存在的情况,不能使用for循环和直接的指向
用前后指针来写,注意要加多一个最后为空的判断情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public void delNode(int no){ if(head.next == null){ System.out.println("NULL"); return; } HeroNode2 prer = head.next; flag = false; while(true){ if(temp == ture){ System.out.println("NULL"); return; } else{ if(temp.no == no){ flag = true; break; } temp = temp.next; } } if(flag == true){ temp.pre.next = temp.next; if(temp.next != null){ temp.next.pre = temp;.pre; } } else { System.out.printf("del %d not exist\n",no); } }
|