我正在练习简单的链表问题,并编写了一个简单的程序来删除链表中的重复项。当我使用g++
进行编译时,出现以下错误:
$ g++ removeDuplicateNode.cpp
$ ./a.out < test
./a.out: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./a.out)
我尝试使用g++ removeDuplicateNode.cpp -std=c++11
,并且代码运行正常。该程序如下所示,有人可以帮助我理解为什么会出现此错误,以及为什么我需要在此处使用 -std=c++11
标志
#include <iostream>
struct Node {
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};
void print(Node *root)
{
Node *temp = root;
while (temp != NULL) {
std::cout << temp->data << " ";
temp = temp->next;
}
}
Node *removeDuplicates(Node *root);
int main()
{
// your code goes here
int T;
std::cin >> T;
while (T--) {
int K;
std::cin >> K;
Node *head = NULL;
Node *temp = head;
for (int i = 0; i < K; i++) {
int data;
std::cin >> data;
if (head == NULL)
head = temp = new Node(data);
else {
temp->next = new Node(data);
temp = temp->next;
}
}
Node *result = removeDuplicates(head);
print(result);
std::cout << std::endl;
}
return 0;
}
/*This is a function problem.You only need to complete the function given below*/
void removeNode(Node *prev, Node *curr)
{
prev->next = curr->next;
curr->next = nullptr;
delete curr;
}
// root: head node
Node *removeDuplicates(Node *root)
{
Node *tmp = root;
while (tmp != nullptr) {
int data = tmp->data;
if (tmp->next != nullptr) {
int nextData = tmp->next->data;
if (data == nextData) {
removeNode(tmp, tmp->next);
continue;
}
}
tmp = tmp->next;
}
return root;
}