옵션 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 5 struct age{ int tag; int age; char name[10]; struct age *next; }; struct age Age[MAX]; struct age *Root = NULL;//루트 void age_init(){//태그 초기ㅗ하 함수 int i; for(i=0; i<MAX; i++) Age[i].tag = 0; } struct age *age_get(int index){//tag가 0ㅇ인걸 1로 바꿔주는함수 if(Age[index].tag ==0) Age[index].tag = 1; return Age; } int get_info(int index){//정보를 받는 함수 char f_buffer[40]; char *token; if(Age[index].tag !=0) return 1; fgets(f_buffer,sizeof(f_buffer),stdin); f_buffer[strlen(f_buffer)-1]='\0'; token=strtok(f_buffer," "); Age[index].age=atoi(token); token=strtok(NULL," "); strcpy(Age[index].name,token); Age[index] = *age_get(index);// 태그를 1로 바꿔주는 함수 if(index !=0) Sort(index);//정렬함수. return 0; } int Sort(int Index){ int i, pt,ppt; ppt=-1; pt=0; for(i=0; i<MAX; i++){ if(strcmp(Age[Index].name,Age[pt].name)<0){ if(ppt==-1)
else
Age[Index].next=&Age[pt]; break; } else if(Age[pt].next == NULL){ Age[pt].next=&Age[Index]; break; } else{ ppt=pt; pt=atoi(Age[pt].next); } } return 0; } void print(){ int i; struct age *pt = Root; for(i=0;i<MAX;i++){ printf("%d, %s\n",pt->age,pt->name); pt=pt->next; } } int main(){ int i; age_init(); for(i=0; i<MAX; i++) get_info(i); printf("\n\n"); print(); } | cs |
출처 | 하........ |