我里面INT主要代码:将节点添加到链表(C++)的末尾?
node *run=NULL, *head=NULL, *temp=NULL;
for (int x = 1; x <= 10; x++)
{
temp = new node();
temp->value = x*10;
temp->next = NULL;
temp -> prev = NULL;
if (head == NULL)
{
head = temp;
}
else
{
run = head;
while (run->next != NULL)
{
run = run->next;
}
temp -> prev = run;
run->next = temp;
}
}
run = head;
cout << "ORIGINAL:" << endl;
while (run != NULL)
{
printf("%d\n", run->value);
run = run->next;
}
cout << endl << endl;
//=============== ADD AT THE END ========================
int xb = 105; //Value I want to add
run = head;
while (run -> next -> value > xb)
{
run = run -> next;
}
temp = new node();
temp -> prev = run;
temp -> value = xb;
temp -> next = NULL;
run -> next = temp;
run = head;
cout << "ADDED 105:" << endl;
while (run != NULL)
{
printf("%d\n", run->value);
run = run->next;
}
我一直在试图找出这个问题在我的代码,但没有增加新的节点(105)我做似乎工作。原来的工作完全正常,输出
10 20 30 40 50 60 70 80 90 100
但插入的代码只输出
10 105
,而不是
10 20 30 40 50 60 70 80 90 100 105
做自己的忙,并且不要分配一个新的节点,直到你的管理指针放在应该插入节点的地方。你在cutline下面设置'temp'是完全错误的,你从来没有正确地连接temp-> next。而且,你的帖子应该包括*期望的*顺序结果以及失败的结果。 *以前的结果虽然有趣,但并不真正相关。 – WhozCraig