Как вам удалось исправить мой код в прошлый раз, я снова хотел попросить вашу помощь.Отмена проверки, если список пуст
Поскольку у меня уже есть предопределенный список из пяти элементов, этот код кажется довольно абсурдным, так как нет никакой цели проверить, пуст ли список. Я не могу показаться, чтобы выяснить, кто в обход если-то еще, а просто держать функцию «вставить» вместо того, чтобы также проверить, если список пуст ...
#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 << "Menu\n";
cout << "1. Add food, beverage etc.\n";
cout << "2. Show the list of food(s), beverage(s) etc.\n";
cout << "3. Update your current weight\n";
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;
}
}
Позвольте мне знать, если вам нужно больше кода?
Надеюсь на вашу помощь!
Почему вы используете '' и теги для создания кода HTML? – Shoe