class HeroNode
package DataStructures. linkedList;
public 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 + '\'' +
'}' ;
}
}
class singleLinkedList
package DataStructures. linkedList;
import java. util. Stack;
public 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. println ( "准备插入的英雄编号已存在" ) ;
} else {
heroNode. next = temp. next;
temp. next = heroNode;
}
}
public void list ( ) {
if ( head. next == null) {
System. out. println ( "链表为空" ) ;
return ;
}
HeroNode temp = head. next;
while ( true ) {
if ( temp == null)
break ;
System. out. println ( temp) ;
temp = temp. next;
}
}
public void update ( HeroNode heroNode) {
if ( head. next == null) {
System. out. println ( "链表为空" ) ;
return ;
}
HeroNode temp = head. next;
boolean flag = false ;
while ( true ) {
if ( temp == null)
break ;
if ( temp. no == heroNode. no) {
flag = true ;
break ;
}
temp = temp. next;
}
if ( flag) {
temp. name = heroNode. name;
temp. nickName = heroNode. nickName;
} else {
System. out. println ( "没有找到" ) ;
}
}
public void delete ( int n) {
HeroNode temp = head;
boolean flag = false ;
while ( true ) {
if ( temp. next == null) {
break ;
}
if ( temp. next. no == n) {
flag = true ;
break ;
}
temp = temp. next;
}
if ( flag) {
temp. next = temp. next. next;
} else {
System. out. println ( "未找到需删除节点" ) ;
}
}
public static int getLength ( HeroNode heroNode) {
if ( heroNode. next == null) {
return 0 ;
}
int len = 0 ;
HeroNode temp = heroNode. next;
while ( temp != null) {
len++ ;
temp = temp. next;
}
return len;
}
public static HeroNode findLastIndexNode ( HeroNode heroNode, int index) {
if ( heroNode. next == null) {
return null;
}
int size = getLength ( heroNode) ;
if ( index <= 0 || index > size) {
return null;
}
HeroNode temp = heroNode. next;
for ( int i = 0 ; i < size - index; i++ ) {
temp = temp. next;
}
return temp;
}
public static void reverseList ( 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) {
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 linkedListTogether ( HeroNode heroNode1, HeroNode heroNode2) {
}
}