Note that there are some explanatory texts on larger screens.

plurals
  1. POabout implementing a multi linked list
    primarykey
    data
    text
    <p>I am working on a project which is similar to an address book. Firstly we have a number of students in a text file. I am going to implement a multi linked list which this list have 2 head+tail pointer (head+tail pointer for name,surname). These pointers show the same list but different locations because I am adding the nodes in ascending order and use double pointers to print the list backwards. The problem is I implemented the list by adding nodes by name and surname and the operation goes successfully when I insert another one. But I realized that when I deleted a node by her/his "name" and print the list again the student doesn't exist, but when I print the list by "surname" the student does exist. Then I recognize that I was implemented two separate linked lists. I was told to implement only ONE add and remove function. But how can I add a node by its first name pointers and surname pointers together? I hope I explained my issue understandably. Here are my blocks of code.</p> <p>My structures:</p> <pre><code>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; </code></pre> <p>The code block where I read the file line by line and call the add functions:</p> <pre><code>while ( !feof(myfile) ) { NODE* pnew_stu; if( !(pnew_stu = (NODE*) malloc(sizeof(NODE))) ) { printf("ERROR NOT ENOUGH MEMORY!!!\n"); exit(100); } fscanf(myfile,"%s", &amp;(pnew_stu-&gt;first_name) ); fscanf(myfile,"%s", &amp;(pnew_stu-&gt;last_name) ); fscanf(myfile,"%s", &amp;(pnew_stu-&gt;email_addr) ); fscanf(myfile,"%d", &amp;(pnew_stu-&gt;phone_num) ); fscanf(myfile,"%s", &amp;(pnew_stu-&gt;address) ); fscanf(myfile,"%s", &amp;(pnew_stu-&gt;city) ); fscanf(myfile,"%d", &amp;(pnew_stu-&gt;zipcode) ); add_Node(list,pnew_stu); } </code></pre> <p>And finally my add functions:</p> <pre><code>void add_fn_Node(LIST* list, NODE* pnew_stu) { NODE* temp = list-&gt;fn_head; if( list-&gt;fn_head == NULL ) { pnew_stu-&gt;fn_next = list-&gt;fn_head; pnew_stu-&gt;fn_pre = list-&gt;fn_head; list-&gt;fn_head = pnew_stu; list-&gt;fn_count = 1; return; } else { if ( (strcmp( pnew_stu-&gt;first_name, temp-&gt;first_name )) &lt;= 0 ) { // Basa Ekleme Kosulu pnew_stu-&gt;fn_next = temp; pnew_stu-&gt;fn_pre = temp-&gt;fn_pre; temp-&gt;fn_pre = pnew_stu; list-&gt;fn_head = pnew_stu; list-&gt;fn_count++; return; } else { while ( temp-&gt;fn_next != NULL ) { // Ortaya Ekleme Kosulu if ( (strcmp( pnew_stu-&gt;first_name, temp-&gt;first_name ) &gt;= 0 ) &amp;&amp; (strcmp( pnew_stu-&gt;first_name, temp-&gt;fn_next-&gt;first_name) &lt; 0)) { pnew_stu-&gt;fn_next = temp-&gt;fn_next; pnew_stu-&gt;fn_pre = temp; temp-&gt;fn_next-&gt;fn_pre = pnew_stu; temp-&gt;fn_next = pnew_stu; list-&gt;fn_count++; return; } temp = temp-&gt;fn_next; } if ( temp-&gt;fn_next == NULL ) { // Sona Ekleme Kosulu temp-&gt;fn_next = pnew_stu; pnew_stu-&gt;fn_pre = temp; pnew_stu-&gt;fn_next = NULL; list-&gt;fn_tail = pnew_stu; list-&gt;fn_count++; return; } } } } void add_ln_Node(LIST* list, NODE* pnew_stu) { NODE* temp = list-&gt;ln_head; if( list-&gt;ln_head == NULL ) { pnew_stu-&gt;ln_next = list-&gt;ln_head; pnew_stu-&gt;ln_pre = list-&gt;ln_head; list-&gt;ln_head = pnew_stu; list-&gt;ln_count = 1; return; } else { if ( (strcmp( pnew_stu-&gt;last_name, temp-&gt;last_name )) &lt;= 0 ) { // Basa Ekleme Kosulu pnew_stu-&gt;ln_next = temp; pnew_stu-&gt;ln_pre = temp-&gt;ln_pre; temp-&gt;ln_pre = pnew_stu; list-&gt;ln_head = pnew_stu; list-&gt;ln_count++; return; } else { while ( temp-&gt;ln_next != NULL ) { // Ortaya Ekleme Kosulu if ( (strcmp( pnew_stu-&gt;last_name, temp-&gt;last_name ) &gt;= 0 ) &amp;&amp; (strcmp( pnew_stu-&gt;last_name, temp-&gt;ln_next-&gt;last_name) &lt; 0)) { pnew_stu-&gt;ln_next = temp-&gt;ln_next; pnew_stu-&gt;ln_pre = temp; temp-&gt;ln_next-&gt;ln_pre = pnew_stu; temp-&gt;ln_next = pnew_stu; list-&gt;ln_count++; return; } temp = temp-&gt;ln_next; } if ( temp-&gt;ln_next == NULL ) { // Sona Ekleme Kosulu temp-&gt;ln_next = pnew_stu; pnew_stu-&gt;ln_pre = temp; pnew_stu-&gt;ln_next = NULL; list-&gt;ln_tail = pnew_stu; list-&gt;ln_count++; return; } } } } </code></pre> <p>My delete functions:</p> <pre><code>void del_fn_name(LIST* list, char* str_key) { NODE* temp; int num,counter=1,flag; temp = list-&gt;fn_head; if( list-&gt;fn_head == NULL ) { printf("The list is EMPTY!!!\n"); return; } flag = search_fn(list,str_key); if ( flag ) { printf("Enter the number of student you want to delete : "); scanf("%d", &amp;num); if( strcmp( list-&gt;fn_head-&gt;first_name, str_key ) == 0 ) { // Bastan Silme Kosulu if( num == counter ) { list-&gt;fn_head-&gt;fn_next-&gt;fn_pre = list-&gt;fn_head; list-&gt;fn_head = list-&gt;fn_head-&gt;fn_next; free(list-&gt;fn_head); printf("%s is deleted and the new header is %s\n", str_key, list-&gt;fn_head-&gt;first_name ); return; } counter++; } temp = temp-&gt;fn_next; while ( temp-&gt;fn_next != NULL ) { if( strcmp( temp-&gt;first_name, str_key ) == 0 ) { if( num == counter ) { temp-&gt;fn_pre-&gt;fn_next = temp-&gt;fn_next; temp-&gt;fn_next-&gt;fn_pre = temp-&gt;fn_pre; free(temp); printf("%s deleted at between %s and %s\n", str_key, temp-&gt;fn_pre-&gt;first_name, temp-&gt;fn_next-&gt;first_name); return; } counter++; } temp = temp-&gt;fn_next; } if( temp-&gt;fn_next == NULL ) { // Sondan Silme Kosulu if( strcmp(temp-&gt;first_name, str_key) == 0 ) { if( num == counter ) { list-&gt;fn_tail = temp-&gt;fn_pre; temp-&gt;fn_pre-&gt;fn_next = NULL; free(temp); printf("%s deleted at the end and new tail is %s \n", str_key, list-&gt;fn_tail-&gt;first_name ); return; } } } } void del_ln_name(LIST* list, char* str_key) { NODE* temp; int num,counter=1,flag; temp = list-&gt;ln_head; if( list-&gt;ln_head == NULL ) { printf("The list is EMPTY!!!\n"); return; } flag = search_ln(list,str_key); if ( flag ) { printf("Enter the number of student you want to delete : "); scanf("%d", &amp;num); if( strcmp( list-&gt;ln_head-&gt;last_name, str_key ) == 0 ) { // Bastan Silme Kosulu if( num == counter ) { temp-&gt;ln_next-&gt;ln_pre = list-&gt;ln_head; list-&gt;ln_head = temp-&gt;ln_next; free(temp); printf("%s is deleted and the new header is %s\n", str_key, list-&gt;ln_head-&gt;last_name ); return; } counter++; } temp = temp-&gt;ln_next; while ( temp-&gt;ln_next != NULL ) { if( strcmp( temp-&gt;last_name, str_key ) == 0 ) { if( num == counter ) { temp-&gt;ln_pre-&gt;ln_next = temp-&gt;ln_next; temp-&gt;ln_next-&gt;ln_pre = temp-&gt;ln_pre; free(temp); printf("%s deleted at between %s and %s\n", str_key, temp-&gt;ln_pre-&gt;last_name, temp-&gt;ln_next-&gt;last_name); return; } counter++; } temp = temp-&gt;ln_next; } if( temp-&gt;ln_next == NULL ) { // Sondan Silme Kosulu printf("The last item %s second last item %s\n", temp-&gt;first_name, list-&gt;fn_tail-&gt;fn_pre-&gt;first_name);*/ if( strcmp(temp-&gt;last_name, str_key) == 0 ) { if( num == counter ) { list-&gt;ln_tail = temp-&gt;ln_pre; temp-&gt;ln_pre-&gt;ln_next = NULL; free(temp); printf("%s deleted at the end and new tail is %s \n", str_key, list-&gt;ln_tail-&gt;last_name ); return; } } } } </code></pre> <p>The integers <code>flag</code> and <code>counter</code> are for deleting the duplicate students. For example, if there are three duplicates it asks me to which number of student I want to delete. So if I enter number 2 it deletes the second duplicate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload