如果列表为空,则放弃检查

Discarding checking if list is empty

本文关键字:放弃 检查 列表 如果      更新时间:2023-10-16

上次您设法修复了我的代码,我想再次寻求您的帮助。

由于我已经有了一个由五个元素组成的预定义列表,所以这段代码看起来相当荒谬,因为没有检查列表是否为空的目的。我似乎不知道该绕过谁,只保留"插入"函数,而不是同时检查列表是否为空。。。

#include <iostream>
#include <cmath>
using namespace std;
struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);

bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;
    cout << "Menun";
    cout << "1. Add food, beverage etc.n";
    cout << "2. Show the list of food(s), beverage(s) etc.n";
    cout << "3. Update your current weightn";
    cout << "4. What have you been eaten?n";
    cout << "5. What exercise have you done?n";
    cout << "6. Exit program n";
    cin >> choice;
    return choice;
}
void insertAsFirstElement(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    node *temp = new node;
    temp->nameOfFood = nameOfFood;
    temp->eatCalories = eatCalories;
    temp->next = NULL;
    head = temp;
    last = temp;
}
void insert(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, nameOfFood, eatCalories);
    else
    {
        node *temp = new node;
        temp->nameOfFood = nameOfFood;
        temp->eatCalories = eatCalories;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
}

如果你需要更多的代码,请告诉我?

希望得到你的帮助!

该检查是必要的,因为如果列表为空,则必须执行一定数量的操作,而这些操作仅在这种情况下执行。

实现自己的链接列表实际上没有任何意义。标准已经定义了比您的类灵活得多的类,请参见std::forward_list(单链列表)和std::list(双链表)。

建议您在选择容器时默认为std::vectorstd::array。在这种情况下,如果您只是有一个5个元素的列表,只需使用std::array和一个自定义类型:

struct food
{
    string nameOfFood;
    int eatCalories;
    int number;
};

然后:

std::array<food, 5> food_list { ... };

但我的列表永远不会是空的,因为我有五个预定义的元素。我会发布其余的代码,也许你会理解我的意思?否则,就是我弄错了。

