Sage Harpuia
Sage Harpuia

Reputation: 356

C - passing sub linked list to functions

I'm working in this simple program with list but I'm having a bad time passing pointers.

Let's say I have a struct for students

typedef struct student{
    char lastname[50];
    int age;
    int std_id;
    struct student * next;
    struct student * prev;
}stdn;

and I have another struct for classes

typedef struct class{
    char class_id[3];
    struct class * next;
    struct class * prev;
    struct student * stdn_list;
}clss;

so basically I have this list with classes, and each class contain a sublist with students.

so, here is the function creating the class list, and it works!

void create_class_list(clss ** root, clss * node){
    clss * root_aux;
    if(!(*root)){
        (*root) = node;
    }
    else{
        root_aux = (*root);
        while(root_aux->next != NULL){
            root_aux = root_aux->next;
        }
        node->prev = root_aux;
        root_aux->next = node;
    }
}

my problem is when I need to work with the sublist in each node of the class list.

Here is the function in charge of creating the sublist, and it works

    void assign_student(clss ** root, stdn * node, char * class_id){
        clss * root_aux;
        stdn * stdn_aux;
        root_aux = (*root);

        while(root_aux != NULL){
            if(strcmp(root_aux->class_id,class_id) == 0) 
                break;
            root_aux = root_aux->next;
        }
        if(root_aux != NULL){
            if(root_aux->stdn_list == NULL){
                root_aux->stdn_list = node;
            }
            else{
                stdn_aux = root_aux->stdn_list;
                while(stdn_aux->next != NULL){
                    stdn_aux = stdn_aux->next;
                }
                node->prev = stdn_aux;
                stdn_aux->next = node;
            }
        }
    }

Basically this function looks for the specific class and add the student to that class.

My problem is when I want to delete a student, or sort the list using an algorithm like bubblesort, here's an example of a function to delete a student.

void delete_student(clss ** root, int stdn_id){
    clss * root_aux;
    stdn * stdn_aux;
    stdn * temp;
    int deleted=0;

    root_aux = (*root);
    while(root_aux != NULL){
        stdn_aux = root_aux->stdn_list;
        //try with root first//
        if(stdn_aux->std_id == stdn_id){
            temp = stdn_aux;
            stdn_aux = stdn_aux->next;
            stdn_aux->prev = NULL;
            free(temp);
            deleted = 1;
        }
        //if the student isn't the root
        if(deleted == 0){
            stdn_aux = stdn_aux->next;
            while(stdn_aux != NULL){
                if(stdn_aux->std_id == stdn_id){
                    temp = stdn_aux;
                    //link the prev element with the next element
                    stdn_aux->prev->next = stdn_aux->next;
                    //link the next element with the prev element
                    stdn_aux->next->prev = stdn_aux->prev;
                    stdn_aux = stdn_aux->next;
                    free(temp);
                    deleted = 1;
                    break;
                }
                stdn_aux = stdn_aux->next;
            }
        } 
        if(deleted == 1){
            break;
        }
        root_aux = root_aux->next;
    }
}

The function just looks like don't delete the element from the list and I'm not sure if is something with how I pass the pointer to the function, or how I create the list in first place.

Upvotes: 0

Views: 54

Answers (1)

Scooter
Scooter

Reputation: 7061

When you delete a student node that is at the head of the Student List, you need to re-assign to root_aux->stdn_list as you are deleting the node it currently points to. That must be why it appers you are not deleting a student node.

root_aux->stdn_list = stdn_aux->next; 

There are other issues with regard to processing that should be wrapped in if statements to prevent the program from core dumping:

Before you begin processing the student list, you need to first check that there is a student list. That is, check that the class variable pointing to the student list - root_aux->stdn_list - is not NULL.

Before doing the following statements, make sure that stdn_aux->next is not NULL, that is, there is stuff beyond the root node you are deleting.

stdn_aux = stdn_aux->next;
stdn_aux->prev = NULL;

Before making this assignment

stdn_aux->next->prev = stdn_aux->prev;

check that stdn_aux->next is not null because this is the last node in the student list.

Upvotes: 1

Related Questions