ListRef getPathTo(GraphRef g, int dest){ int path = dest; ListRef list = newList(); printf("Path to %d: ", dest); while(path!=-1){ insertBeforeFirst(list,path); path = g->parent[path]; } return list; }
// inserts a node in a list before the current node // the new node must have a pointer next to current // and point the previous to the previous of the current of the node void insertBeforeCurrent(ListRef L, long data) { NodeType *node = malloc(sizeof(struct Node)); node ->data = data; if(!offEnd(L)) { if(L->current!=L->first) { node->next = L->current; node->prev = L->current->prev; node->prev->next=node; L->current->prev=node; L->current = node; } else insertBeforeFirst(L, data); } }
/* * insert before the current node, if defined */ void insertBeforeCurrent(ListRef L, long data) { NodeRef n; if (L == NULL) { printf( "Error: Called insertBeforeCurrent() on NULL ListRef\n"); //exit(EXIT_FAILURE); } else if (isEmpty(L)) { printf( "Error: Called insertBeforeCurrent() on empty ListRef\n"); //exit(EXIT_FAILURE); } else if (offEnd(L)) { printf("Error: Called insertBeforeCurrent() with offEnd() == true\n"); //exit(EXIT_FAILURE); } if (atFirst(L)) { insertBeforeFirst(L, data); } else { n = newNode(data); n->next = L->current; n->prev = L->current->prev; n->prev->next = n; n->next->prev = n; L->length++; } }