コード例 #1
0
void outputSorted(const Person people[],
        int numPeople,
        int (* compare)(const void *pKey1, const void *pKey2))
{
    Heap heap;
    void *data;
    int i;

    /* Initialize heap */
    heap_init(&heap, compare, NULL);

    /* Add people to heap */
    for (i = 0; i < numPeople; ++i)
        if (heap_insert(&heap, &people[i]) != 0)
            fatalError("Failed to insert person into heap");

    /* Extract and output people from heap */
    while (heap_size(&heap) > 0) {
        if (heap_extract(&heap, &data) != 0)
            fatalError("Failed to extract person from heap");
        outputPerson((const Person *)data);
    }

    /* Destroy heap */
    heap_destroy(&heap);
}
コード例 #2
0
ファイル: outputList.cpp プロジェクト: namtar/cplusplus-beleg
                /**
                 * {@inheritDoc}
                 */
                void outputList(ListPerson* list) {

                    if (list == NULL) {
                        std::cout << endl;
                        std::cout << "List is null" << endl;
                    }

                    ListPerson* currentPerson = list;

                    if (currentPerson != NULL) {
                        outputPersonTableHeader();
                        outputPerson(currentPerson->data);
                        while (currentPerson->next != NULL) {
                            currentPerson = currentPerson->next;
                            outputPerson(currentPerson->data);
                        }
                    }
                }
コード例 #3
0
ファイル: list_api.cpp プロジェクト: namtar/cplusplus-beleg
            /**
             * {@inheritDoc}
             */
            void remove(ListPerson* element) {

                if (element == NULL) {
                    cout << "The given element is null " << endl;
                }

                ListPerson* rootElement = getList();
                // if element is the first one of the list check for follower.
                // if it has a follower delete element and set pointer to follower, otherwise set pointer = NULL
                if (element == rootElement) {
                    ListPerson* nextOne = NULL;
                    if (element->next != NULL) {
                        nextOne = element->next;
                    }
                    Person* personToDelete = element->data;
                    outputPerson(personToDelete);

                    delete[] personToDelete->firstname;
                    delete[] personToDelete->name;
                    delete personToDelete;
                    personToDelete = NULL;
                    element->next = NULL;
                    delete element;
                    // There is no need and no chance to delete sex, deparment and birth because there is no extra memory allocated for.
                    // These elements just exist whithin the person struct and should be freed automatically when deleting the person.

                    if (nextOne != NULL) {
                        rootElement = NULL;
                        personList = nextOne;
                    } else {
                        rootElement = NULL;
                        personList = NULL; // important, if the list is empty set the base pointer to null.
                    }
                } else {
                    // if it is not the first element, search in the list for matches.
                    // Remember the predecessor
                    bool done = false;
                    ListPerson* currentElement = rootElement;
                    while (!done) {
                        ListPerson* predecessor = currentElement;
                        currentElement = currentElement->next;

                        if (element == currentElement) {

                            // delete element and attach follwing element to predecessor.
                            predecessor->next = currentElement->next;
                            Person* personToDelete = currentElement->data;
                            outputPerson(personToDelete);
                            delete[] personToDelete->firstname;
                            delete[] personToDelete->name;
                            delete personToDelete;
                            personToDelete = NULL;
                            currentElement->next = NULL;
                            delete element;
                            delete currentElement;
                            currentElement = NULL; // link current element pointer to null

                            done = true;
                        } else {
                            if (currentElement->next == NULL) {
                                // end when the end of the list has been reached.
                                done = true;
                            }
                        }
                    }
                }
            }