Search code examples
clinked-listnodes

function to delete elements selected linked list C


Hi I'm creating a program for school where I have to:

  1. Create structures
  2. Create functions to print my linked list
  3. Create functions to insert an ordered element
  4. Delete elements with a minor year less than the year selected I created all a part the last one step. Can you help me to know what is the right way? This is my code:

STRUCTURE:

typedef struct dimension {
    int height;
    int length;
} DIMENSION;

typedef struct pic {
    DIMENSION d;
    char name[50];
    int year;
} PIC;

typedef struct node {
    PIC p;
    struct node *next;
} NODE;

PRINT LIST:

void printPic(PIC p) {
    printf("Autor: %s\nDimension\nHeight: %d\nLength: %d\nYear: %d\n", p.name, p.d.height, p.d.length, p.year);
}

void printList(NODE *head) {
    if(head->next==NULL) {
        printf("No element in the list!\n");
    } else {
        while(head->next != NULL) {
            head = head->next;
            printPic(head->p);
        }
    }
}

NEW PIC(ORDERED WITH AREA DIMENSION)

int area(PIC p) {
    return (p.d.height * p.d.length);
}

PIC createPic() {
    PIC newPic;
    
    printf("Author: ");
    fgets(newPic.name, 50, stdin);
    newPic.name[strlen(newPic.name)-1] = '\0';
    printf("Height: ");
    scanf("%d", &newPic.d.height);
    printf("\n");
    printf("Length: ");
    scanf("%d", &newPic.d.length);
    printf("\n");
    printf("Year: ");
    scanf("%d", &newPic.year);
    printf("\n");
    printf("\n");
    
    return newPic;
    
}

void insertPic(NODE *head) {
    
    NODE* newNode = malloc(sizeof(NODE));
    newNode->p = createPic();
    newNode->next = NULL;
    
    if(head==NULL) {
        head = newNode;
    } else {
        if(area(newNode->p) < area(head->p)) {
            newNode->next = head;
            head = newNode;
        } else {
            while(head->next != NULL && (area(newNode->p) > area(head->next->p))) {
                head = head->next;
            }
            newNode->next = head->next;
            head->next = newNode;
        }
    }
    
}

DELETE ELEMENTS WITH A MINOR YEAR THAN SELECTED YEAR:

Edited and now it works:

    void deletePic(NODE *head, int year) {
    if(head==NULL) {
        printf("No element in the list!\n");
    } else {
        while(head->next != NULL) {
            if(head->next->p.year < year) {
                NODE *p = head->next;
                head->next = p->next;
                free(p);
            } else {
                head = head->next;
            }
        }
    }
}

MAIN:

int main() {
    
    NODE *head = malloc(sizeof(NODE));
    head->next = NULL;
    int choice = -1;
    
    while(choice != 0) {
        printf("Select an action:\n");
        printf("Press 1 --> See list\n");
        printf("Press 2 --> Insert a new element\n");
        printf("Press 3 --> Delete elements with a minor year\n");
        printf("Press 0 --> Stop program\n");
        scanf("%d%*c", &choice);
        
        if(choice==1) {
            printList(head);
        }
        else if(choice==2) {
            insertPic(head);
        }
        else if(choice==3) {
            int year;
            printf("Choose an year\nAll elements with a smaller year will be eliminated\n");
            scanf("%d", &year);
            deletePic(head, year);
        }
        else if(choice==0) {
            printf("See you soon ;)\n");
        }
    }
    
}

Solution

  • Your deletePic function is broken in multiple places. Among them:

    • Dereferencing an indeterminate pointer, yearNode
    • Incorrect comparator (should use < ; not !=
    • free'ing an indeterminate pointer.

    The first and last of those are a recipe for disaster. If that function does what the menu claims it should, I think what you want is this:

    void deletePic(NODE *head, int year)
    {
        if (head == NULL)
        {
            printf("No element in the list!\n");
        }
        else
        {
            while (head->next != NULL)
            {
                if (head->next->p.year < year)
                {
                    NODE *p = head->next;
                    head->next = p->next;
                    free(p);
                }
                else
                {
                    head = head->next;
                }
            }
        }
    }