struct STUDENTINFO char id16 char name16 int score

  • Slides: 67
Download presentation

자료구조 설계 struct STUDENT_INFO { char id[16]; char name[16]; int score; struct STUDENT_INFO *next;

자료구조 설계 struct STUDENT_INFO { char id[16]; char name[16]; int score; struct STUDENT_INFO *next; }; struct STUDENT_INFO *head = NULL; // 학번 // 이름 // 성적 // 다음 포인터 // 연결리스트 헤드 충북대학교 소프트웨어학과 8

프로그램 구조 1. 2. #include <stdio. h> typedef struct STUDENT_INFO SINFO; // 구조체 형식

프로그램 구조 1. 2. #include <stdio. h> typedef struct STUDENT_INFO SINFO; // 구조체 형식 재정의 void get_strudentinfo(); void print_list(); void delete_list(); // 함수 선언 // 구조체 선언 12. struct STUDENT_INFO { char id[16]; char name[16]; int score; struct STUDENT_INFO *next; }; 13. SINFO *listhead = NULL; // 리스트 헤드 선언 및 초기화 14. void main() { get_studentinfo(); print_list(); } 3. 4. 5. 6. 7. 8. 9. 10. 11. 15. 16. 17. 18. 19. 20. 21. void get_strudentinfo() { } void print_list() { } void delete_list() { } // 학생 정보를 저장하고 // 결과를 출력하고 // 비어 있는 함수 구현 충북대학교 소프트웨어학과 10

typedef � � 자료형을 재정의하는 방법 예 1. 2. 3. typedef long time_t; typedef

typedef � � 자료형을 재정의하는 방법 예 1. 2. 3. typedef long time_t; typedef int AGE; typedef struct STUDENTINFO SINFO; 4. AGE age; 5. SINFO *listhead = NULL; 6. //struct STUDENTINFO *listhead = NULL; 충북대학교 소프트웨어학과 11

노드 추가 1. #include <string. h> // 문자열 함수 선언 2. #include <stdlib. h>

노드 추가 1. #include <string. h> // 문자열 함수 선언 2. #include <stdlib. h> // 기억 장소 할당 및 해제 함수 선언 3. // typedef 문 이후에 4. void insert_node(SINFO *student); 5. 6. void get_studentinfo() 7. { 8. … 9. insert_node(&student); 10. … 11. } 12. 13. void insert_node(SINFO *student) 14. { 15. SINFO *temp = (SINFO *)malloc(sizeof(SINFO)); // 동적으로 노드를 생성한다. 16. strcpy(temp->id, student->id); // 학생 데이터를 복사한다. 17. strcpy(temp->name, student->name); 18. temp->score = student->score; 19. 20. temp->next = listhead; 21. listhead = temp; 22. } // 함수 선언 추가 // 학생정보 정렬 저장 // 함수 구현 // 리스트의 앞에 삽입한다. 충북대학교 소프트웨어학과 14

노드 삽입 위치 결정 temp 90 previous NULL head 85 next search temp 78

노드 삽입 위치 결정 temp 90 previous NULL head 85 next search temp 78 next 67 next NULL (a) 첫 번째 노드에 삽입 75 next 85 next 1. previous = search; 2. search = search->next; previous head 78 next 67 next NULL search (b) 중간 노드에 삽입 충북대학교 소프트웨어학과 21

노드 삽입 search head 85 next … head 85 next ① temp->next = search;

노드 삽입 search head 85 next … head 85 next ① temp->next = search; ② head=temp; temp 90 next temp 78 search next 67 90 previous next (b) 리스트의 처음에 추가 후 (a) 리스트의 처음에 추가 전 previous … 78 search next 67 ② previous->next=temp; temp 70 next (c) 리스트의 중간/끝에 추가 전 … temp next … ① temp->next = search; 70 next (d) 리스트의 중간/끝에 추가 후 충북대학교 소프트웨어학과 22

노드 삽입 구현 1. void insert_node(SINFO *student) 1. if (previous == NULL) // 맨

노드 삽입 구현 1. void insert_node(SINFO *student) 1. if (previous == NULL) // 맨 앞인 경우 2. { 3. SINFO *search, *previous; // 노드 포인터 3. temp->next = listhead; 4. listhead = temp; 5. } //temp->next = listhead; 6. else 6. //listhead = temp; 7. { 7. search = listhead; // search 초기화 8. temp->next = search; 8. previous = NULL; // previous 초기화 9. previous->next = temp; 9. while (search != NULL) 10. } 10. { 11. } 4. … 5. // 노드 할당 및 노드 복사 11. if (temp->score < search->score) 12. { 13. previous = search; 14. search = search->next; 15. } 16. else // 삽입 위치를 찾았으면 17. break; // 반복문을 벗어난다. 18. // 중간 혹은 끝인 경우 // 다음 노드로 이동 } 충북대학교 소프트웨어학과 24

영문자 생성 1. 2. 3. 4. 5. char get_alphabet() { int y, x; char

영문자 생성 1. 2. 3. 4. 5. char get_alphabet() { int y, x; char alpha; // 영문자 struct ALPHA_NODE *temp; 6. 7. 8. 9. do { x = rand() % 80; y = rand() % 24; } while (check_node(x, y) == 1); 10. 11. 12. 13. 14. alpha = (rand()%26) + 'A'; // 영문자 생성 temp = (struct ALPHA_NODE *)malloc(sizeof(struct ALPHA_NODE)); temp->x = x; // 노드에 좌표 저장 temp->y = y; temp->alpha = alpha; // 노드에 영문자 저장 15. 16. 17. 18. temp->next = alphalist; alphalist = temp; return alpha; } // 영문자의 좌표 // 영문자를 저장할 노드 // x 좌표 생성 // y 좌표 생성 // {x, y}가 존재하면 반복 // 연결리스트의 맨 앞에 노드 연결 // 생성된 영문자 리턴 충북대학교 소프트웨어학과 34

연결리스트 노드 // studentmgnt. h … struct STUDENT_INFO { char major[32]; char id[16]; char

연결리스트 노드 // studentmgnt. h … struct STUDENT_INFO { char major[32]; char id[16]; char name[16]; int score; struct STUDENT_INFO *next; }; // studentmgnt. c SINFO *listhead = NULL; // 구조체 선언 // 학과 // 학번 // 이름 // 성적 // 다음 학생 데이터에 대한 포인터 // 연결리스트의 헤드 충북대학교 소프트웨어학과 45

insert_node() void insert_node(SINFO *temp, SINFO *previous, SINFO *pos) SINFO *temp: 연결리스트에 저장할 새로운 노드에

insert_node() void insert_node(SINFO *temp, SINFO *previous, SINFO *pos) SINFO *temp: 연결리스트에 저장할 새로운 노드에 대한 포인터 SINFO *previous: 저장할 노드의 앞 노드에 대한 포인터 SINFO *pos: 저장할 노드에 대한 포인터 // studentmgnt. c 1. void insert_node(SINFO *temp, SINFO *previous, SINFO *pos) 2. { 3. if (previous == NULL) 4. { 5. temp->next = pos; 6. listhead = temp; 7. } 8. else 9. { 10. temp->next = pos; 11. previous->next = temp; 12. } 13. } 충북대학교 소프트웨어학과 47

목록보기 // studentmgnt. h void print_list(); // studentmgnt. c void print_list() { SINFO *search

목록보기 // studentmgnt. h void print_list(); // studentmgnt. c void print_list() { SINFO *search = listhead; // 리스트 헤드로 초기화 printf("n%-16 s%-16 s%-6 sn", "학과", "학번", "이름", "성적"); printf("========================n"); while (search != NULL) // 리스트의 모든 노드에 대하여 { printf("%-16 s", search->major); // 데이터 출력 printf("%-16 s", search->id); printf("%-16 s", search->name); printf("%6 dn", search->score); search = search->next; // 다음 노드로 이동 } printf(“n”); } 충북대학교 소프트웨어학과 48

목록정렬 listhead 2013041002 next 2013041001 next 2013041005 next NULL (a) 정렬 전 oldlisthead NULL

목록정렬 listhead 2013041002 next 2013041001 next 2013041005 next NULL (a) 정렬 전 oldlisthead NULL (b) 정렬 초기화 oldlisthead 2013041002 next NULL (c) 한 명 정렬 후 oldlisthead 2013041001 next 2013041002 next NULL (d) 두 명 정렬 후 oldlisthead NULL 2013041001 next 2013041002 next 2013041005 next NULL (e) 마지막 정렬 후 충북대학교 소프트웨어학과 58

sort_add() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

sort_add() 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. void sort_add(SINFO *temp, char cond) { int result; // 비교 결과를 저장하기 위한 지역 변수 SINFO *search, *previous; // 리스트 추적을 위한 포인터 search = listhead; // search를 첫 번쨰 노드를 가리키도록 설정 previous = NULL; // previous를 NULL로 설정 while (search != NULL) // 리스트의 끝까지 반복한다. { if (cond == '1') // 검색 조건에 따라 result = strcmp(temp->major, search->major); // 학과를 비교한다. else result = strcmp(temp->id, search->id); // 학번을 비교한다. if (result > 0) // 추가할 노드의 값이 더 크면 { previous = search; // 다음 노드로 진행한다. search = search->next; } else // 삽입 위치를 찾았다면 break; // 루프를 벗어난다. } insert_node(temp, previous, search); // temp를 search 전에 추가한다. } 충북대학교 소프트웨어학과 62

목록삭제 previous NULL listhead search 2013041002 ② free(search) next 2013041001 next ① listhead =

목록삭제 previous NULL listhead search 2013041002 ② free(search) next 2013041001 next ① listhead = search->next; (a) 첫 번째 노드 삭제 previous 2013041002 search next 2013041001 ② free(search) next 2013041005 next ① previous->next = search->next; (b) 리스트 중간 노드 삭제 충북대학교 소프트웨어학과 64

헤더 파일(studentmgnt. h) #include <stdio. h> // 헤더 파일 포함 #include <malloc. h> #include

헤더 파일(studentmgnt. h) #include <stdio. h> // 헤더 파일 포함 #include <malloc. h> #include <string. h> typedef struct STUDENT_INFO SINFO; // 구조체 재정의 char print_menu(); // 함수 선언 void add_student(); void insert_node(SINFO *temp, SINFO *previous, SINFO *pos); void delete_list(); void print_list(); void write_list(); void read_list(); void search_menu(); void print_node(char *key, char cond); void sort_menu(); void sort_list(char cond); void sort_add(SINFO *temp, char cond); void delete_node(); struct STUDENT_INFO // 구조체 정의 { char major[32]; // 학과 char id[16]; // 학번 char name[16]; // 이름 int score; // 성적 struct STUDENT_INFO *next; // 다음 학생 데이터에 대한 포인터 }; 충북대학교 소프트웨어학과 67