下面实现一个例子来进行学习
1)介绍
单链表的逻辑结构
在内存中的实际结构
具体创建示意图:
2)代码实现
例子
1。第一个程序在添加的时候并没有按照序号排序,如果在添加的时候把位置改变输出的时候序号会改变
package DataStructures. LinkedList; public class SingleLinedListDemo { 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 linkedList = new SingleLinkedList ( ) ; linkedList. add ( hero1) ; linkedList. add ( hero2) ; linkedList. add ( hero3) ; linkedList. add ( hero4) ; linkedList. list ( ) ; }
}
class SingleLinkedList { private HeroNode head = new HeroNode ( 0 , "" , "" ) ; public void add ( HeroNode heroNode) { HeroNode temp = head; while ( true ) { if ( temp. next == null) { break ; } temp = temp. next; } temp. next = heroNode; } public void list ( ) { if ( head. next == null) { 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 + '\'' + '}' ; }
}
2。第二个程序下面的代码会进行排序,这里排序我感觉和JAVA-API的实现ComparaTo类似
下面是思路分析
和上面程序比新添加了一个addByOrder()方法
这样就可以在内存中实现数据以链表形式生成并根据序号排序,速度快效率高。
package DataStructures. LinkedList; public class SingleLinedListDemo { 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 linkedList = new SingleLinkedList ( ) ;
linkedList. addByOrder ( hero1) ; linkedList. addByOrder ( hero4) ; linkedList. addByOrder ( hero2) ; linkedList. addByOrder ( hero3) ; linkedList. addByOrder ( hero3) ; linkedList. list ( ) ; }
}
class SingleLinkedList { private HeroNode head = new HeroNode ( 0 , "" , "" ) ; 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 list ( ) { if ( head. next == null) { 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 + '\'' + '}' ; }
}
下面进行修改节点信息,根据no,no不能变,在SingleLinkedList中添加方法
public void updata ( 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 的节点,不能修改" , newHeroNode. no) ; } }
测试代码
System. out. println ( "显示(修改前)" ) ; linkedList. list ( ) ; HeroNode hero5 = new HeroNode ( 2 , "chun" , "chunchun" ) ; linkedList. updata ( hero5) ; System. out. println ( "显示(修改后)" ) ; linkedList. list ( ) ;
根据no来删除节点,遍历链表,找到no节点的前一个位置,让no前一个位置的next指向no的后一个位置的数据
public void delete ( int no) { if ( head. next == null) { System. out. println ( "链表为空,不能删除" ) ; return ; } 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 ( "链表里没有no为:%d 的节点\n" , no) ; } }
测试代码
linkedList. delete ( 2 ) ; System. out. println ( "显示删除后" ) ; linkedList. list ( ) ; linkedList. delete ( 2 ) ; System. out. println ( "显示删除后" ) ; linkedList. list ( ) ;