mardi 25 juin 2019

libstdc++ error when not using -std=c++11

I was practicing simple linked list problems and wrote a simple program to remove duplicates in a linked list. When I compiled it with g++ I get the below error:

$ g++ removeDuplicateNode.cpp
$ ./a.out < test
./a.out: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./a.out)

I tried with g++ removeDuplicateNode.cpp -std=c++11 and the code runs perfectly. The program is given below, could anyone help me understand why this error would come and why I need the -std=c++11 flag here

#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;
}

Aucun commentaire:

Enregistrer un commentaire