C++ 从链接列表中删除节点工作不正常
我在删除链接列表中的节点时遇到问题。这是我的代码(除了C++ 从链接列表中删除节点工作不正常,c++,linked-list,C++,Linked List,我在删除链接列表中的节点时遇到问题。这是我的代码(除了addElement函数,它可以正常工作)。我通过输入初始化列表中的节点,然后调用函数删除右侧具有较高值的节点,然后打印修改后的列表,最后删除列表。 问题是,对于某些输入,我的程序不能正常工作。 例如,如果我输入1,2,3,4,3,那么输出应该是1和3(第二个三),但我的输出只有1 有什么问题吗?我好像想不出来 编辑1:以下是包含的内容。 编辑2:包含addElement功能 #include <iostream> #includ
addElement
函数,它可以正常工作)。我通过输入初始化列表中的节点,然后调用函数删除右侧具有较高值的节点,然后打印修改后的列表,最后删除列表。
问题是,对于某些输入,我的程序不能正常工作。
例如,如果我输入1,2,3,4,3,那么输出应该是1和3(第二个三),但我的输出只有1
有什么问题吗?我好像想不出来
编辑1:以下是包含的内容。
编辑2:包含addElement功能
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
struct digits {
int value;
digits *next
};
int main() {
int a, b, c;
digits *head = NULL, *tale = NULL, *current;
cout << "How many digits you want in the linked list?" << endl;
cin >> a;
for (int i = 0; i < a; i++) {
cin >> b;
current = new digits;
current->value = b;
current->next = NULL;
if (head == NULL)
head = tale = current;
else {
tale->next = current;
tale = current;
}
if (!cin.good()) {
cin.clear();
cin.ignore(256, '\n');
cout << "Input can be int value! You can still input " << (a - i) - 1
<< " digits." << endl;
continue;
}
}
cout << "Want to add element? Press J if so, otherwise any other key" << endl;
cin >> add;
if (add == 'J') {
cin >> c;
addElement(&head, c);
}
removeElement(head);
for (current = head; current != NULL; current = current->next)
cout << current->value << endl;
current = head;
while (current != NULL) {
head = head->next;
delete current;
current = head;
}
}
// function which removes elements which have greater value on right side
void removeElement(struct digits *head) {
struct digits *current = head;
struct digits *max = head;
struct digits *temp;
while (current != NULL && current->next != NULL) {
if (current->next->value > max->value) {
temp = current->next;
current->next = temp->next;
free(temp);
} else {
current = current->next;
max = current;
}
}
}
void addElement(struct digits **head, int a) {
struct digits *newelem = (struct digits*) malloc(sizeof (struct digits));
newelem->value = a;
newelem->next = NULL;
struct digits *temp = *head;
if (*head == NULL) {
*head = newelem;
} else {
while (temp->next != NULL)
temp = temp->next;
temp->next = newelem;
}
}
#包括
#包括
#包括
结构数字{
int值;
数字*下一个
};
int main(){
INTA、b、c;
数字*head=NULL,*tale=NULL,*当前;
库塔;
for(int i=0;i>b;
当前=新数字;
当前->值=b;
当前->下一步=空;
if(head==NULL)
水头=功率=电流;
否则{
故事->下一个=当前;
故事=电流;
}
如果(!cin.good()){
cin.clear();
cin.ignore(256,“\n”);
cout next!=空){
如果(当前->下一个->值->最大->值){
温度=当前->下一步;
当前->下一步=临时->下一步;
免费(临时);
}否则{
当前=当前->下一步;
最大值=电流;
}
}
}
无效加法元素(结构数字**头,整数a){
结构数字*newelem=(结构数字*)malloc(sizeof(结构数字));
newelem->value=a;
newelem->next=NULL;
结构数字*温度=*磁头;
如果(*head==NULL){
*头=纽兰;
}否则{
while(临时->下一步!=NULL)
温度=温度->下一步;
temp->next=newelem;
}
}
为什么您的代码无法工作:
Node *last, *lastTail = NULL;
current = *head;
int last_val = INT_MAX;
while (current != NULL) {
if(current->value > last_val) {
last = current;
last_val = current->value;
current = current->next;
if(lastTail) {
lastTail->next = current;
}
else {
*head = current;
lastTail = current;
}
delete last;
}
else{
lastTail = current;
last_val = current->value;
current = current->next;
}
}
仔细查看以下代码:
while (current != NULL && current->next != NULL) {
if (current->next->value > max->value) {
temp = current->next;
current->next = temp->next;
free(temp);
}
您正在更改current
,但没有更改max
。代码中包含max
var似乎完全不相关
实际上,您从未进入代码的其他部分
,当前
值始终与最大
值进行比较,该值始终保持固定在1
,最终在当前
是最后一个节点(值=3)时,循环结束,因为最后一个节点的current->next!=NULL
失败。因此,它无法摆脱最后一个节点。因此,您得到:
1(第一个节点)和3(最后一个节点)
解决方案:尝试这种迭代方法:
Node *last, *lastTail = NULL;
current = *head;
int last_val = INT_MAX;
while (current != NULL) {
if(current->value > last_val) {
last = current;
last_val = current->value;
current = current->next;
if(lastTail) {
lastTail->next = current;
}
else {
*head = current;
lastTail = current;
}
delete last;
}
else{
lastTail = current;
last_val = current->value;
current = current->next;
}
}
如果你能从最后开始,朝着头部努力,这会变得容易得多
您不能直接使用单链表,但可以使用递归
首先,如果列表不是空的,请清除列表的其余部分。
然后查看右侧的节点是否较大,如果较大,则将其删除。
然后你就完蛋了
void scrub(digits* link)
{
if (link != nullptr)
{
scrub(link->next);
if (link->next != nullptr && link->next->value > link->value)
{
digits* scrap = link->next;
link->next = link->next->next;
delete scrap;
}
}
}
不要混合使用free和new-delete是对newt的匹配调用。这缺少所有包含项。此外,请使用格式化程序。如果您真的编写}
或在声明结构后放置int main()
,则很难对代码的结构进行推理。@RawN一方面,C++应该在<>代码> STD之上学习。另一方面,在过去的(不太高)级别下,C++开发人员应该理解引擎下的代码> STD < /C> >,并且能够做类似的事情(当然,在不太优化的情况下)。Nope,你需要一个调试器。您的代码总是删除当前值右边大于当前值->值==1的任何数字。它删除2,它删除3,它删除4,然后你有1->3,所以它删除3,就像你告诉它的那样。如果没有4(因为你已经删除了它),它怎么知道3<4呢?谢谢你的建议,我会记住的。Saurav Sahu的回答解决了我的问题。谢谢你的回答。我尝试了你的建议,但当输入为1 2 3 4 3时,它仍然输出1。最好遍历一次完整的列表,并在待删除列表中标记节点,然后在第二次遍历中执行删除。我添加了另一种方法,看一看。它肯定会起作用。跟踪lastTail节点(未删除的节点)和previousNodeValue的值。在这种方法中,您也不需要遍历列表两次。@screw4445如果您觉得答案有帮助,请向上投票。尝试了这个相当不错的概念代码,但它给了我一个错误main。cpp:64:20:error:“nullptr”未在此范围内声明对不起,重复评论。试图在我的.cpp文件的dir中通过cmd输入这个g++-std=c++0x*.cpp-o输出,但它不起作用。@screw4445它应该与任何符合c++11的编译器一起工作。如果你有问题,请使用<代码> null <代码>。是的,在项目属性中,将C++标准化为C++ 11,它工作了,虽然我会坚持NULL,就像它更好。谢谢你的解决方案,它很有效。