1

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


        }
5
  • Can you please post the error message ! Commented Oct 9, 2017 at 4:55
  • 1
    city* tmp = new city; tmp = head; This will cause a memory leak in your program. Commented Oct 9, 2017 at 4:59
  • It's a singly linked list. I am running the code on an online platform, i'll send you a screenshot of the error shortly Commented Oct 9, 2017 at 5:00
  • Yeah, I am not sure if I can post images since I am a new user. Commented Oct 9, 2017 at 5:07
  • U can make this code lot more easier. No need to do all these things. Wait I'll share the code Commented Oct 9, 2017 at 5:30

4 Answers 4

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

This will run infinite times as tmp is reset to head in each iteration. tmp is just toggling in a cyclic way between values head and head->next in this loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Not necessarily infinite. At the beginning, the list is probably empty, head==NULL, so the tmp variable is assigned the add value before the above loop. Then the loop will enter the first iteration (due to tmp != NULL) and most probably crash on accessing tmp->next after tmp=head (i.e.: NULL) assignment.
0

Too much complication.

You have two general cases: either you're given the previous node in the list, so you don't have to seek it, or previous == NULL and you insert the new node at the front of the list (which also covers the case of an empty list).

city* addCity(city *head, city *previous, string cityName)
{
    city *newcity = new city;
    newcity->name = cityName;

    if (previous != NULL)
    {
        newcity->next = previous->next;
        previous->next = newcity;
    }
    else
    {
        newcity->next = head;
        head = newcity;
    }

    return head;
}

Note the return statement – your function has no access to the original head of the list, so the only way to inform the code outside that a new node appeared at the front of the list is returning a pointer to it. Of course, if a new node is inserted somewhere inside the list or appended at the end, the head doesn't change and the returned value equals the input head.

1 Comment

Thanks a lot! this worked. I am kinda new to data structures, do you have suggestions as to how become more familiar with it?
0

Your code has a lot of issues.

Firstly,

city* tmp = new city;
tmp = head;

Your are creating a new city for tmp but immediately assigning it to head. Assuming you just need a *tmp, so you can change city *tmp = new city; to city *tmp = head;

Then,

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

tmp == NULL means the list is empty. So you should add the new city to head and simply return from there. So, your code should be-

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

Continuing,

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

This is an infinite loop if tmp is not NULL. Because your are doing the same things in each iteration. It should be like-

tmp = head;

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

Forwarding,

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

I don't know why you are assigning tmp to head. Just removing head = tmp; should solve a lot of issues.

Comments

0

If you have the previous node that contain the next the node and the String you can directly add the new node to that.

Try this

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

This is enough too achieve the same ans.

For the first node to create check the following example

void Cites(){
    city *head = new city();
    head = addCity(head, head ,"First city name");
    //Do the rest of the codes
}

Edited: Now this code will work even to add node in the first. by passing previous as NULL

18 Comments

For the first code, you would get "nothing in path". The second one a run time error :/.
For the updated code, if you add a member in the beginning of the list you would end up with a runtime error. But it works if the new city was in the middle of the linked list.
== CURRENT PATH == Los Angeles -> Phoenix -> Denver -> Dallas -> Atlanta -> New York -> NULL === Runtime error
I am using moodle to submit the code and instead of the runtime error message we're supposed to update the linked list and put a city before LosAngeles in this case
@MahmoudAlmansouri You need to insert before or after the previous node ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.