-
linkedlist공부/C 2020. 12. 17. 19:52
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct STUDENT_INFO SINFO; void get_studentinfo(); void print_list(); void delete_list(); void insert_node(SINFO *student); struct STUDENT_INFO{ char id[16]; char name[16]; int score; struct STUDENT_INFO *next; }; SINFO *listhead = NULL; int main(){ get_studentinfo(); print_list(); delete_list(); } void get_studentinfo(){ SINFO student; printf("학생 정보를 입력하세요.\n"); printf("입력을 마치려면 학번에 -1을 넣으세요.\n\n"); while(1){ printf("학번 : "); scanf("%s", student.id); if(strcmp(student.id, "-1")!=0){ printf("이름 : "); scanf("%s", student.name); printf("성적 : "); scanf("%d", &student.score); //학생정보를 정렬하여 저장 insert_node(&student); printf("\n"); } else break; } } void print_list(){ SINFO *search; search = listhead; printf("\n%16s%16s%10s\n", "학번", "이름", "성적"); printf("==========================================\n"); while(search !=NULL){ printf("%16s", search->id); printf("%16s", search->name); printf("%6d\n", search->score); search = search->next; } } void delete_list(){ SINFO *temp = listhead; while(listhead != NULL){ listhead = listhead->next; free(temp); temp = listhead; } } void insert_node(SINFO *student){ SINFO *search, *previous; SINFO *temp = (SINFO*)malloc(sizeof(SINFO)); strcpy(temp->id, student->id); strcpy(temp->name, student->name); temp->score = student->score; //temp->next = listhead; //listhead = temp; search = listhead; previous = NULL; while(search != NULL){ if(temp->score < search->score){ previous = search; search = search->next; } else break; } if(previous == NULL){ temp->next = listhead; listhead = temp; } else{ temp->next = search; previous->next = temp; } }
ㅇㅅㅇ.
'공부 > C' 카테고리의 다른 글
[C] 키오스크 - IEC61508 위배 코드 (0) 2023.08.04 과제 studentmgnt (0) 2020.12.20 1219C Project (0) 2020.12.19 [C] 기억력 게임/문자열 전광판 (0) 2020.12.13