Last active
October 31, 2015 13:41
-
-
Save itochan/4c22ea880c46cbe9198c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <strings.h> | |
struct entry { | |
char name[32]; | |
char sex; | |
int age; | |
struct entry *next; | |
}; | |
struct entry *CreateEntry() | |
{ | |
struct entry *ep; | |
ep = (struct entry *)malloc(sizeof(struct entry)); | |
ep->next = NULL; | |
return ep; | |
} | |
struct entry *ReadEntryData(struct entry *Top) | |
{ | |
struct entry *ep; | |
char *cp, *tp; | |
char buf[512]; | |
char name[32]; | |
char sex; | |
int age; | |
while (fgets(buf, sizeof(buf), stdin) != NULL) { | |
/* | |
* Node setup | |
*/ | |
if (Top == NULL) { | |
Top = CreateEntry(); | |
ep = Top; | |
} else { | |
for (ep = Top; ep->next != NULL; ep = ep->next); | |
ep->next = CreateEntry(); | |
ep = ep->next; | |
} | |
cp = buf; | |
tp = cp; | |
if((cp = index(tp, ' ')) == NULL) { | |
fprintf(stderr, "Input format fail: %s\n", tp); | |
exit(-1); | |
} | |
*cp++ = '\0'; | |
strcpy(ep->name, tp); | |
ep->sex = *cp++; | |
cp++; | |
ep->age = atoi(cp); | |
} | |
return (Top); | |
} | |
int count_list(struct entry *entry_head) | |
{ | |
int count; | |
struct entry *ep; | |
ep = entry_head; | |
while (ep != NULL) { | |
count++; | |
ep = ep->next; | |
} | |
return (count); | |
} | |
struct entry *sortlist(struct entry *entry_head) | |
{ | |
struct entry *sorted_head; | |
struct entry *sorted_last; | |
struct entry *ep_prev; | |
struct entry *ep; | |
struct entry *min_entry_prev; | |
struct entry *min_entry; | |
int list_count = count_list(entry_head); | |
int i; | |
for (i = 0; i < list_count; i++) { | |
ep = entry_head; | |
min_entry_prev = entry_head; | |
min_entry = entry_head; | |
while (1) { | |
if (ep->age < min_entry->age) { | |
min_entry_prev = ep_prev; | |
min_entry = ep; | |
} | |
ep_prev = ep; | |
ep = ep->next; | |
if (ep == NULL) { | |
if (ep_prev == min_entry) { | |
min_entry_prev->next = NULL; | |
} | |
break; | |
} | |
} | |
if (sorted_head == NULL) { | |
sorted_head = min_entry; | |
sorted_last = min_entry; | |
} else { | |
sorted_last->next = min_entry; | |
sorted_last = min_entry; | |
} | |
if (min_entry->next != NULL) { | |
if (min_entry_prev == min_entry) { | |
entry_head = min_entry -> next; | |
} else { | |
min_entry_prev->next = min_entry->next; | |
} | |
} | |
} | |
return (sorted_head); | |
} | |
int main() | |
{ | |
struct entry *Top; | |
struct entry *ep; | |
/* | |
* read data | |
*/ | |
Top = (struct entry *)NULL; | |
Top = ReadEntryData(Top); | |
/* | |
* Print out Entry Data | |
*/ | |
printf("Name\tSex\tAge\n"); | |
for (ep = Top; ep != NULL; ep = ep->next) { | |
printf("%s\t%c\t%d\n", ep->name, ep->sex, ep->age); | |
} | |
/* | |
* sorting by age | |
*/ | |
Top = sortlist(Top); | |
printf("Name\tSex\tAge\n"); | |
for (ep = Top; ep != NULL; ep = ep->next) { | |
printf("%s\t%c\t%d\n", ep->name, ep->sex, ep->age); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment