Home > Software engineering >  How can I write the data input code to prevent infinite repetition?
How can I write the data input code to prevent infinite repetition?

Time:12-11

I don't know why the name and score stored in varchar format in mysql are in descending order by score using linkedlist, but it causes infinite repetition when saving the information.

`public class PlayerController : MonoBehaviour
{
    public string addURL = "http://127.0.0.1:3000/db_add/";
    public string resultURL = "http://127.0.0.1:3000/db_connection/";

    PlayerDataVO[] ranking; 
    PlayerDataVO head = null;

    public void StartSendData()
    {
        StartCoroutine(SendData());
    }
    public void StartGetData()
    {
        StartCoroutine(GetResult());
    }
    public void ReadData()
    {

    }

    IEnumerator SendData()  
    {
        WWWForm form = new WWWForm();   
    
        string n = "0000";    
        string s = "0000";      

        form.AddField("name", n);   
        form.AddField("score", s);  
        UnityWebRequest www = UnityWebRequest.Post(addURL, form);  
        yield return www.SendWebRequest();  

        if (www.result != UnityWebRequest.Result.Success)
        {
            Debug.Log(www.error);
        }
        else
        {
            Debug.Log("UPLOAD COMPLETE");
        }
        www.Dispose();

        StopCoroutine(GetResult());
        StartCoroutine(GetResult());
    }

    IEnumerator GetResult() 
    {
        using (WWW www = new WWW(resultURL))   
        {
            yield return www;   
            byte[] bytesForEncoding = System.Text.Encoding.UTF8.GetBytes(www.text);
            string str = System.Text.Encoding.UTF8.GetString(bytesForEncoding);
            Debug.Log(str);
            ranking = JsonConvert.DeserializeObject<PlayerDataVO[]>(str);
            for (int i = 0; ranking.Length > i; i  )
            {
                string name = ranking[i].getName();
                string score = ranking[i].getScore();
                PlayerDataVO vo = new PlayerDataVO(name, score);
                if (head == null)
                {
                    head = vo;
                    continue;
                }
                else if(head.next == null)
                {
                    if (Convert.ToInt32(head.getScore()) < Convert.ToInt32(vo.getScore()))
                    {
                        vo.next = head;
                        head = vo;
                    }
                    else
                    {
                        head.next = vo;
                    }
                    continue;
                }
                PlayerDataVO prev = head;
                PlayerDataVO temp = head.next;
                print(temp.getName());
                while(temp.next != null)
                {
                    temp = temp.next;
                }
                temp.next = vo;
            }

            Debug.Log(head.getName());
            Debug.Log("데이터 길이 : "   ranking.Length);

           
            PlayerDataVO temp2 = head;
          
            while (temp2 != null)
            {
                print(temp2.getName()   " "   temp2.getScore());
                temp2 = temp2.next;
            }
        }
    }
}`
`[System.Serializable]
public class PlayerDataVO
{
    private string name;
    private string score;

    public PlayerDataVO(string name, string score)
    {
        this.name = name;
        this.score = score;
    }
    public string getName()
    {
        return name;
    }
    public string getScore()
    {
        return score;
    }
    public PlayerDataVO next;`
}

I can't remember what I tried because I was debugging while in a state of body aches. I'm sorry.

I was expecting The highest score is 1st and must be ranked in descending order of score.

CodePudding user response:

I’m your code, you’re not stepping through the linked list properly. You need to step through and update both the prev and temp pointers so the new vo can be inserted into the right place.

This should do the trick (written but untested):

PlayerDataVO prev = head;
PlayerDataVO temp = head.next;
print(temp.getName());

var s = Convert.ToInt32(vo.getScore());
do
{
    if (Convert.ToInt32(temp.getScore()) < voScore)
    {
        prev.next = vo;
        vo.next = temp;
        continue;
    }
    prev = temp;
    temp = temp.next;
} while ( temp != null );

temp = vo;
  • Related