Home > Back-end >  Why singly linked lists with loop to add data will be a success, an infinite loop
Why singly linked lists with loop to add data will be a success, an infinite loop

Time:10-13

I want to ask is there any bosses know, singly linked lists to flip, I want to put the chain table node in the array, and then turn back to the new list, but also with a loop to add, why there will be a added successfully,
an infinite loop?
This is a linked list class
 public class SingeLinkedList {
//to create a head head node, is said the head of a singly linked list
Private Node firstNode=new Node (0, ""," ");

/* list add a node method: each add a node, is directly added to the list of the last */

//behind every time we add a node, is directly added to the list at the end of the
//ideas when regardless of the number of order
//1. Find the current list of the last node
//2. The last of the node next to the new node
Public void the add Node (the Node) {
//1. Find the last node, query element to start from scratch every time
The Node temp=firstNode;
A Boolean flag=true;
While (flag) {
If (temp. GetNext ()==null) {
flag=false;
} else {
Temp=temp. GetNext ();
}
}
//2. The last of the node next to the new node
Temp. SetNext (node);
}

/* traversing the entire list */
//by an auxiliary variable traversal, help to traverse the entire list
Public void list () {
//whether the list is empty first
If (firstNode getNext ()==null) {
System. The out. Println (" list is empty ");
} else {
//because the head node can't move so need an auxiliary variable
The Node temp=firstNode;
While (true) {
//whether the list at the end of the
If (temp. GetNext ()==null) {
break;
} else {
//output node information
System. The out. Println (temp. GetNext ());
//the temp backwards
Temp=temp. GetNext ();
}
}
}
}
}

This is a node
 public class Node {
Private int num.
private String name;
Private String nickname;
Private Node next;

Public Node (int num, String name, String nickname) {
This. Num=num;
This. Name=name;
This. Nickname=nickname;
}

Public Node getNext () {
Return next;
}
Public void setNext (next) {
This. Next=next;
}

Public int getNum () {
return num;
}

Public void elegantly-named setName (String name) {
This. Name=name;
}

Public void setNickname (String nickname) {
This. Nickname=nickname;
}

Public String getName () {
return name;
}

Public String getNickname () {
Return the nickname;
}

@ Override
Public String toString () {
Return the Node "{" +
"Num=" + num +
"And name='" + name +' \ ' '+
='" + ", nickname nickname +' \ '+'
'} ';
}
}



this is a problem of the main program
 public class Demo05 {
Public static void main (String [] args) {
The Node one=new Node (1, "sung river", "timely rain");
The Node two=new Node (2, "jun-yi lu", "jade kirin");
Node three=new Node (3, "wu", "resource");
The Node four=new Node (4, "GongSunSheng", "enter the dragon");
The Node five=new Node (5, "GuanSheng", "sword");
SingeLinkedList SingeLinkedList=new SingeLinkedList ();
SingeLinkedList reverse01=new SingeLinkedList ();

List List=new ArrayList<> (a);

The Collections. AddAll (list, one, two, three, four, five);
List. The stream (). ForEach (I - & gt; SingeLinkedList. Add (I));//success

Reverse (singeLinkedList). Stream (). ForEach (I - & gt; Reverse01. Add (I));//death cycle?????????????????????????????????????

}


Public static List The reverse (SingeLinkedList SingeLinkedList) {
SingeLinkedList New=New SingeLinkedList ();
The Node temp=singeLinkedList. GetFirstNode ();
//the list to store the map key
//map put order node and key
//listNode reverse node, and the output
int count=0;
List List=new ArrayList<> (a);
List ListNode=new ArrayList<> (a);
Map The map=new HashMap<> (a);

//traverse the list
While (true) {
If (temp. GetNext ()==null) {
break;
} else {
The map. The put (the count, temp. GetNext ());
List. The add (count);
count++;
Temp=temp. GetNext ();
}
}

The Collections. The sort (list, (a, b) - & gt; {
Return b - a;
});

List. The stream (). ForEach (I - & gt; ListNode. Add (map. Get (I)));
Return listNode;
}
}
  • Related