回复 9楼 wp231957
笑死了,看到你这个,我又想起了我写的那个变量名特别恶心的二级链表,改一改应该就可以满足你的新需求了。[此贴子已经被作者于2017-3-13 19:02编辑过]
09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct word { char *words; struct word *Next; }; struct word_tab { char *year; struct word_tab *Next; struct word *Word_Next; }; int index_word_tab( struct word_tab **p_total, char word[], char *year ); void print_word( struct word_tab **p_total ); int gettime( char *time, int size ); int main( void ) { struct word_tab *p; char year[ 50 ]; char date[ 50 ]; char day[ 50 ]; p = NULL; while( gettime( date, 50 ) > 0 ) { if( gettime( day, 50 ) < 0 ) break; if( gettime( year, 50 ) < 0 ) break; strcat( date, " "); strcat( date, day ); index_word_tab( &p, date, year); } print_word( &p ); getchar(); return 0; } int gettime( char *time , int limit ) { int ch; int i; for( i = 0; i < limit - 1 && ( ch = getchar() ) != EOF && !isspace( ch ); i++ ) time[ i ] = ch; time[ i ] = '\0'; return ch; } int index_word_tab( struct word_tab **p_total, char word[], char *year ) { struct word_tab * next; struct word_tab * new_total; struct word * next_word; struct word * new_word; struct word ** kNext; while( ( next = *p_total ) != NULL && strcmp( next->year, year ) < 0 ) p_total = &next->Next; if( next == NULL || strcmp( next->year, year ) != 0 ) { new_total = ( struct word_tab * )malloc( sizeof( struct word_tab ) ); if ( new_total == NULL ) return 0; *p_total = new_total; new_total->Next = next; new_total->year = strdup( year ); new_total->Word_Next = NULL; } kNext = &(*p_total)->Word_Next; while ( ( next_word = *kNext ) != NULL && strcmp( next_word->words, word ) <= 0) { if ( strcmp( next_word->words, word ) == 0 ) { break; } kNext = &next_word->Next; } new_word = ( struct word * )malloc( sizeof( struct word ) ); if ( new_word == NULL ) return 0; *kNext = new_word; new_word->Next = next_word; new_word->words = strdup( word ); return 1; } void print_word( struct word_tab **p_total ) { struct word_tab *next; struct word *word_next; while( ( next = *p_total ) != NULL ) { for( word_next = next->Word_Next; word_next != NULL; word_next = word_next->Next ) fprintf( stderr, "%s %s \n", next->year, word_next->words ); p_total = &next->Next; } }
[此贴子已经被作者于2017-3-13 20:06编辑过]
/*用一个链表将多个链表连起来*/ /* 二级链表 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct Two_Node { char *string; struct Two_Node *Next; }; struct One_Node { char *string; struct One_Node *Next; struct Two_Node *Two_Next; }; int insert_list( struct One_Node **RootP, char *one_str, char *two_str ); void print_list( struct One_Node **RootP ); int insert_list( struct One_Node **RootP, char *one_str, char *two_str ) { struct One_Node *Next_One_Node; struct One_Node *New_One_Node; struct Two_Node *Next_Two_Node; struct Two_Node *New_Two_Node; struct Two_Node **Two_Node_Root; while( ( Next_One_Node = *RootP ) != NULL && strcmp( Next_One_Node->string, one_str ) < 0 ) RootP = &Next_One_Node->Next;//遍历一级链表 if( Next_One_Node == NULL || strcmp( Next_One_Node->string, one_str ) != 0 )//如果不存在,则建立一个新的一级节点 { New_One_Node = ( struct One_Node * )malloc( sizeof( struct One_Node ) ); if ( New_One_Node == NULL ) return 0; *RootP = New_One_Node; New_One_Node->Next = Next_One_Node; New_One_Node->string = strdup( one_str ); New_One_Node->Two_Next = NULL; } Two_Node_Root = &(*RootP)->Two_Next; while ( ( Next_Two_Node = *Two_Node_Root ) != NULL && strcmp( Next_Two_Node->string, two_str ) <= 0)//遍历二级链表 { if ( strcmp( Next_Two_Node->string, two_str ) == 0 ) break; Two_Node_Root = &Next_Two_Node->Next; } New_Two_Node = ( struct Two_Node * )malloc( sizeof( struct Two_Node ) );//新建二级节点 if ( New_Two_Node == NULL ) return 0; *Two_Node_Root = New_Two_Node; New_Two_Node->Next = Next_Two_Node; New_Two_Node->string = strdup( two_str ); return 1; } void print_list( struct One_Node **RootP ) { struct One_Node *Next_One_Node; struct Two_Node *Next_Two_Node; while( ( Next_One_Node = *RootP ) != NULL ) { for( Next_Two_Node = Next_One_Node->Two_Next; Next_Two_Node != NULL; Next_Two_Node = Next_Two_Node->Next ) printf( "%s %s \n", Next_One_Node->string, Next_Two_Node->string ); RootP = &Next_One_Node->Next; } }
[此贴子已经被作者于2017-3-13 21:40编辑过]
#include <stdio.h> #include <stdlib.h> #define MAX 100 typedef struct __tag_date { short y; short m; short d; } Date; int cmp(const void *a, const void *b) { Date *da = (Date *) a, *db = (Date *) b; if (da->y != db->y) return da->y - db->y; if (da->m != db->m) return da->m - db->m; return da->d - db->d; } int main() { size_t n = 0; Date date[MAX]; FILE *fp = fopen("../a.txt", "r"); if (NULL == fp) { perror("Can't find file a.txt"); exit(EXIT_FAILURE); } while (3 == fscanf(fp, "%hi%hi%hi", &date[n].m, &date[n].d, &date[n].y)) { n++; } fclose(fp); qsort(date, n, sizeof(date[0]), cmp); for (size_t i = 0; i < n; ++i) { printf("%d %d %d\n", date[i].y, date[i].m, date[i].d); } return 0; }