Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List * zeroPredList;
- List * list;
- List retList;
- Int * count;
- int num_Nodes, index, value, pred, succ;
- if (fscanf(idata, "%d", &num_Nodes) == EOF) {
- printf("Number of nodes is inaccessible.\n");
- return NULL;
- }
- zeroPredList = makeList(sizeofInt(), copyInt, free);
- list = (List *)calloc(num_Nodes, sizeof(List));
- index = 0;
- /* Make a list for each node for successor/predecessor upkeep */
- while (index < num_Nodes) {
- list[index] = makeList(sizeofInt(), copyInt, free);
- }
- /* Pred count of succ is incremented and succ goes in pred list */
- while (fscanf(idata, "%d %d", &pred, &succ) != EOF) {
- count[succ]++;
- insertTail(list[pred], &succ);
- }
- index = 0;
- /* Put items with no pred into zero-pred list */
- while (index < num_Nodes) {
- if (count[index] == 0) {
- insertTail(zeroPredList, &index);
- }
- index++;
- }
- /* Remove item k from zero-pred list and output record. Traverse
- k successor list and decrement the count for each record found.
- If record j count is set to zero, then include j on the zero-pred
- list. Continue until zero-pred list is emtpy */
- while (deleteTail(zeroPredList, &value)) {
- /* Push zero-pred items onto the returned list */
- insertTail(retList, &value);
- Int temp;
- while (deleteTail(list[value], temp)) {
- count[valueInt(temp)]--;
- if (count[valueInt(temp)] == 0) {
- insertTail(zeroPredList, &temp);
- }
- }
- }
- return retList;
Add Comment
Please, Sign In to add comment