单链表
节点定义
public class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next;
public HeroNode ( int no, String name, String nickName, HeroNode next) {
this . no = no;
this . name = name;
this . nickName = nickName;
this . next = next;
}
@Override
public String toString ( ) {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}' ;
}
}
单链表
public class SingleLinkedList {
private HeroNode head = new HeroNode ( 0 , "" , "" , null ) ;
public HeroNode getHead ( ) {
return head;
}
public void add ( HeroNode heroNode) {
HeroNode temp = head;
while ( temp. next != null ) {
temp = temp. next;
}
temp. next = heroNode;
}
public void show ( ) {
if ( head. next == null ) {
System . out. println ( "单链表为空" ) ;
}
HeroNode temp = head. next;
while ( temp != null ) {
System . out. println ( temp) ;
temp = temp. next;
}
}
public void addByOrder ( HeroNode heroNode) {
HeroNode temp = head;
while ( temp. next != null ) {
if ( temp. next. no > heroNode. no) {
break ;
} else if ( temp. next. no == heroNode. no) {
System . out. println ( "编号" + heroNode. no + "已经存在,添加失败" ) ;
return ;
}
temp = temp. next;
}
heroNode. next = temp. next;
temp. next = heroNode;
}
public void update ( HeroNode heroNode) {
HeroNode temp = head. next;
if ( temp == null ) {
System . out. println ( "单链表为空,修改失败" ) ;
return ;
}
while ( temp != null ) {
if ( temp. no == heroNode. no)
break ;
else
temp = temp. next;
}
if ( temp == null ) {
System . out. println ( "没有找到编号为:" + heroNode. no + "的元素" ) ;
return ;
}
else {
temp. name = heroNode. name;
temp. nickName = heroNode. nickName;
}
}
public void delete ( HeroNode heroNode) {
HeroNode temp = head;
if ( temp. next == null ) {
System . out. println ( "单链表为空,删除失败" ) ;
return ;
}
while ( temp. next != null ) {
if ( temp. next. no == heroNode. no)
break ;
else
temp = temp. next;
}
if ( temp. next == null ) {
System . out. println ( "没找到编号为:" + heroNode. no + "的元素,删除失败" ) ;
return ;
} else
temp. next = temp. next. next;
}
}
测试类
public class Test {
public static void main ( String [ ] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList ( ) ;
HeroNode hero1 = new HeroNode ( 1 , "宋江" , "及时雨" , null ) ;
HeroNode hero2 = new HeroNode ( 2 , "卢俊义" , "玉麒麟" , null ) ;
HeroNode hero3 = new HeroNode ( 3 , "吴用" , "智多星" , null ) ;
HeroNode hero4 = new HeroNode ( 4 , "林冲" , "豹子头" , null ) ;
singleLinkedList. addByOrder ( hero1) ;
singleLinkedList. addByOrder ( hero4) ;
singleLinkedList. addByOrder ( hero3) ;
singleLinkedList. addByOrder ( hero2) ;
singleLinkedList. show ( ) ;
System . out. println ( ) ;
HeroNode newhero = new HeroNode ( 2 , "卢俊义" , "玉麒麟" , null ) ;
singleLinkedList. delete ( newhero) ;
singleLinkedList. show ( ) ;
System . out. println ( ) ;
System . out. println ( "单链表的节点个数 = " + getlenth ( singleLinkedList. getHead ( ) ) ) ;
System . out. println ( ) ;
int Index = 4 ;
HeroNode indexNode = findLastIndexNode ( singleLinkedList. getHead ( ) , Index ) ;
System . out. println ( indexNode) ;
if ( indexNode == null )
System . out. println ( "没有找到倒数第:" + Index + "个元素" ) ;
else {
System . out. println ( "倒数第:" + Index + "个元素为:" ) ;
System . out. println ( indexNode) ;
}
System . out. println ( ) ;
reverse ( singleLinkedList. getHead ( ) ) ;
singleLinkedList. show ( ) ;
System . out. println ( ) ;
reversePrint ( singleLinkedList. getHead ( ) ) ;
System . out. println ( ) ;
SingleLinkedList s1 = new SingleLinkedList ( ) ;
HeroNode h1 = new HeroNode ( 1 , "孙悟空" , "小孙" , null ) ;
HeroNode h3 = new HeroNode ( 3 , "猪八戒" , "小猪" , null ) ;
s1. addByOrder ( h1) ;
s1. addByOrder ( h3) ;
System . out. println ( ) ;
SingleLinkedList s2 = new SingleLinkedList ( ) ;
HeroNode h2 = new HeroNode ( 2 , "唐僧" , "小唐" , null ) ;
HeroNode h4 = new HeroNode ( 4 , "沙悟净" , "小沙" , null ) ;
s2. addByOrder ( h2) ;
s2. addByOrder ( h4) ;
merge ( s1. getHead ( ) , s2. getHead ( ) ) ;
s1. show ( ) ;
}
public static void merge ( HeroNode head1, HeroNode head2) {
HeroNode head3 = new HeroNode ( 0 , "" , "" , null ) ;
HeroNode temp1 = head1. next;
HeroNode temp2 = head2. next;
HeroNode temp3 = head3;
while ( temp1 != null && temp2 != null ) {
if ( temp1. no <= temp2. no) {
head1. next = temp1. next;
temp3. next = temp1;
temp3 = temp1;
temp1 = head1. next;
} else {
head2. next = temp2. next;
temp3. next = temp2;
temp3 = temp2;
temp2 = head2. next;
}
}
if ( temp1 != null )
temp3. next = temp1;
if ( temp2 != null )
temp3. next = temp2;
head1. next = head3. next;
}
public static void reversePrint ( HeroNode head) {
if ( head == null ) {
System . out. println ( "单链表为空,不能打印" ) ;
return ;
}
Stack < HeroNode > stack = new Stack < > ( ) ;
HeroNode temp = head. next;
while ( temp != null ) {
stack. push ( temp) ;
temp = temp. next;
}
while ( stack. size ( ) > 0 )
System . out. println ( stack. pop ( ) ) ;
}
public static void reverse ( HeroNode head) {
if ( head. next == null || head. next. next == null )
return ;
HeroNode reverseHead = new HeroNode ( 0 , "" , "" , null ) ;
HeroNode temp = head. next;
while ( temp != null ) {
head. next = temp. next;
temp. next = reverseHead. next;
reverseHead. next = temp;
temp = head. next;
}
head. next = reverseHead. next;
}
public static HeroNode findLastIndexNode ( HeroNode head, int Index ) {
int num = getlenth ( head) ;
if ( num < Index )
return null ;
HeroNode temp = head. next;
for ( int i = 0 ; i < ( num - Index ) ; i++ ) {
temp = temp. next;
}
return temp;
}
public static int getlenth ( HeroNode head) {
if ( head. next == null )
return 0 ;
HeroNode temp = head. next;
int length = 0 ;
while ( temp != null ) {
length++ ;
temp = temp. next;
}
return length;
}
}
双链表
节点定义
public class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next;
public HeroNode pre;
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 + '\'' +
'}' ;
}
}
双链表
public class DoubleLinkedList {
private HeroNode head = new HeroNode ( 0 , "" , "" ) ;
public HeroNode getHead ( ) {
return head;
}
public void addByNo ( HeroNode heroNode) {
HeroNode temp = head;
while ( temp. next != null ) {
if ( temp. next. no > heroNode. no) {
break ;
} else if ( temp. next. no == heroNode. no) {
System . out. println ( "编号" + heroNode. no + "已经存在,添加失败" ) ;
return ;
}
temp = temp. next;
}
heroNode. next = temp. next;
if ( temp. next != null )
temp. next. pre = heroNode;
heroNode. pre = temp;
temp. next = heroNode;
}
public void delete ( HeroNode heroNode) {
HeroNode temp = head. next;
if ( temp == null ) {
System . out. println ( "双链表为空,删除失败" ) ;
return ;
}
while ( temp != null ) {
if ( temp. no == heroNode. no)
break ;
else
temp = temp. next;
}
if ( temp == null ) {
System . out. println ( "没找到编号为:" + heroNode. no + "的元素,删除失败" ) ;
return ;
} else {
temp. pre. next = temp. next;
if ( temp. next != null )
temp. next. pre = temp. pre;
}
}
public void update ( HeroNode heroNode) {
HeroNode temp = head. next;
if ( temp == null ) {
System . out. println ( "双链表为空,修改失败" ) ;
return ;
}
while ( temp != null ) {
if ( temp. no == heroNode. no)
break ;
else
temp = temp. next;
}
if ( temp == null ) {
System . out. println ( "没有找到编号为:" + heroNode. no + "的元素" ) ;
return ;
} else {
temp. name = heroNode. name;
temp. nickName = heroNode. nickName;
}
}
public void add ( HeroNode heroNode) {
HeroNode temp = head;
while ( temp. next != null ) {
temp = temp. next;
}
temp. next = heroNode;
heroNode. pre = temp;
}
public void show ( ) {
if ( head. next == null ) {
System . out. println ( "双链表为空" ) ;
}
HeroNode temp = head. next;
while ( temp != null ) {
System . out. println ( temp) ;
temp = temp. next;
}
}
}
测试类
public class Test {
public static void main ( String [ ] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList ( ) ;
HeroNode hero1 = new HeroNode ( 1 , "宋江" , "及时雨" ) ;
HeroNode hero2 = new HeroNode ( 2 , "卢俊义" , "玉麒麟" ) ;
HeroNode hero3 = new HeroNode ( 3 , "吴用" , "智多星" ) ;
HeroNode hero5 = new HeroNode ( 5 , "林冲" , "豹子头" ) ;
doubleLinkedList. add ( hero1) ;
doubleLinkedList. add ( hero2) ;
doubleLinkedList. add ( hero3) ;
doubleLinkedList. add ( hero5) ;
doubleLinkedList. show ( ) ;
System . out. println ( ) ;
HeroNode hero4 = new HeroNode ( 4 , "沙悟净" , "小沙" ) ;
HeroNode hero6 = new HeroNode ( 6 , "哪吒" , "小喳" ) ;
doubleLinkedList. addByNo ( hero4) ;
doubleLinkedList. addByNo ( hero6) ;
doubleLinkedList. show ( ) ;
}
}
约瑟夫问题(循环单链表)
设编号为1, 2, …n的n个人围坐一圈,num个人从编号startNum开始报数,数到countNum的那个人出列,他的下一位又从1开始报数,数到countNum的那个人又出列,一次类推,知道所有人出列为止,由此产生一个出队编号的序列
小孩类
public class Boy {
private int no;
private Boy next;
public Boy ( int no) {
this . no = no;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public Boy getNext ( ) {
return next;
}
public void setNext ( Boy next) {
this . next = next;
}
}
循环单链表
public class CircleSingleLinkedList {
private Boy first = null ;
public void countBoy ( int startNo, int countNum, int num) {
if ( first == null || startNo < 1 || startNo > num) {
System . out. println ( "数据不合理,请重新输入" ) ;
return ;
}
Boy temp = first;
while ( temp. getNext ( ) != first)
temp = temp. getNext ( ) ;
for ( int i = 0 ; i < startNo - 1 ; i++ ) {
first = first. getNext ( ) ;
temp = temp. getNext ( ) ;
}
while ( temp != first) {
for ( int i = 0 ; i < countNum - 1 ; i++ ) {
first = first. getNext ( ) ;
temp = temp. getNext ( ) ;
}
System . out. println ( "小孩:" + first. getNo ( ) + "出圈" ) ;
first = first. getNext ( ) ;
temp. setNext ( first) ;
}
System . out. println ( "小孩:" + first. getNo ( ) + "获得胜利" ) ;
}
public void addBoy ( int num) {
if ( num < 1 ) {
System . out. println ( "num数据不合理" ) ;
return ;
}
Boy temp = new Boy ( 0 ) ;
for ( int i = 1 ; i <= num; i++ ) {
Boy boy = new Boy ( i) ;
if ( i == 1 ) {
first = boy;
first. setNext ( first) ;
temp = first;
} else {
temp. setNext ( boy) ;
boy. setNext ( first) ;
temp = boy;
}
}
}
public void show ( ) {
if ( first == null )
System . out. println ( "链表为空" ) ;
Boy temp = first;
while ( true ) {
System . out. println ( "小孩编号为:" + temp. getNo ( ) ) ;
if ( temp. getNext ( ) == first)
break ;
temp = temp. getNext ( ) ;
}
}
}
测试类
public class test {
public static void main ( String [ ] args) {
CircleSingleLinkedList c = new CircleSingleLinkedList ( ) ;
c. addBoy ( 5 ) ;
c. show ( ) ;
c. countBoy ( 1 , 2 , 5 ) ;
}
}