diff --git a/main.c b/main.c index f4a886e4ea5d1354a675206183b16cc207f8ac31..e20164eb286b194e984d9c048840bfe20fa8bfc6 100755 --- a/main.c +++ b/main.c @@ -15,6 +15,178 @@ struct student struct list_elem elem; }; +//============================================================================= + +void insert(struct list *student_list) +{ + char temp[40]; + // struct list_elem *it; + printf("\nEnter the name of the student: "); + scanf("%s", temp); + char* entered_name = (char*) malloc((strlen(temp)+1) * sizeof(char)); + strcpy(entered_name, temp); + + struct student *student_node = list_entry(&student_list->tail, struct student, elem); + student_node->name = (char*) malloc((strlen(entered_name)) * sizeof(char)); + + // strcpy(student_node->name, entered_name); + student_node->name = entered_name; + printf("Student node name is: %s\n", student_node->name); + // printf("The list_elem has this value: %s\n", student_node->elem); + printf("The list_elem has this address: %p\n", &student_node->elem); + printf("The list_elem next has this address: %p\n", student_node->elem.next); + printf("The list_elem prev has this address: %p\n", student_node->elem.prev); + + printf("The list tail next has this address: %p\n", student_list->tail.next); + printf("The list tail prev has this address: %p\n", student_list->tail.prev); + + printf("The list head next has this address: %p\n", student_list->head.next); + printf("The list head prev has this address: %p\n", student_list->head.prev); + + list_push_back(student_list, &student_node->elem); + printf("Address of list head: %p\n", &student_list->head); + + // printf("The list tail has this address: %s\n", student_list->tail); + // printf("The list head has this address: %s\n", student_list->head); + + printf("The list tail next has this address: %p\n", student_list->tail.next); + printf("The list tail prev has this address: %p\n", student_list->tail.prev); + + printf("The list head next has this address: %p\n", student_list->head.next); + printf("The list head prev has this address: %p\n", student_list->head.prev); + + // struct list_elem *e; + // bool name_already_exists = false; + + // for (e = list_begin (&student_list); e != list_end (&student_list); + // e = list_next (e)) + // { + // struct student *student_node = list_entry(e, struct student, elem); + // if (entered_name == student_node->name) { + // name_already_exists = true; + // } + // } + + // if (name_already_exists) + // { + // printf("Another student with that name already exists. No new student was added to the list."); + // } + // else + // { + // list_push_back(student_list, student_node); + // } +} + +//============================================================================= + +void delete (struct list *student_list) +{ +} + +//============================================================================= + +void list(struct list *student_list) +{ + if (list_empty (student_list)) + { + printf("THE LIST IS EMPTY\n"); + // return; + } + else + { + struct list_elem *e; + for (e = list_begin(student_list); e != list_end(student_list); + e = list_next(e)) + { + struct student *student_node = list_entry(e, struct student, elem); + printf("%s\n", student_node->name); + } + } +} + +//============================================================================= + +void quit(struct list *student_list) +{ + /* + for (e = list_begin (&list); e != list_end (&list); e = list_remove (e)) + { + ...do something with e... + } + + If you need to free() elements of the list then you need to be + more conservative. Here's an alternate strategy that works + even in that case: + */ + while (!list_empty (student_list)) + { + struct list_elem* e = list_pop_front (student_list); + free(e); + } + exit(1); +} + +//============================================================================= + +int main() +{ + struct list student_list; + list_init(&student_list); + int opt; + + do + { + printf("Menu:\n"); + printf("1 - Insert student\n"); + printf("2 - Delete student\n"); + printf("3 - List students\n"); + printf("4 - Exit\n\n"); + printf("Enter your choice: "); + scanf("%d", &opt); + + switch (opt) + { + case 1: + { + insert(&student_list); + break; + } + case 2: + { + delete (&student_list); + break; + } + case 3: + { + list(&student_list); + break; + } + case 4: + { + quit(&student_list); + break; + } + default: + { + printf("Quit? (1/0):\n"); + scanf("%d", &opt); + if (opt) + quit(&student_list); + break; + } + } + } while (1); + + return 0; +} + + + + + +/* + + void checkIfMallocFailed(void* ptr) { printf("\nAddress of ptr INSIDE FUNC is: %p\n", ptr); @@ -169,10 +341,11 @@ void list(struct list *student_list) // struct student student_node; struct list_elem *it; + struct student *student_node = (struct student*) malloc(sizeof(student_node)); for (it = list_begin(student_list); it != list_end(student_list); it = list_next(it)) { - struct student *student_node = list_entry(it, struct student, elem); + student_node = list_entry(it, struct student, elem); printf("%s\n", student_node->name); } @@ -184,16 +357,14 @@ void list(struct list *student_list) void quit(struct list *student_list) { - /* - for (e = list_begin (&list); e != list_end (&list); e = list_remove (e)) - { - ...do something with e... - } + // for (e = list_begin (&list); e != list_end (&list); e = list_remove (e)) + // { + // ...do something with e... + // } - If you need to free() elements of the list then you need to be - more conservative. Here's an alternate strategy that works - even in that case: - */ + // If you need to free() elements of the list then you need to be + // more conservative. Here's an alternate strategy that works + // even in that case: while (!list_empty (student_list)) { struct list_elem* e = list_pop_front (student_list); @@ -204,54 +375,5 @@ void quit(struct list *student_list) //============================================================================= -int main() -{ - struct list student_list; - list_init(&student_list); - int opt; - - do - { - printf("Menu:\n"); - printf("1 - Insert student\n"); - printf("2 - Delete student\n"); - printf("3 - List students\n"); - printf("4 - Exit\n\n"); - printf("Enter your choice: "); - scanf("%d", &opt); - - switch (opt) - { - case 1: - { - insert(&student_list); - break; - } - case 2: - { - delete (&student_list); - break; - } - case 3: - { - list(&student_list); - break; - } - case 4: - { - quit(&student_list); - break; - } - default: - { - printf("Quit? (1/0):\n"); - scanf("%d", &opt); - if (opt) - quit(&student_list); - break; - } - } - } while (1); - return 0; -} +*/ \ No newline at end of file