diff --git a/main.c b/main.c index 05f17acde226cc0daf00d77f541a2b945f08a83a..6fdba572534dd219df2bd74305f3168cdebfb25f 100755 --- a/main.c +++ b/main.c @@ -9,16 +9,67 @@ struct student { }; + +/*This function should fetch a student name from the terminal input and + * add it to the list (remember to allocate memory using malloc as appropriate*/ void insert (struct list *student_list) { + + /*static sized array to hold name*/ + char *array = (char * )malloc(32 * sizeof(char)); + printf("Enter a student name:\n"); + scanf("%s", array); + + /*Allocate memory for new student and add name*/ + struct student *student; + student = (struct student *)malloc(sizeof(struct student)); + student->name = array; + /*insert at end of list*/ + list_push_back(student_list, &student->elem); } + +/*This function should get a student name from the terminal input, remove it from the list, + * and deallocate the appropriate memory(if it exists in the list)*/ void delete (struct list *student_list) { + /*static sized array to hold name*/ + char array[32]; + printf("Enter a student name for deletion:\n"); + scanf("%s", &array); + + struct list_elem *e; + for (e = list_begin(student_list); e != list_end(student_list); e = list_next(e)){ + struct student *s = list_entry(e, struct student, elem); + /*if the two arrays are equal we delete it and free memory*/ + if(strcmp(array, s->name)==0){ + struct list_elem* temp = e; + list_remove(e); + free(s->name); + free(s); + } + } } + +/*This function should print the entire list*/ void list (struct list *student_list) { + /*starting point head for iteration*/ + struct list_elem *e; + for (e = list_begin(student_list); e != list_end(student_list); e = list_next(e)){ + struct student *s = list_entry(e, struct student, elem); + printf("%s <--> ", s->name); + } + printf("\n"); } + +/*This function should clear the list and deallocates the memory for all items in the list*/ void quit (struct list *student_list) { + while (!list_empty (student_list)){ + struct list_elem *element = list_pop_front (student_list); + struct student *s = list_entry(element, struct student, elem); + free(s->name); + free(s); + } } int main() {