옵션 |
|
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | // Header Files #include<stdio.h> #include<string.h> #include<stdlib.h> // Structure Definition struct Person{ int tag; int age; char name[10]; int next; }; // Global Variables const int numOfPeople = 5; struct Person people[numOfPeople]; int root = 0; // Functions int initPerson() { int i; for (i = 0; i < numOfPeople; i++) { people[i].tag = 0; people[i].next = -1; } return 0; } int insertionSort(int newerIndex) { int index, pointer, ppointer; ppointer = -1; pointer = root; for (index = 0; index < numOfPeople; index++) { if (people[newerIndex].age < people[pointer].age) { if (ppointer == -1) root = newerIndex; else people[ppointer].next = newerIndex; people[newerIndex].next = pointer; break; } else if (people[pointer].next == -1) { people[pointer].next = newerIndex; break; } else { ppointer = pointer; pointer = people[pointer].next; } } people[newerIndex].tag = 1; return 0; } int getPerson(int index) { if (people[index].tag != 0) return 1; char fgetBuffer[40]; char *tokenPointer; fgets(fgetBuffer, sizeof(fgetBuffer), stdin); fgetBuffer[strlen(fgetBuffer) - 1] = '\0'; tokenPointer = strtok(fgetBuffer, " "); people[index].age = atoi(tokenPointer); tokenPointer = strtok(NULL, " "); strcpy(people[index].name, tokenPointer); if (index != 0) // 첫 원소 insertionSort(index); else people[index].tag = 1; return 0; } // Main int main(){ // Init DataArray initPerson(); // Get Data int index; for (index = 0; index < numOfPeople; index++) { getPerson(index); } // Print result int pointer = root; for (index = 0; index<numOfPeople; index++) { printf(" T : %d , Age : %d, Name : %s, next : %d\n", people[pointer].tag, people[pointer].age, people[pointer].name, people[pointer].next); pointer = people[pointer].next; } } | cs |
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 | int insertionSort(int newerIndex) { int index, pointer, ppointer; ppointer = -1; pointer = root; for (index = 0; index < numOfPeople; index++) { if (people[newerIndex].age < people[pointer].age) { if (ppointer == -1) root = newerIndex; else people[ppointer].next = newerIndex; people[newerIndex].next = pointer; break; } else if (people[pointer].next == -1) { people[pointer].next = newerIndex; break; } else { ppointer = pointer; pointer = people[pointer].next; } } people[newerIndex].tag = 1; return 0; | cs |