mardi 15 juin 2021

Abort trap 6 error in c++ when trying to delete the duplicate elements in a sorted linked list

I am a beginner in data structures and in stackoverflow.The below code is to delete the duplicate elements in the sorted linked list.The error is abort trap 6. and i tried debugging .In the line delete move inside the function deletduplicate() it shows Exception has occurred. SIGABRT.I don't understand what's the problem.

#include <iostream>
using namespace std;

struct node{
    int data;
    node* next;
};
class LinkedList{
    private:
        node* head;
        node* tail;
    public:
        LinkedList(){
            head = NULL;
            tail = NULL;
        }
    void addNode(int data){
        node* temp = new node();
        temp->data = data;
        temp->next = NULL;
        if(head == NULL){
            head = temp;
            tail = temp;
        }
        else{
            tail->next = temp;
            tail = tail->next;
        }
    }
    void printlist(){
        node* temp = head;
        while(temp){
            if(temp->data){
            cout<<temp->data<<" ";
            }
            temp = temp->next;
        }
        cout<<" "<<endl;
    }
    void sortList(){
        node* current = new node();
        node* move = new node();
        int swap;
        current = head;
        move = current->next;
        while(current != NULL){
            move = current->next;
            while(move != NULL){
                if(current->data < move->data){
                        swap = current->data;
                        current->data = move->data;
                        move->data = swap;
                }
                move = move->next;
            }
            current = current->next;
        }
    }
    void deleteduplicate(){
        node* current = new node();
        node* move = new node();
        node* temp;
        int i;
        current = head;
        move = current;
        while(current){
            move = current;
            while(move && move->next){
                i = 0;
                if(current->data == (move->next)->data){
                        temp = (move->next)->next;
                        delete current;
                        delete move;
                        current = temp;
                        move = current;
                        i = 1;
                }
                if(i == 0){
                move = move->next;
                }
            }
            current = current->next;
        }
    }
};
int main(){
    LinkedList root;
    root.addNode(1);
    root.addNode(20);
    root.addNode(14);
    root.addNode(4);
    root.addNode(4);
    root.addNode(0);
    root.printlist();
    root.sortList();
    root.printlist();
    root.deleteduplicate();
    root.printlist();
}

Aucun commentaire:

Enregistrer un commentaire