Home > OS >  Adding and deleting elements in doubly linked lists
Adding and deleting elements in doubly linked lists

Time:08-24

I wrote a code for adding and deleting from doubly linked list.First I add elements sequentially and print and it works.Then I delete elements and considering the element which is not in the list and it prints "Number was not found".About the other elements I can only delete the 300 others cannot be deleted.What is the problem?

#include<stdio.h>    
#include<stdlib.h>    
//Doubly Linked List                
struct n{    
    int x;   
    struct n * next;  
    struct n * prev;  
}; 
typedef struct n node;       
void print(node * r){    
    while(r!=NULL){  
        printf("%d ", r->x);    
        r=r->next;    
    }    
    printf("\n");    
}    
node * addSeq(node * r, int x){    
    if(r==NULL){//if linkedlist is empty, makes a node and puts an element    
        r=(node *)malloc(sizeof(node));    
        r->next=NULL;    
        r->prev=NULL;//as well as next prev must point to NULL    
        r->x=x;    
        return r;    
        }           
    if(r->x > x){//if not empty but less than first element root changes        
            node * temp = (node *)malloc(sizeof(node));    
            temp->x = x;    
            temp->next=r;    
            r->prev=temp;    
            return temp;    
        }    
    node * iter = r;    
    while(iter->next!=NULL && iter->next->x < x){    
        iter=iter->next;    
    }    
    node * temp = (node *)malloc(sizeof(node));    
    temp->next = iter->next;    
    iter->next=temp;    
    temp->prev=iter;    
    if(temp->next!=NULL)    
        temp->next->prev=temp;    
    temp->x=x;    
    return r;    
}    
node * delete(node *r, int x){    
    node *temp;    
    node *iter = r;     
    if(r->x == x){    
        temp = r;    
        r = r->next;    
        free(temp);    
        return r;    
    }    
    while(iter->next != NULL && iter->next->x != x){    
        iter = iter->next;    
    }    
    if(iter->next==NULL){    
        printf("Number was not found \n");    
        return r;    
    }    
    temp = iter->next; //iter points to the element that is previous of the element which we want     
   //to delete    
    iter->next = iter->next->next;       
    iter->next->prev=iter;//temp is the element that we want to delete    
    free(temp);    
    return r;    
}    
int main(){    
    node * root;    
    root = NULL;    
    root=addSeq(root, 400);    
    root=addSeq(root, 350);    
    root=addSeq(root, 500);    
    root=addSeq(root, 450);    
    root=addSeq(root, 300);     
    print(root);    
    root=delete(root, 410);    
    print(root);//Cannot find number and prints the elements    
    root=delete(root, 300);    
    print(root);    
    root=delete(root, 500);    
    print(root);    
    root=delete(root, 350);    
    print(root);    
}    

CodePudding user response:

When a node to delete is not the first in the list, this code is executed:

    temp = iter->next; //iter points to the element that is previous of the element which we want     
   //to delete    
    iter->next = iter->next->next;       
    iter->next->prev=iter;//temp is the element that we want to delete

Consider what happens then this node, iter->next, is the last in the list. Then iter->next->nexzt is null, and iter->next = iter->next->next; sets iter->next to null. Then iter->next->prev attempts to dereference this null pointer.

You must change how the code handles the last node in the list.

  • Related