问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
```c++
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class Food
{
public:
string ID;
string name;
/*int tasty;
string add;*/
float price;
public:
Food()
{
ID = "0";
name = "0";
price = 0;
}
Food(string id, string n, float p );
void Print();
friend class node;
};
Food::Food(string id, string n, float p)
{
ID = id;
name = n;
price = p;
}
void Food::Print()
{
cout <<setw(5)<<this->ID;
cout << setw(15) << this->name;
cout << setw(5) << this->price << endl;
}
class node
{
public:
Food data;
node* next;
friend class Menu;
public:
node();
node(const Food& t1);
void print();
};
node::node()
{
next = NULL;
}
node::node(const Food& t1)
{
this->data = t1;
next = NULL;
}
void node::print()
{
this->data.Print();
}
class Menu
{public:
node* head;
node* tail;
node* temp;
public:
Menu();
Menu(const node& t1);
void Inserthead(const node&t2);
void Inserttail(const node&t3);
void Delete(const node&t4);
void print1();
};
Menu::Menu()
{
head = NULL;
tail = NULL;
temp = NULL;
}
Menu::Menu(const node& t1)
{
head = new node(t1);
tail = head;
temp = head;
}
void Menu::Inserthead(const node&t2)
{
if (head == NULL)
{
head = new node(t2);
}
else
{
temp = new node(t2);
temp->next = head;
head = temp;
}
}
void Menu::Inserttail(const node& t3)
{
if (head == NULL)
{
head = new node(t3);
head->next = tail;
}
else
{
tail = new node(t3);
tail->next = temp;
tail = temp;
}
}
void Menu::Delete(const node& t4)
{
if (head->data.ID == t4.data.ID)
{
temp = head;
head = head->next;
delete temp;
}
else if (tail->data.ID == t4.data.ID)
{
temp = head;
while (temp->next->data.ID != tail->data.ID)
{
temp++;
}
tail = temp;
temp = temp->next;
delete temp;
}
else
{
temp = head;
while (temp->next->data.ID != t4.data.ID)
{
temp++;
}
node* tamp;
tamp = temp->next;
temp->next = temp->next->next;
delete tamp;
}
}
void Menu::print1()
{
temp = head;
while (temp!=tail)
{
temp->data.Print();
temp++;
}
tail->data.Print();
}
int main()
{
Food* a;
a = new Food[3];
for (int i = 0; i < 3; i++)
{
cin >> a[i].ID >> a[i].name >> a[i].price;
}
/*for (int i = 0; i < 3; i++)
{
a[i].Print();
}*/
node* b;
b = new node[3];
for (int i = 0; i < 3; i++)
{
b[i] = a[i];
}
/*for (int i = 0; i < 3; i++)
{
b[i].print();
}*/
Menu C;
for (int i = 0; i < 3; i++)
{
C.Inserttail(b[i]);
}
C.print1();
delete[]b;
delete[]a;
return 0;
}

