Create student structure. A student has a name (cstring), Student ID (int), grad
ID: 3593262 • Letter: C
Question
Create student structure. A student has a name (cstring), Student ID (int), grade (char), birthday (myDate) and Home Town (string). Create an array of 10 students. Write a function that populates the array with 10 students. The student IDs will be randomly generated numbers between 1000 and 9999 inclusively. No two IDs can be the same. The birthdays will be random dates between Jan 1, 1994 and December 31, 1998. The grades will be random characters between A and F (not including E).. This will be the Original list.
Create 5 arrays of pointers to students, each pointer will point to a student in your array. One pointer array will sort itself so that when the pointed to contents are displayed, it shows all the students in student ID order. The other arrays will sort so that each displays students in age, Name, Grade or HomeTown order.
The displayed list should be nicely formatted with column names like this: All columns should be left-justified.
Student ID Name Birthday Grade Home Town
1002 Tom Thumb January 1, 1992 C Small Ville
2987 Fred Flintstone February 3, 1993 B Bedrock
4765 Sponge Bob June 3, 1994 A Bikini Bottom
Create a menu that shows the following options:
Display original list
Display list sorted by Student ID
Display list sorted by Student Age
Display list sorted by Name
Display list sorted by Grade
Display list sorted by Home Town
Exit
Note:
Take advantage of your myDate class that you just wrote.
Explanation / Answer
#include #include #include struct test_struct { int val; struct test_struct *next; }; struct test_struct *head = NULL; struct test_struct *curr = NULL; struct test_struct* create_list(int val) { printf(" creating list with headnode as [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; head = curr = ptr; return ptr; } struct test_struct* add_to_list(int val, bool add_to_end) { if(NULL == head) { return (create_list(val)); } if(add_to_end) printf(" Adding node to end of list with value [%d] ",val); else printf(" Adding node to beginning of list with value [%d] ",val); struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); if(NULL == ptr) { printf(" Node creation failed "); return NULL; } ptr->val = val; ptr->next = NULL; if(add_to_end) { curr->next = ptr; curr = ptr; } else { ptr->next = head; head = ptr; } return ptr; } struct test_struct* search_in_list(int val, struct test_struct **prev) { struct test_struct *ptr = head; struct test_struct *tmp = NULL; bool found = false; printf(" Searching the list for value [%d] ",val); while(ptr != NULL) { if(ptr->val == val) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } } if(true == found) { if(prev) *prev = tmp; return ptr; } else { return NULL; } } int delete_from_list(int val) { struct test_struct *prev = NULL; struct test_struct *del = NULL; printf(" Deleting value [%d] from list ",val); del = search_in_list(val,&prev); if(del == NULL) { return -1; } else { if(prev != NULL) prev->next = del->next; if(del == curr) { curr = prev; } else if(del == head) { head = del->next; } } free(del); del = NULL; return 0; } void print_list(void) { struct test_struct *ptr = head; printf(" -------Printing list Start------- "); while(ptr != NULL) { printf(" [%d] ",ptr->val); ptr = ptr->next; } printf(" -------Printing list End------- "); return; } int main(void) { int i = 0, ret = 0; struct test_struct *ptr = NULL; print_list(); for(i = 5; i0; i--) add_to_list(i,false); print_list(); for(i = 1; ival); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf(" delete [val = %d] failed, no such element found ",i); } else { printf(" delete [val = %d] passed ",i); } print_list(); } return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.