/* * * TOP sort by algorithm T page 265 * Jinjfeng */ #include "stdio.h" #define NODENUM 10 typedef struct top_relation_ { int pre; int suc; }top_relation_t; typedef struct successor_ { int suc; struct successor_ *next; }successor_t; typedef struct node_ { int count; successor_t *top; } node_t; node_t node_table[NODENUM]; int Qlink0; int F; int R; int N; int sort_N; static inline successor_t * get_avial_successor_t (void) { return (successor_t*) malloc (sizeof(NODENUM)); } static inline void free_successor_t (void *suc) { free(suc); return; } static void construct_tablle (top_relation_t *tr, int tr_n) { int i; int j, k; successor_t *suc; /* sanity check */ if (!tr || tr_n <=0) { printf("construct_tablle() : input para error./n"); return; } /* T1: init the table and N */ for (i = 0; i < NODENUM; ++i) { node_table[i].count = 0; node_table[i].top = NULL; } N = 0; /* T2: get the tr and set it in node_table */ for (i = 0; i < tr_n; ++i) { /*Check the range of the tr*/ if (tr[i].pre < 0 || tr[i].pre >= NODENUM) { printf("construct_tablle(): input para pre range error/n"); return; } if (tr[i].suc < 0 || tr[i].suc >= NODENUM) { printf("construct_tablle(): input para suc range error/n"); return; } j = tr[i].pre; k = tr[i].suc; if (N < j + 1) { N = j + 1; } if (N < k + 1) { N = k + 1; } /* Inc the count[k] */ node_table[k].count ++; /* Link the node to node_table[j].top */ suc = get_avial_successor_t(); if (!suc) { printf("construct_tablle(): Get node error./n"); return; } suc->suc = k; suc->next = node_table[j].top; node_table[j].top = suc; } return; } static void top_sort (void) { int i = 0; successor_t *suc; sort_N = 0; /* Set the F R */ F = -1; // Find the first while (i < N) { if (node_table[i].count == 0 && node_table[i].top) { F = R = i; node_table[i].count = -1; break; } ++i; } // others are put in the queue ++i; while (i < N) { if (node_table[i].count == 0 && node_table[i].top) { node_table[R].count = i; R = i; node_table[R].count = -1; } ++i; } if (F == -1) { printf("top_start(): Can not find the queue head./n"); return; } Qlink0 = F; /* Process the node table */ do { suc = node_table[F].top; while (suc) { node_table[suc->suc].count--; if (node_table[suc->suc].count == 0) { node_table[R].count = suc->suc; R = suc->suc; node_table[R].count = -1; } suc = suc->next; } F = node_table[F].count; sort_N ++; } while (F != -1); /* Find if the sort is successful */ if (N != sort_N) { printf("N:%d, sort_N:%d/n", N, sort_N); printf("Error: The table can not be sorted successfullty./n"); } return; } /* * Print the sort results: * * [x:count]->(suc)->....->NULL */ static void print_sort_table (void) { int i; /* Print the title */ printf("/n========================================/n"); i = Qlink0; while (i != -1){ printf("%d/t", i); i = node_table[i].count; } printf("/n========================================"); return; } /* * Print the table: * * [x:count]->(suc)->....->NULL */ static void print_table (void) { int i; successor_t *suc; /* Print the title */ printf("/n========================================/n"); for (i = 0; i < NODENUM; ++i) { printf("[%02d:%02d]->", i, node_table[i].count); suc = node_table[i].top; while (suc) { printf("(%02d)->", suc->suc); suc = suc->next; } printf("null/n"); } printf("========================================"); return; } int main (int agrc, char **argv) { top_relation_t tr[] = {{0,1}, {4, 7}, {2, 4}, {1, 2}, {6, 3}, {4,3}, {4,5}}; construct_tablle(&tr[0], 7); print_table(); top_sort(); print_sort_ta