搜索基于链接的列表C ++

时间:2015-05-07 07:57:50

标签: c++ linked-list

因此,我的项目是使用随机数猜测游戏(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);
        }
    }   
}

任何想法?

1 个答案:

答案 0 :(得分:1)

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

    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')。