我想在列表末尾添加一个节点,并且此代码可以处理此问题,但是当我添加节点时,它将删除第一个节点。第二个节点成为第一个,第二个节点成为第三个节点。问题是什么 ?在列表末尾添加节点
struct Clothes
{
int item_id;
string name_of_clothing;
Clothes * next;
Clothes * back;
}*new_item, *temp, *last, *list;
int add_clothing() {
string name_of_clothing, item_id;
cout << "Enter ID\t: ";
cin >> item_id;
cout << "Enter Name of item\t: ";
cin >> name_of_clothing;
string new_item[2] = { item_id, name_of_clothing};
add_New_item(new_item[0], new_item[1]);
system("PAUSE");
palce_and_order_menu();
return 0;
}
void add_New_item(string item_id, string name_of_clothing)
{
new_item = new Clothes();
new_item->item_id = std::stoi(item_id);
new_item->name_of_clothing = name_of_clothing;
if (temp == NULL)
{
temp = last = new_item;
cout << "\n>Element inserted in empty list\n";
}
else
{
while (temp->next != NULL){
temp = temp->next;
}
last->next = new_item;
new_item->back = last;
last = new_item;
cout << "\n>Element inserted \n";
}
}
什么是最后一个指针应该代表什么?名单的头,还是尾巴?你的整体逻辑似乎是正确的,但如果你能澄清'临时'和'最后'应该做的事情会有所帮助。 –
如果'temp'指向列表的头部,然后在'while'循环中修改它并且不恢复其以前的值,这就是您丢失了列表头部的原因。 –
@GAURANGVYAS是它的头。我怎样才能恢复以前的价值? – Sava