#include <iostream>
#include <cmath>
using namespace std;
struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);
bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}
char menu()
{
    char choice;
    
    cout << "Menun";
    cout << "1. Add food, beverage etc.n";
    cout << "2. Show the list of food(s), beverage(s) etc.n";
    cout << "3. Update your current weightn";
    cout << "4. What have you been eaten?n";
    cout << "5. What exercise have you done?n";
    cout << "6. Exit program n";
    
    cin >> choice;
    
    return choice;
    
}
void insertAsFirstElement(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    node *temp = new node;
    temp->nameOfFood = nameOfFood;
    temp->eatCalories = eatCalories;
    temp->next = NULL;
    head = temp;
    last = temp;
}
void insert(node *&head, node *&last, string nameOfFood, int eatCalories)
{
    if(isEmpty(head))
        insertAsFirstElement(head, last, nameOfFood, eatCalories);
    else
    {
        node *temp = new node;
        temp->nameOfFood = nameOfFood;
        temp->eatCalories = eatCalories;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
    
}
void showList(node *current)
{
    if(isEmpty(current))
        cout << "The list of foods is emptyn";
    else
    {
        cout << "The list of foods contain: n";
        int  number = 0;
        
        while(current != NULL)
        {
            ++number;
            cout << number << " " << current->nameOfFood << " " << current->eatCalories << " calories" << endl;
            
            current = current->next;
            
        }
    }
}
node* subtractCalories(node *head)
{
    int choice;
    showList(head);
    cout << "Pick the food, beverage etc. from the list by numbern";
    cin >> choice;
    
    int number = 1;
    node *current = head;
    while (current != NULL && choice != number)
    {
        ++number;
        current = current->next;
    }
    cout << "You have choosen: " << current->nameOfFood << endl;
    return current;
}
int main(int argc, const char * argv[]) {
    
    double caloriesToBurn;
    double weight;
    double height;
    double BMI;
    
    int answerWhile = 1;
    int answerToExercise = 0;
    float minutesExerciseInHour;
    float caloriesBurned;
    
    node *head = NULL;
    node *last = NULL;
    node *current;
    char choice;
    int eatCalories;
    string nameOfFood;
    
    insert(head, last, "Tuna (in oil)", 190);
    insert(head, last, "Milkchocolate (Marabou)", 540);
    insert(head, last, "Milk (skimmed)", 33 );
    insert(head, last, "Ice Tea (white peach)", 1);
    insert(head, last, "Peanuts (roasted and salted)", 624);
    
    cout << "*** Welcome to the calorie calculator! ***n";
    cout << "To get started, i need some numbers from youn";
    cout << "Please enter the number of calories you need to burn today:n";
    cin >> caloriesToBurn;
    cout << "What are your current weight? (please enter in kilograms)n";
    cin >> weight;
    cout << "And lastly i need to know your height (please enter in centimeters)n";
    cin >> height;
    BMI = (weight / pow(height/100,2));
    cout << "Your BMI is: " << BMI << endl;
    
    if (BMI <= 18.5)
        cout << "You are in the range: under weight" << endl;
    else if ((BMI > 18.5) && (BMI < 25))
        cout << "You are in the range: normal weight" << endl;
    else
        cout << "You are in the range: over weight" << endl;
    do{
        
        choice = menu();
        switch(choice)
        {
            case '1':
                cout << "Enter the name of the food, beverage etc.:";
                cin >> nameOfFood;
                cout << "How many calories did it contain? (measured per 100 grams):";
                cin >> eatCalories;
                insert(head, last, nameOfFood, eatCalories);
                break;
            case '2': showList(head);
                break;
            case '3': cout << "What you wanna update your weight to?n";
                cin >> weight;
                cout << "Your weight have been update to: n";
                cout << weight << endl;
                if (BMI <= 18.5)
                    cout << "You are in the range: under weight" << endl;
                else if ((BMI > 18.5) && (BMI < 25))
                    cout << "You are in the range: normal weight" << endl;
                else
                    cout << "You are in the range: over weight" << endl;
                break;
            case '4':
                cout << endl << "You need to consume " << caloriesToBurn << " calories today!" << endl << endl;
                current = subtractCalories(head);
                caloriesToBurn = caloriesToBurn-current->eatCalories;
                cout << endl << "You need to eat " << caloriesToBurn << " calories more today!" << endl;
                break;
            case '5':
                do
                {
                    cout << "How many minuttes have you ben exercising (please enter in hours)n";
                    cin >> minutesExerciseInHour;
                    cout << "What type of exercise have you been doing?" <<endl;
                    cout << "1. Running" <<endl;
                    cout << "2. Swimming" <<endl;
                    cout << "3. Cycling" <<endl;
                    cin >> answerToExercise;
                    switch(answerToExercise)
                    {
                        case 1:
                            caloriesBurned = weight*7.5*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "caloriesn";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left todayn";
                            break;
                            
                        case 2:
                            caloriesBurned = weight*7*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "caloriesn";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left todayn";
                            break;
                            
                        case 3:
                            caloriesBurned = weight*6*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "caloriesn";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left todayn";
                            break;
                            
                        default: cout << "I think something went wrong? Try again..";
                    }
                }
                while (answerWhile != 1);
            default: cout << "System exitn";
        }
    }while(choice != '6');
    
    
    return 0;
}

我想我可以通过检查空列表来理解,因此我将把我的代码带到另一个问题上。

我得到了InsertAsFirstElement函数,但现在,我想我忘记了这个函数的作用,因为在代码中的任何地方,都不会有"按第一个元素插入",所以有人能说出这个函数的功能吗?

很抱歉给您带来不便!