Search code examples
cstructlinked-listdoubly-linked-list

can't get response from a function in c


I'm writing a program that creates a doubly linked list and removes a element with negative value from it. Everything pretty much works, except for the part when I called the modify function and when I try to delete it, program crashes. Any suggestions?

/*
*Given a doubly linked lists with +ve and -ve key values.
*Write a function to delete all the nodes with negative key values.
*/

#include<stdio.h>
#include<stdlib.h>

struct list {
    int data;
    struct list *next;
    struct list *prev;
};
struct list *head = NULL;

struct list* create(int);
void modify(struct list*);

int main(void) {

int n, i, value;
struct list *temp;

printf("Enter the count of node :");
scanf("%d",&n);
for (i = 0; i < n; i ++) {
    printf("Enter the value of node: ");
    scanf("%d",&value);
    create(value);
}
temp = head;
printf("\nDoubly linked list is created and the list is as follows : \n");
while (temp != NULL) {
    printf("%d ",temp -> data);
    temp = temp -> next;
}
    modify(head);
}

struct list* create(int value) {
    struct list *new_node, *temp;
    temp = head;
    new_node = (struct list*)malloc(sizeof(struct list));
    new_node -> data = value;
    new_node -> next = NULL;
    new_node -> prev = NULL;
    if (head == NULL) {
        head = new_node;
    }
    else {
        while (temp -> next != NULL) {
            temp = temp -> next;
        }

        temp -> next = new_node;
        new_node -> prev = temp;
    }
    return head;
}

void modify(struct list *head) {
    struct list *current_node, *prev_node, *next_node, *temp;
    temp = head;
    while (temp -> next != NULL) {
        if (temp -> data < 0) {
            current_node = temp;
            prev_node = temp -> prev;
            next_node = temp -> next;
            prev_node -> next = next_node;
            next_node -> prev = prev_node;
            free(current_node);

        }
    }
    printf("\nThe modified doubly linked list is : \n ");
    temp = head;
    while (temp -> next != NULL) {
        printf("%d",temp -> data);
        temp = temp -> next;
    }
}

Solution

  • The create() function returns a linked list item. so you have to assign the return value to an item. Also the definition of pointers inside the struct is completely wrong.

    struct list {
        int data;
        struct list *next;
        struct list *prev;
    };
    struct list *head = NULL;
    struct list* create(int); //function prototype
    void modify(struct list*);//function prototype
    
    int main(void) {
    
    int n, i, value;
    struct list *temp;
    
    printf("Enter the number of nodes :");
    scanf("%d",&n);
    for (i = 0; i < n; i ++) {
        printf("Enter the value of node: ");
        scanf("%d",&value);
        create(value);
    }
    temp = head;
    printf("\nDoubly linked list is created and the list is as follows : \n");
    while (temp != NULL) {
        printf("%d ",temp -> data);
        temp = temp -> next;
    }
        modify(head);
    }
    
    void create(int value) {
        struct list* point = head;
        while(point->next){
          if(point->data != value)
              point = point->next;
          else{
             printf("Data exists\n");
             return NULL;
          }
        }
        struct list* item = (struct list*)malloc(sizeof(struct list));
        item->data = value;
        item->next = NULL;
        item->prev = point;
    }
    
    void modify(struct list *head) {
        struct list *current_node, *prev_node, *next_node, *temp;
        temp = head;
        while (temp -> next != NULL) {
            if (temp -> data < 0) {
                temp->prev->next = temp->next;
                temp->next->prev = temp->prev;
                free(temp);
            }
            temp = temp->next;
        }
        printf("\nThe modified doubly linked list is : \n ");
        temp = head;
        while (temp -> next != NULL) {
            printf("%d",temp -> data);
            temp = temp -> next;
        }
    }
    

    I hope this will work for you.