Exemplo n.º 1
0
int main(){
    /* test 1 */
    struct ListNode *l1 = createNode(1);
    struct ListNode *p = l1;
    int i;
    for (i = 2; i <= 5; i++) {
        p->next = createNode(i);
        p = p->next;
    }
    p->next = NULL;

    printf("List  1: ");
    p = l1;
    while (p) {
        printf("%d->", p->val);
        p = p->next;
    }
    printf("NIL\n");

    reorderList(l1);

    printf("Reorder: ");
    p = l1;
    while (p) {
        printf("%d->", p->val);
        p = p->next;
    }
    printf("NIL\n");

    /* test 2 */
    struct ListNode *l2 = createNode(1);
    p = l2;
    for (i = 2; i <= 6; i++) {
        p->next = createNode(i);
        p = p->next;
    }
    p->next = NULL;

    printf("List  2: ");
    p = l2;
    while (p) {
        printf("%d->", p->val);
        p = p->next;
    }
    printf("NIL\n");

    reorderList(l2);

    printf("Reorder: ");
    p = l2;
    while (p) {
        printf("%d->", p->val);
        p = p->next;
    }
    printf("NIL\n");

    return 0;
}
Exemplo n.º 2
0
int main(){
	int arr[] ={1,2,3};
	ListNode *head = init_list(arr, sizeof(arr)/sizeof(int));
	reorderList(head);
	print_list(head);
	return 0;
}
Exemplo n.º 3
0
void test(int n) {
    struct ListNode* head = NULL;
    struct ListNode* prev = NULL;
    for (int i = 0; i < n; ++i) {
        struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
        node->val = i;
        node->next = NULL;
        if (prev) {
            prev->next = node;
        } else {
            head = node;
        }
        prev = node;
    }

    printf(" original list is:");
    list_print(head);

    reorderList(head);

    printf("reordered list is:");
    list_print(head);

    printf("\n");
}
Exemplo n.º 4
0
	void test() {
		vector<string> tests = { "12345", "1234" };
		for (string s : tests) {
			ListNode *root = Helper::createList(s);
			Helper::out(root);
			reorderList(root);
			Helper::out(root);
		}
	}
Exemplo n.º 5
0
int main(int argc, char const *argv[])
{
  List L1, L2, L;

	L1 = CreateEvenList(1);
  L2 = CreateOddList(10);
  PrintList(L1);
  PrintList(L2);

  reorderList(L1);
  PrintList(L1);
	return 0;
}
Exemplo n.º 6
0
int main(){
    ListNode *n1 = new ListNode(1);
    ListNode *n2 = new ListNode(2);
    ListNode *n3 = new ListNode(3);
    ListNode *n4 = new ListNode(4);
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = NULL;
    printList(n1);
    reorderList(n1);
    printList(n1);
    return 0;
}
    ShortestPathVector computeShortestPaths(const Graph& graph, GraphNodeIndex root, const DistanceFunction& distance, bool debug = false) {
        typedef decltype(distance(GraphNodeIndex(), GraphNodeIndex())) DistanceType;
        typedef std::pair<GraphNodeIndex, DistanceType> DijkstraElement;
        std::vector<DistanceType> distances(graph.size(), std::numeric_limits<DistanceType>::max()); // Will store the distances to the light node
        ShortestPathVector result(graph.size(), DijkstraNode());
        std::list<DijkstraElement> node_set;
        distances[root] = 0;
        result[root] = DijkstraNode(root, 0);
        node_set.push_front(DijkstraElement(root, 0));

        // Dijkstra algorithm
        while (!node_set.empty()) {
            DijkstraElement elt = node_set.front(); // Next node to handle
            DistanceType tmp = distances[elt.first];

            // No path exists from the light to this node: the computation is over
            if (tmp == std::numeric_limits<DistanceType>::max()) {
                break;
            }

            node_set.pop_front();
            
            // For each successor, update the distances
            for (GraphNodeIndex i = 0; i < graph[elt.first].size(); i++) {
                GraphNodeIndex idx_suc = graph[elt.first][i];
                
                DistanceType cur_dist = distance(elt.first, idx_suc) + distances[elt.first];

                // If the new distance is less than the current distance, update
                if (cur_dist < distances[idx_suc]) {
                    distances[idx_suc] = cur_dist;
                    result[idx_suc] = DijkstraNode(elt.first, distances[idx_suc]);
                    
                    reorderList(idx_suc, cur_dist, node_set);
                }
            }
        }

        return result;
    }