Implementing Multi-Linked List

hi there i am working on a project with implement a multi-linked list. in this list, nodes have a student's name,last name,birthday,address vs. i thought that i made it but i suppose i am implementing two separate linked lists, not a multi linked list which have 2 different header ( list->fn_head and list->ln_head ) and i recognize this problem when i delete a student by his name it deletes the node, but when i check this student's last name it says "this students exist". i appreciated if you can help me.

here are my structures

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
typedef struct node {

    int birth_date;
    int zipcode;
    int phone_num;
    char first_name[50];
    char last_name[50];
    char city[50];
    char address[50];
    char email_addr[50];

    struct node* fn_next;
    struct node* fn_pre;
    struct node* ln_next;
    struct node* ln_pre;
    struct node* birdat_next;
    struct node* birdat_pre;

} NODE;

typedef struct {

    int fn_count;
    int ln_count;

    NODE* fn_head;
    NODE* ln_head;
    
    NODE* fn_tail;
    NODE* ln_tail;

}LIST;


and my adding functions by name and surname;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
void add_fn_Node(LIST* list, NODE* pnew_stu) {

    NODE* temp = list->fn_head;

    if( list->fn_head == NULL ) {

            pnew_stu->fn_next = list->fn_head;
            pnew_stu->fn_pre = list->fn_head;


            list->fn_head = pnew_stu;

            list->fn_count = 1;

            return;

        }

        else {

            temp = list->fn_head;

            if ( (strcmp( pnew_stu->first_name, temp->first_name )) <= 0 ) { // Condition for add before the first node

                pnew_stu->fn_next = temp;
                pnew_stu->fn_pre = temp->fn_pre;
                temp->fn_pre = pnew_stu;

                list->fn_head = pnew_stu;

                list->fn_count++;

                return;

            }

            else {
                
                 while ( temp->fn_next != NULL ) { // Condition for add middle

                        if ( (strcmp( pnew_stu->first_name, temp->first_name ) >= 0 ) && (strcmp( pnew_stu->first_name, temp->fn_next->first_name) < 0)) {

                        pnew_stu->fn_next = temp->fn_next;
                        pnew_stu->fn_pre = temp;
                        temp->fn_next->fn_pre = pnew_stu;
                        temp->fn_next = pnew_stu;

                        list->fn_count++;

                        return;
                    }

                        temp = temp->fn_next;

                    }

                if ( temp->fn_next == NULL ) { // Condition for add to end

                    temp->fn_next = pnew_stu;
                    pnew_stu->fn_pre = temp;
                    pnew_stu->fn_next = NULL;

                    list->fn_tail = pnew_stu;

                    list->fn_count++;

                    return;

                }
            }

        }
}

void add_ln_Node(LIST* list, NODE* pnew_stu) {

    NODE* temp = list->ln_head;

    if( list->ln_head == NULL ) {

            pnew_stu->ln_next = list->ln_head;
            pnew_stu->ln_pre = list->ln_head;


            list->ln_head = pnew_stu;

            list->ln_count = 1;

            return;

        }

        else {

            temp = list->ln_head;

            if ( (strcmp( pnew_stu->last_name, temp->last_name )) <= 0 ) { // Condition for add before the first node

                pnew_stu->ln_next = temp;
                pnew_stu->ln_pre = temp->ln_pre;
                temp->ln_pre = pnew_stu;

                list->ln_head = pnew_stu;

                list->ln_count++;

                return;

            }

            else {


                 while ( temp->ln_next != NULL ) { //Condition for add middle

                        if ( (strcmp( pnew_stu->last_name, temp->last_name ) >= 0 ) && (strcmp( pnew_stu->last_name, temp->ln_next->last_name) < 0)) {

                        pnew_stu->ln_next = temp->ln_next;
                        pnew_stu->ln_pre = temp;
                        temp->ln_next->ln_pre = pnew_stu;
                        temp->ln_next = pnew_stu;
                        
                        list->ln_count++;

                        return;
                    }

                        temp = temp->ln_next;

                    }

                if ( temp->ln_next == NULL ) { // Condition for add to end

                    temp->ln_next = pnew_stu;
                    pnew_stu->ln_pre = temp;
                    pnew_stu->ln_next = NULL;

                    list->ln_tail = pnew_stu;

                    list->ln_count++;

                    return;

                }
            }

        }
}


and this is the block where program read the text file line by line and call for adding operation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
while ( !feof(myfile) ) {

        NODE* pnew_stu;

        if( !(pnew_stu = (NODE*) malloc(sizeof(NODE))) ) {

            printf("ERROR NOT ENOUGH MEMORY!!!\n");
            exit(100);
        }

        fscanf(myfile,"%s", &(pnew_stu->first_name) );
        fscanf(myfile,"%s", &(pnew_stu->last_name) );
        fscanf(myfile,"%s", &(pnew_stu->email_addr) );
        fscanf(myfile,"%d", &(pnew_stu->phone_num) );
        fscanf(myfile,"%s", &(pnew_stu->address) );
        fscanf(myfile,"%s", &(pnew_stu->city) );
        fscanf(myfile,"%d", &(pnew_stu->zipcode) );

        add_fn_Node(list,pnew_stu);
        add_ln_Node(list,pnew_stu);

}
}
for a long program like this, you need to post all your code so we can just drop it into the compiler and run it through a debugger

without all the code, we have to eyeball the code and run all these lines in our heads, which is not very pleasant

you also need to include a representative chunk of a data file which would cause the problem that you are describing
Last edited on
well mate i have nearly 10 header files. but if you want to see all codes i can upload my data so you can download it_?.
sry not enough time to do it

to avoid issues like this in the future, it helps to unit-test your code - write a test, write new code to pass the test, write a new test - without unit-tests, it will be difficult to write reliable software beyond your 10 header files

since it's a little late for that, you need to user a debugger and step through your code
Topic archived. No new replies allowed.