#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Seito
{
int num;
char lname[20];
char fname[20];
int point;
} Seito;
typedef struct Cell
{
void* car;
struct Cell* cdr;
} Cell;
typedef struct SortResult
{
int count;
Cell* cell;
} SortResult;
void destroy_cell(Cell* cell)
{
Cell* a;
while (cell != NULL)
{
a = cell;
cell = cell->cdr;
}
}
Cell* loadData_(FILE* f)
{
int num;
char lname[20];
char fname[20];
int point;
Cell* head = NULL;
Cell* tail = NULL;
Cell* cell = NULL;
int success = 1;
Seito* seito = NULL;
while (fscanf(f
, "%d%s%s%d", &num
, lname
, fname
, &point
) != EOF
) {
success = 0;
seito
= malloc(sizeof(Seito
)); if (seito != NULL)
{
seito->num = num;
seito->point = point;
if (cell != NULL)
{
cell->car = seito;
cell->cdr = NULL;
if (tail == NULL)
{
tail = cell;
head = cell;
}
else
{
tail->cdr = cell;
tail = cell;
}
success = 1;
}
}
if (!success)
{
break;
}
}
if (success)
{
return head;
}
else
{
destroy_cell(head);
return NULL;
}
}
Cell* loadData()
{
Cell* cell = NULL;
FILE
* f
= fopen("seito.dat", "r"); if (f != NULL)
{
cell = loadData_(f);
}
return cell;
}
SortResult* sort(Cell* cell, int (*cmp)(Cell*, Cell*))
{
SortResult
* result
= malloc(sizeof(SortResult
)); if (result != NULL)
{
Cell* h = cell;
Cell* c = NULL;
Cell* n = NULL;
int count = 0;
if (h != NULL)
{
cell = h->cdr;
h->cdr = NULL;
while (cell != NULL)
{
n = cell->cdr;
count++;
if (cmp(cell, h) <= 0)
{
cell->cdr = h;
h = cell;
}
else
{
c = h;
while (c->cdr != NULL)
{
count++;
if (cmp(cell, c->cdr) <= 0)
{
cell->cdr = c->cdr;
c->cdr = cell;
break;
}
c = c->cdr;
}
}
cell = n;
}
}
result->count = count;
result->cell = h;
}
return result;
}
int bigToSmall(Cell* x, Cell* y)
{
Seito* x1 = x->car;
Seito* y1 = y->car;
if (x1->point < y1->point)
{
return 1;
}
else if (x1->point > y1->point)
{
return -1;
}
else
{
return 0;
}
}
int main(void)
{
Seito* seito;
Cell* cell;
SortResult* result;
Cell* head = loadData();
if (head != NULL)
{
result = sort(head, bigToSmall);
if (result != NULL)
{
head = result->cell;
cell = head;
printf("%4s %-20s %s\n", "番号", "氏名", "得点"); while (cell != NULL)
{
seito = cell->car;
printf("%d %-10s%-10s%3d\n", seito
->num
, seito
->lname
, seito
->fname
, seito
->point
); cell = cell->cdr;
}
printf("整列の為の比較回数=%d回\n", result
->count
); }
destroy_cell(head);
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdHlwZWRlZiBzdHJ1Y3QgU2VpdG8KewogICAgaW50IG51bTsKICAgIGNoYXIgbG5hbWVbMjBdOwogICAgY2hhciBmbmFtZVsyMF07CiAgICBpbnQgcG9pbnQ7Cn0gU2VpdG87Cgp0eXBlZGVmIHN0cnVjdCBDZWxsCnsKICAgIHZvaWQqIGNhcjsKICAgIHN0cnVjdCBDZWxsKiBjZHI7Cn0gQ2VsbDsKCnR5cGVkZWYgc3RydWN0IFNvcnRSZXN1bHQKewogICAgaW50IGNvdW50OwogICAgQ2VsbCogY2VsbDsKfSBTb3J0UmVzdWx0OwoKdm9pZCBkZXN0cm95X2NlbGwoQ2VsbCogY2VsbCkKewogICAgQ2VsbCogYTsKICAgIHdoaWxlIChjZWxsICE9IE5VTEwpCiAgICB7CiAgICAgICAgYSA9IGNlbGw7CiAgICAgICAgY2VsbCA9IGNlbGwtPmNkcjsKICAgICAgICBmcmVlKGEtPmNhcik7CiAgICAgICAgZnJlZShhKTsKICAgIH0KfQoKQ2VsbCogbG9hZERhdGFfKEZJTEUqIGYpCnsKICAgIGludCBudW07CiAgICBjaGFyIGxuYW1lWzIwXTsKICAgIGNoYXIgZm5hbWVbMjBdOwogICAgaW50IHBvaW50OwogICAgQ2VsbCogaGVhZCA9IE5VTEw7CiAgICBDZWxsKiB0YWlsID0gTlVMTDsKICAgIENlbGwqIGNlbGwgPSBOVUxMOwogICAgaW50IHN1Y2Nlc3MgPSAxOwogICAgU2VpdG8qIHNlaXRvID0gTlVMTDsKICAgIAogICAgd2hpbGUgKGZzY2FuZihmLCAiJWQlcyVzJWQiLCAmbnVtLCBsbmFtZSwgZm5hbWUsICZwb2ludCkgIT0gRU9GKQogICAgewogICAgICAgIHN1Y2Nlc3MgPSAwOwogIAogICAgICAgIHNlaXRvID0gbWFsbG9jKHNpemVvZihTZWl0bykpOwogICAgICAgIGlmIChzZWl0byAhPSBOVUxMKQogICAgICAgIHsKICAgICAgICAgICAgc2VpdG8tPm51bSA9IG51bTsKICAgICAgICAgICAgc3RyY3B5KHNlaXRvLT5sbmFtZSwgbG5hbWUpOwogICAgICAgICAgICBzdHJjcHkoc2VpdG8tPmZuYW1lLCBmbmFtZSk7CiAgICAgICAgICAgIHNlaXRvLT5wb2ludCA9IHBvaW50OwogICAgICAgICAgICAKICAgICAgICAgICAgY2VsbCA9IG1hbGxvYyhzaXplb2YoQ2VsbCkpOwogICAgICAgICAgICBpZiAoY2VsbCAhPSBOVUxMKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjZWxsLT5jYXIgPSBzZWl0bzsKICAgICAgICAgICAgICAgIGNlbGwtPmNkciA9IE5VTEw7CiAgICAgICAgICAgICAgICBpZiAodGFpbCA9PSBOVUxMKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHRhaWwgPSBjZWxsOwogICAgICAgICAgICAgICAgICAgIGhlYWQgPSBjZWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIHRhaWwtPmNkciA9IGNlbGw7CiAgICAgICAgICAgICAgICAgICAgdGFpbCA9IGNlbGw7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzdWNjZXNzID0gMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpZiAoIXN1Y2Nlc3MpCiAgICAgICAgewogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICB9CiAgICBpZiAoc3VjY2VzcykKICAgIHsKICAgICAgICByZXR1cm4gaGVhZDsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBmcmVlKHNlaXRvKTsKICAgICAgICBkZXN0cm95X2NlbGwoaGVhZCk7CiAgICAgICAgcmV0dXJuIE5VTEw7CiAgICB9Cn0KCkNlbGwqIGxvYWREYXRhKCkKewogICAgQ2VsbCogY2VsbCA9IE5VTEw7CiAgICBGSUxFKiBmID0gZm9wZW4oInNlaXRvLmRhdCIsICJyIik7CiAgICBpZiAoZiAhPSBOVUxMKQogICAgewogICAgICAgIGNlbGwgPSBsb2FkRGF0YV8oZik7CiAgICAgICAgZmNsb3NlKGYpOwogICAgfQogICAgcmV0dXJuIGNlbGw7Cn0KClNvcnRSZXN1bHQqIHNvcnQoQ2VsbCogY2VsbCwgaW50ICgqY21wKShDZWxsKiwgQ2VsbCopKQp7CiAgICBTb3J0UmVzdWx0KiByZXN1bHQgPSBtYWxsb2Moc2l6ZW9mKFNvcnRSZXN1bHQpKTsKICAgIGlmIChyZXN1bHQgIT0gTlVMTCkKICAgIHsKICAgICAgICBDZWxsKiBoID0gY2VsbDsKICAgICAgICBDZWxsKiBjID0gTlVMTDsKICAgICAgICBDZWxsKiBuID0gTlVMTDsKICAgICAgICBpbnQgY291bnQgPSAwOwogICAgICAgIAogICAgICAgIGlmIChoICE9IE5VTEwpCiAgICAgICAgewogICAgICAgICAgICBjZWxsID0gaC0+Y2RyOwogICAgICAgICAgICBoLT5jZHIgPSBOVUxMOwogICAgICAgICAgICB3aGlsZSAoY2VsbCAhPSBOVUxMKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBuID0gY2VsbC0+Y2RyOwogICAgICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgICAgIGlmIChjbXAoY2VsbCwgaCkgPD0gMCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBjZWxsLT5jZHIgPSBoOwogICAgICAgICAgICAgICAgICAgIGggPSBjZWxsOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGMgPSBoOwogICAgICAgICAgICAgICAgICAgIHdoaWxlIChjLT5jZHIgIT0gTlVMTCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChjbXAoY2VsbCwgYy0+Y2RyKSA8PSAwKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBjZWxsLT5jZHIgPSBjLT5jZHI7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBjLT5jZHIgPSBjZWxsOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgYyA9IGMtPmNkcjsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBjZWxsID0gbjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXN1bHQtPmNvdW50ID0gY291bnQ7CiAgICAgICAgcmVzdWx0LT5jZWxsID0gaDsKICAgIH0KICAgIHJldHVybiByZXN1bHQ7Cn0KCmludCBiaWdUb1NtYWxsKENlbGwqIHgsIENlbGwqIHkpCnsKICAgIFNlaXRvKiB4MSA9IHgtPmNhcjsKICAgIFNlaXRvKiB5MSA9IHktPmNhcjsKICAgIGlmICh4MS0+cG9pbnQgPCB5MS0+cG9pbnQpCiAgICB7CiAgICAgICAgcmV0dXJuIDE7CiAgICB9CiAgICBlbHNlIGlmICh4MS0+cG9pbnQgPiB5MS0+cG9pbnQpCiAgICB7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIHJldHVybiAwOwogICAgfQp9CgppbnQgbWFpbih2b2lkKQp7CiAgICBTZWl0byogc2VpdG87CiAgICBDZWxsKiBjZWxsOwogICAgU29ydFJlc3VsdCogcmVzdWx0OwogICAgQ2VsbCogaGVhZCA9IGxvYWREYXRhKCk7CiAgICBpZiAoaGVhZCAhPSBOVUxMKQogICAgewogICAgICAgIHJlc3VsdCA9IHNvcnQoaGVhZCwgYmlnVG9TbWFsbCk7CiAgICAgICAgaWYgKHJlc3VsdCAhPSBOVUxMKQogICAgICAgIHsKICAgICAgICAgICAgaGVhZCA9IHJlc3VsdC0+Y2VsbDsKICAgICAgICAgICAgY2VsbCA9IGhlYWQ7CiAgICAgICAgICAgIHByaW50ZigiJTRzICUtMjBzICVzXG4iLCAi55Wq5Y+3IiwgIuawj+WQjSIsICLlvpfngrkiKTsKICAgICAgICAgICAgd2hpbGUgKGNlbGwgIT0gTlVMTCkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc2VpdG8gPSBjZWxsLT5jYXI7CiAgICAgICAgICAgICAgICBwcmludGYoIiVkICUtMTBzJS0xMHMlM2RcbiIsIHNlaXRvLT5udW0sIHNlaXRvLT5sbmFtZSwgc2VpdG8tPmZuYW1lLCBzZWl0by0+cG9pbnQpOwogICAgICAgICAgICAgICAgY2VsbCA9IGNlbGwtPmNkcjsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIuaVtOWIl+OBrueCuuOBruavlOi8g+WbnuaVsD0lZOWbnlxuIiwgcmVzdWx0LT5jb3VudCk7CiAgICAgICAgICAgIGZyZWUocmVzdWx0KTsKICAgICAgICB9CiAgICAgICAgZGVzdHJveV9jZWxsKGhlYWQpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K