0

I have problem with deleting first and last element of linked list. When I am trying to delete first element, my code do nothing (when I print a list, deleted element is still there). When I am trying to delete the last one, console shows core dumped.

Here is my code:

void Delete_element(point del, node *elem) {

    struct node *temp = elem;

    if(elem->p.x==del.x && elem->p.y == del.y) {   
        elem=elem->next;
        return;
    } else {
        while(elem->next->next!=NULL) {
            if(elem->next->p.x==del.x && elem->next->p.y==del.y) {
                temp=elem->next;
                elem->next=elem->next->next;
                elem->prev=temp->prev;
                return;
            }

            temp=temp->next;
        }
    }

    if(elem->next->p.x==del.x && elem->next->p.y==del.y) {
        elem->next=NULL;
    }
}

EDIT: After fixes

void Delete_element(point del, node *& elem){
     struct node *temp = elem;
if(elem->p.x==del.x && elem->p.y == del.y){
        temp = elem->next;
        free(elem);
        elem=temp;
    return;
}else{
    while(elem->next->next!=NULL)
    {
        if(elem->next->p.x==del.x && elem->next->p.y==del.y)
        {
            temp=elem->next;
            elem->next=elem->next->next;
            elem->next->prev=elem;
            return;
        }

        elem=elem->next;
    }}
    if(elem->next->p.x==del.x && elem->next->p.y==del.y){
            elem->next=NULL;
            return;
    }

}

Now removing an middle element is broken.

Please help

2
  • 1
    Wow, there's not much null-testing going on. Have you tried stepping through your code in a debugger? Commented Dec 8, 2015 at 21:12
  • Yes, I fixed problem with last element, but I still cannot delete first one. Commented Dec 8, 2015 at 21:16

2 Answers 2

1

Firstly, you are not actually deleting anything. You are, however, leaving dangling pointers which result in a memory leak. In each case you should be deleting something. This is why you still see the data instead of garbage or some kind of crash.

Second, you look to have an infinite loop. When you hit your while loop you never change elem so elem->next->next never changes.

Third, why are you deleting elem->next in your while loop? Delete elem to avoid confusion.

EDIT:

You can't change elem like that. Think of elem like an integer or float you pass in. If you want elem to maintain the head change you're going to have to pass in a pointer to elem which ends up being a pointer to a pointer.

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

1 Comment

Thank you for your answer. I have already fixed infinitive loop. I have also added free() function, and I see garbage, when I delete first element. But what should I do to make a pointer to second element. When I print a list, I want to see only second and third element, when I delete first. But now i see garage, second and third. What should I do?
0

I believe you have at least two bugs:

In order to modify the first element you must pass a reference to the first pointer as you intent to modify this pointer. use:

void Delete_element(point del, node *& elem)

Removing an element from the middle seems to be broken. instead of:

elem->prev=temp->prev

you should have:

elem->next->prev=element

1 Comment

Thank you for your answer. Removing first and last element working now, but now, removing an element from the middle is broken, it also removes the first element.

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.