搜索链表 C++

Searching linked based list C++

所以我的项目是有一个随机数猜谜游戏(1-100),并将用户的猜测值存储到一个链表中。我遇到的困难部分是使用已存储在链表中的先前输入来检查用户的当前输入。我不明白为什么它一直告诉我我已经猜到上一个数字,而我真的没有。

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std; 

struct Guess
{
    int numberGuess;
    Guess* next;
};

int main ()
{
    srand(time(0));

    Guess* start = 0;   

    //Defining variables
    int randomNumber;
    randomNumber = (rand() % 100) + 1;
    int userGuess;
    int i = 0;

    Guess* userValue = new Guess;   

    cout << "I'm thinking of a number between 1 and 100. Guess what it is: " << endl;
    cin >> userGuess;
    cin.ignore(1000,10);

    userValue->numberGuess = userGuess;
    userValue->next = start;
    start = userValue;

    while(true)
    {
        if (userGuess > randomNumber)
        {
            cout << "That's too high, guess again: ";
            cin >> userGuess;
            cin.ignore(1000,10);

            //Pass user's guess to linked based list
            Guess* userValue = new Guess;
            userValue->numberGuess = userGuess;
            userValue->next = start;
            start = userValue;
        }

        else if(userGuess < randomNumber)
        {
            cout << "That's too low, guess again: ";
            cin >> userGuess;
            cin.ignore(1000,10);

            //Pass user's guess to linked based list
            Guess* userValue = new Guess;
            userValue->numberGuess = userGuess;
            userValue->next = start;
            start = userValue;
        }

        if (userGuess == randomNumber)
        {
            cout << "Correct!" << endl;
            break;
        }

        //Check to see if it has been guessed
        bool checkDuplicate = false;
        Guess* p;
        for (p = start; p; p = p->next)
        {
            if (p->numberGuess == userGuess)
            {               
                checkDuplicate = true;
                break;
            }
        }

        if (checkDuplicate == true) 
        {
            cout << "You already guessed that value! -- Guess again: ";
            cin >> userGuess;
            cin.ignore(1000,10);
        }
    }   
}

有什么想法吗?

当用户输入他们的猜测时,您将其添加到列表中,然后尝试找到它。因为你已经添加了它,所以每次都可以在列表中找到最后的猜测。所以你需要将添加节点代码移动到循环的末尾,例如这里

    if (checkDuplicate == true) {
        cout << "You already guessed that value! -- Guess again: ";
        cin >> userGuess;
        cin.ignore(1000,10);
    } else {
        Guess* userValue = new Guess;
        userValue->numberGuess = userGuess;
        userValue->next = start;
        start = userValue;
    }

也为add_nodeexist等列表操作创建函数,代码会简单明了。看例子

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std; 

struct Guess {
    int numberGuess;
    Guess* next;
};

bool exist(Guess* start, int number) {
    for (Guess* p = start; p != NULL; p = p->next) {
        if (p->numberGuess == number) {
            return true;
        }
    }
    return false;
}

void print(Guess* start) {
    for (Guess* p = start; p != NULL; p = p->next) {
        cout << p->numberGuess << " ";
    }
    cout << endl;
}

void add(Guess** start, int number) {
    Guess* userValue = new Guess;
    userValue->numberGuess = number;
    userValue->next = *start;
    *start = userValue;
}

int main () {
    srand(time(0));

    Guess* start = 0;   

    //Defining variables
    int randomNumber;
    randomNumber = (rand() % 100) + 1;
    int userGuess;

    cout << "I'm thinking of a number between 1 and 100. Guess what it is: " << endl;
    while(true)
    {
        cin >> userGuess;
        cin.ignore(1000, '\n');
        if (userGuess > randomNumber) {
            cout << "That's too high, guess again: ";
        }
        else if(userGuess < randomNumber) {
            cout << "That's too low, guess again: ";
        }
        if (userGuess == randomNumber) {
            cout << "Correct!" << endl;
            cout << "Your previous guesses was: ";
            print(start);
            break;
        }
        //Check to see if it has been guessed
        bool checkDuplicate = exist(start, userGuess);
        if (checkDuplicate == true)  {
            cout << "You already guessed that value! -- Guess again: ";
        } else {
            add(&start, userGuess);
        }
    }   
}

现在很明显 main 中发生了什么,它易于阅读,没有代码重复,就像以前一样。也不要在你的程序中使用符号代码,使用符号(在你的情况下'\n')。