dimanche 8 octobre 2017

insert node in a linked list runtime error

the code is supposed to add a node after the previous node argument, given the city name and the head pointer. I get a runtime error, however, when I run the code, why?

 city* addCity(city *head, city *previous, string cityName )
    {
        city* add = new city;
        add->name=cityName;
        add->next = NULL;
        city* tmp = new city;
        tmp = head;


        if(tmp==NULL){
            tmp = add;
        }

        while(tmp != NULL && tmp != previous){
            tmp = head;
            tmp = tmp->next;
        }


    if(tmp == previous){
        add->next = previous->next;
        tmp->next = add;
        head = tmp;
        return head;
        } 


        }

Aucun commentaire:

Enregistrer un commentaire