【发布时间】:2020-08-06 17:03:06
【问题描述】:
我正在执行一个程序,使用两个循环从未排序的链表中删除重复项。
该程序包括两个structs,用于定义Node和newNode。此外,它还包括两个用户定义的函数removeDuplicates 用于删除链表的重复项和printList 用于打印列表。
struct Node {
int data;
struct Node *next;
};
struct Node *newNode(int data) {
Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
};
/* Function to remove duplicates from an unsorted linked list */
void removeDuplicates(struct Node *start) {
struct Node *ptr1, *ptr2, *dup;
ptr1 = start;
while (ptr1 != NULL && ptr1->next != NULL) {
ptr2 = ptr1;
while (ptr2->next != NULL) {
if (ptr1->data == ptr2->next->data) {
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete (dup);
} else
ptr2 = ptr2->next;
ptr1 = ptr1->next;
}
}
}
void printList(struct Node *node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
我运行了几个测试用例,
案例 1 输入:12->11->12->21->41->43->21
Output(from the program) : 12->11->12->21->41->43->21
Required Output : 12->11->21->41->43
int main() {
struct Node *start = newNode(12);
start->next = newNode(11);
start->next->next = newNode(12);
start->next->next->next = newNode(21);
start->next->next->next->next = newNode(41);
start->next->next->next->next->next = newNode(43);
start->next->next->next->next->next->next = newNode(21);
printf("Linked List before removing duplicates");
printList(start);
removeDuplicates(start);
printf("Linked List after removing duplicates");
printList(start);
}
案例 2 输入:10->12->11->11->12->11->10
Output : 10->12->11
int main() {
struct Node *start = newNode(10);
start->next = newNode(12);
start->next->next = newNode(11);
start->next->next->next = newNode(11);
start->next->next->next->next = newNode(12);
start->next->next->next->next->next = newNode(11);
start->next->next->next->next->next->next = newNode(10);
printf("Linked List before removing duplicates");
printList(start);
removeDuplicates(start);
printf("Linked List after removing duplicates");
printList(start);
}
该程序适用于一个测试用例,而不适用于其他用例。我在代码中遗漏了什么?
【问题讨论】:
-
您是否尝试过在调试器中逐行运行代码,同时监控所有变量的值?如果没有,那么您可能想阅读以下内容:What is a debugger and how can it help me diagnose problems? 您可能还想阅读以下内容:How to debug small programs
-
不要做
delete(dup);你不会在任何地方做new。另外,制作一个minimal reproducible example -
ptr1 = ptr1->next;这行不应该在外循环的末尾吗?目前,它位于内部循环的末尾。 -
警告:好像有人在教你 C 并告诉你它是 C++。别被骗了。 Upgrade your reference materials 以便您了解差异。
-
如果你让
newNode、Node *newNode(int data, Node * next){ Node *temp = new Node; temp->data = data; temp->next = next; return temp; }变得更聪明一点,start->next->next->next->next->next->nexts 的巨大质量可以简化为start = newNode(1, newNode(2, newNode(3, NULL)))之类的东西
标签: c++ linked-list