#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX 10
typedef struct list { char s_word[MAX]; char word[MAX]; struct list* next; }LIST;
void init_list(LIST** L) { *L = (LIST*)malloc(sizeof(LIST)); (*L)->next = NULL; }
void add_list(LIST* L, char* s_word, char* word) { LIST *p = (LIST*)malloc(sizeof(LIST)); sprintf(p->s_word,"%s",s_word); sprintf(p->word,"%s",word); LIST* q = L; LIST* r = NULL; while(q!=NULL) { r = q; q = q->next; if(q==NULL || strcmp(s_word,q->s_word)<0) break; } r->next = p; p->next = q; }
void print_list(LIST* L) { LIST* p = L->next; while(p!=NULL) { printf("%s, %s\n",p->s_word, p->word); p = p->next; } }
void swap(char* a, char* b) { char tmp = *a; *a = *b; *b = tmp; }
int partition(char A[], int start, int end) { int i = start-1; int j = start; char x = A[end]; for(;j<end;j++) { if(A[j]<x) { i++; swap(A+i,A+j); } } swap(A+i+1, A+end); return (i+1); }
void quick_sort(char A[], int start, int end) { if(start<end) { int q = partition(A, start, end); quick_sort(A, start, q-1); quick_sort(A, q+1, end); } }
void process_name(LIST* L) { LIST* p = L->next; int flag = 0; char* str = p->s_word; while(p!=NULL) { if(flag==0) printf("\n%s:",p->s_word); if(strcmp(str, p->s_word)==0) { flag=1; printf("%s ",p->word); p = p->next; } else { flag =0; str = p->s_word; } } printf("\n"); }
int main(int argc, char *argv[]) { FILE* fp = fopen("word.txt","r"); LIST* L = NULL; char s_str[MAX]; char str[MAX]; init_list(&L); while(fgets(s_str, MAX, fp)!=NULL) { // printf("we gets:%s", s_str);
s_str[strlen(s_str)-1] = '\0'; sprintf(str,"%s",s_str); // printf("before sorts:%s\n", s_str);
quick_sort(s_str, 0, strlen(s_str)-1); //printf("after sorts:%s\n", s_str);
add_list(L, s_str, str); } printf("list is:\n"); print_list(L);
process_name(L); system("PAUSE"); return 0; }
|