class SingleLinkList4{
private Hero head = new Hero(0);
public Hero getHead() {
return head;
}
public void del(int no){
if (head.getNext() == null){
System.out.println("链表为空!!!");
return;
}
boolean flag = false;
Hero temp = head;
while (true){
if (temp.getNext() == null){
break;
}
if (temp.getNext().getNo() == no){
flag = true;
break;
}
temp = temp.getNext();
}
if (flag){
temp.setNext(temp.getNext().getNext());
}else {
System.out.println("没有找到这个节点!!!");
}
}
public void add(Hero node){
Hero temp = head;
while (true){
if (temp.getNext() == null){
break;
}
temp = temp.getNext();
}
temp.setNext(node);
}
public void addByOrder(Hero node){
Hero temp = head;
boolean flag = false;
while (true){
if (temp.getNext() == null){
break;
}
if (temp.getNext().getNo() > node.getNo()){
break;
}else if(temp.getNext().getNo() == node.getNo()){
flag = true;
break;
}
temp= temp.getNext();
}
if (flag){
System.out.println("链表中已经存在此节点!!");
}else {
node.setNext(temp.getNext());
temp.setNext(node);
}
}
public void show(){
if (head.getNext() == null){
System.out.println("链表为空!!!");
return;
}
Hero temp = head.getNext();
while (temp != null){
System.out.println(temp);
temp = temp.getNext();
}
}
}
class Hero{
private int no;
private Hero next;
@Override
public String toString() {
return "Hero{" +
"no=" + no +
'}';
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Hero getNext() {
return next;
}
public void setNext(Hero next) {
this.next = next;
}
public Hero(int no) {
this.no = no;
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-2378.html