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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| public class SingleLinkListDemo { public static void main ( String[] args ) { HeroNode hero1 = new HeroNode ( 2, "松江", "及时雨" ); HeroNode hero2 = new HeroNode ( 3, "卢俊义", "玉麒麟" ); HeroNode hero3 = new HeroNode ( 5, "吴用", "智多星" ); HeroNode hero4 = new HeroNode ( 6, "林冲", "豹子头" ); HeroNode hero5 = new HeroNode ( 1, "松江", "及时雨" ); HeroNode hero6 = new HeroNode ( 4, "卢俊义", "玉麒麟" ); HeroNode hero7 = new HeroNode ( 8, "吴用", "智多星" ); HeroNode hero8 = new HeroNode ( 9, "林冲", "豹子头" );
SingleLinkedList singleLinkedList1 = new SingleLinkedList ( ); SingleLinkedList singleLinkedList2 = new SingleLinkedList ( );
singleLinkedList1.addpro ( hero1 ); singleLinkedList1.addpro ( hero3 ); singleLinkedList1.addpro ( hero2 ); singleLinkedList1.addpro ( hero4 ); singleLinkedList2.addpro ( hero5 ); singleLinkedList2.addpro ( hero6 ); singleLinkedList2.addpro ( hero7 ); singleLinkedList2.addpro ( hero8 ); combineSingleLink ( singleLinkedList1, singleLinkedList2 ).list ( );
System.out.printf ( "链表中有效节点为%d\n", getLength ( singleLinkedList1.getHead ( ) ) ); System.out.println ( "测试逆序打印单链表,没有改变本身结构" ); reversePrint ( singleLinkedList1.getHead ( ) ); System.out.println ( "查找值" ); findIndex ( singleLinkedList1.getHead ( ), 2 ); System.out.println ( "修改之后的列表" ); singleLinkedList1.list ( ); System.out.println ( "原来链表" ); singleLinkedList1.list ( );
System.out.println ( "反转后的链表" ); reverseLinkedList ( singleLinkedList1.getHead ( ) ); singleLinkedList1.list ( );
}
public static int getLength ( HeroNode head ) { if ( head.next == null ) { return 0; } int length = 0; HeroNode temp = head.next; while (temp != null) { length++; temp = temp.next; } return length; }
public static void findIndex ( HeroNode head, int index ) { if ( head.next == null ) { System.out.println ( "列表为空" ); } int size = getLength ( head ); int num = size - index; if ( num < 0 || num > size ) { System.out.println ( "index值输入错误" ); return; } int i = 0; HeroNode temp = head.next; while (i < num) { i++; temp = temp.next; } System.out.println ( temp );
}
public static void reverseLinkedList ( HeroNode head ) { if ( head.next == null || head.next.next == null ) { return; }
HeroNode temp = head.next; HeroNode next = null; HeroNode reverseHead = new HeroNode ( 0, "", "" ); while (temp != null) { next = temp.next; temp.next = reverseHead.next;
reverseHead.next = temp; temp = next; } head.next = reverseHead.next; }
public static void reversePrint ( HeroNode head ) { if ( head.next == null ) { System.out.println ( "空链表无打印" ); return; } Stack<HeroNode> stack = new Stack<HeroNode> ( ); HeroNode temp = head.next; while (temp != null) { stack.push ( temp ); temp = temp.next; } while (stack.size ( ) > 0) { System.out.println ( stack.pop ( ) ); } }
public static SingleLinkedList combineSingleLink ( SingleLinkedList link1, SingleLinkedList link2 ) { HeroNode head1 = link1.getHead ( ); HeroNode head2 = link2.getHead ( ); if ( head1.next == null && head2.next == null ) { return null; } if ( head1.next == null ) { System.out.println ( "链表一为空" ); return link2; } if ( head2.next == null ) { System.out.println ( "链表二为空" ); return link1; }
HeroNode temp1 = head1.next; HeroNode temp2 = head2.next; SingleLinkedList link = new SingleLinkedList ( ); HeroNode temp = link.getHead ( ); temp.next = temp1; HeroNode next = null; while (temp2 != null) { if ( temp.next == null ) { temp.next = temp2; break; } if ( temp2.no <= temp.next.no ) { next = temp2.next; temp2.next = temp.next; temp.next = temp2; temp2 = next; } temp = temp.next; } return link;
} }
|