HI, in my assignment the requierment for the functions are [PHP read_list function This function receives a pointer to an empty list. It creates a list with input STUDENT records given by the user. The input is terminated when a record with a negative student number is given. This last record itself is not stored in the list. output_list function This function receives a list pointer and displays the records as follows: 1 record per row. The output should have a header (Student No. Student Name GPA) and should be well formatted. [/PHP] I made the function but it is not working properly it takes the input and then only prints the last element in the list. I have been trying to figure out the problem for hours and i can;t seem to figure out, i some could please help me that will be higly appreciated. PHP: #include <stdio.h> #include <string.h> typedef struct { float GPA; char LastName[20]; unsigned int StudentNumber; }STUDENT; typedef struct{ STUDENT aStudent; struct NODE * next; }NODE; void * read_list(NODE * pList){ int userStudentNumber =0; NODE * temp; temp = pList; while(userStudentNumber >=0) { printf("Please Input Students StudentNumber\n"); scanf("%d",&userStudentNumber); if(pList==NULL) { if(!(pList = (NODE*) malloc(sizeof(NODE)))){ exit(100); } temp = pList; } else { while((temp->next)!=NULL) { temp=temp->next; } temp->next = malloc(sizeof(NODE)); temp=temp->next; } temp->aStudent.StudentNumber = userStudentNumber; temp->next = NULL; } temp->next = NULL; } void output_list(NODE * pList){ while(pList!=NULL) { printf(" Data : %d",pList->aStudent.StudentNumber); printf(" Link : %d",pList->next); pList=pList->next; } } int main(void){ NODE * head = NULL; head = read_list(head); output_list(head); return 0; } Thanks, Kim
It's hard to read your code, since the formatting is half-blown, but if it compiles without error you need to turn on warnings/errors or get a new compiler. You say that read-list will return a void * (why? what's wrong with a NODE *?), but if there's a return in there anywhere, I'm too blind to see it.
Sorry for the formatting i formated the code, and i dont;t under what are you saying about NODE * can you please show me i am new to c programming, and this is an assignmnet. Thanks Kim PHP: #include <stdio.h> #include <string.h> typedef struct { float GPA; char LastName[20]; unsigned int StudentNumber; }STUDENT; typedef struct{ STUDENT aStudent; struct NODE * next; }NODE; void * read_list(NODE * pList){ int userStudentNumber =0; NODE * temp; temp = pList; while(userStudentNumber >=0) { printf("Please Input Students StudentNumber\n"); scanf("%d",&userStudentNumber); if(pList==NULL) { if(!(pList = (NODE*) malloc(sizeof(NODE)))){ exit(100); } temp = pList; } else { while((temp->next)!=NULL) { temp=temp->next; } temp->next = malloc(sizeof(NODE)); temp=temp->next; } temp->aStudent.StudentNumber = userStudentNumber; temp->next = NULL; } temp->next = NULL; } void output_list(NODE * pList){ while(pList!=NULL) { printf(" Data : %d",pList->aStudent.StudentNumber); printf(" Link : %d",pList->next); pList=pList->next; } } int main(void){ NODE * head = NULL; head = read_list(head); output_list(head); return 0; }
This is from main: Code: NODE * head = NULL; head = read_list(head); The clear implication is that read_list, if given a NULL pointer, will construct a list and return a pointer to its head. That pointer is of type NODE*, it is not a void *. If read-list is going to return a pointer to the head, then you have to write some statement in read_list that returns that pointer, after it has been constructed. Linked lists seem to be difficult at first. I daresay that if you sit down with a pencil and paper and exercise the code that you have written, you will discover that it is not really all that difficult. Really, you should design these things first, then write the code later. Too many employers will recognize the deficiency if you do it the wrong way round.