المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : about multi linked list



C++ Programming
05-08-2011, 08:30 AM
hi there i have discussed my problems about multi linked list thread
http://www.codeproject.com/Messages/3876300/implementing-multi-linked-list-correctly_.aspx

but i recognize that it coldn't solved. my problem was about implementing a multi linked list which your list implemented by more than one cases. for example by first name or last name. so it have two header and tail pointers. the adding and building the list operations are succesfully working. and there is not a problem when i delete a node by "first_name" and when i search him again, the output is "not found". but when i search him by his "last_name" (so i using header pointer of last name here) the output becomes "person exist". i hope i explain my problem understandable : ). so i appreciated if you can help me.

here is my structures

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 here is the block i call adding functions for name and surname;

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); }
and of course my add functions;
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 )) 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 )) 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; } } } }