jeudi 28 décembre 2017

C++ list is working but giving error Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

Hi Guys i am facing this list problem where i want to clone a list with random pointers. But my code is giving me this error while cloning pointers. here i am adding only the function code but i am also attaching the link for full code. the error is

Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)

Node *createAnotherList(Node *head){
Node *temp = head, *start = head;
Node *firstPointer;
int count = 0;

while(temp != NULL){
    Node *newNode = new Node;
    newNode->data = temp->data;
    newNode->random = NULL;
    newNode->next = temp->next;
    temp->next = newNode;
    if(count == 0){
        firstPointer = newNode;
        count++;
    }
    temp = newNode->next;
}

//    printList(list1);

while(start != NULL){
    cout << start->data << " ";
    if(start->next)
        start->next->random = start->random->next;
    start = start->next ? start->next->next : start->next;

}

    return head;
}

i am getting error in 2nd while loop

start->next->random = start->random->next;

if someone knows what is the error please help me. and for more info please check the code from the link and error line is LINE 74

enter link description here

Aucun commentaire:

Enregistrer un commentaire