vendredi 3 avril 2020

Why is copying a linked list to a vector giving a segmentation fault?

int main() {
    LinkedList list;
    for (int i = 1; i <= 5; i++)
        list.appendFront(i);

    vector<int> list_copy;
    Node *temp = list.head;
    int i = 0;
    while (temp->next)
    {
        list_copy[i] = temp->info; //segmentation fault at this line
        temp = temp->next;
        i++;
    }

    for (auto i : list_copy)
        cout << i;
}

Please note that I am using my implementation of Linked List and it works fine.

Aucun commentaire:

Enregistrer un commentaire