Пример #1
0
void str_push_pop(List *list) {
    SET_TYPE(list, string);
    List_push(list, "hello world");
    List_push(list, "bad boy");
    assert(strcmp(List_index(list, 0)->data, "hello world") == 0);
    assert(strcmp(List_index(list, 1)->data, "bad boy") == 0);
    assert(strcmp(List_first(list)->data, "hello world") == 0);
    assert(strcmp(List_last(list)->data, "bad boy") == 0);
    List_pop(list);
    List_pop(list);
}
Пример #2
0
void push_shift(List *list) {
    List_push(list, 12);
    List_push(list, 7);
    List_push(list, 2);
    assert(List_index(list, 0)->data == 12);
    assert(List_index(list, 1)->data == 7);
    assert(List_first(list)->data == 12);
    assert(List_last(list)->data == 2);
    List_shift(list);
    assert(List_first(list)->data == 7);
    List_shift(list);
    assert(List_first(list)->data == 2);
    List_shift(list);
    assert(List_first(list) == NULL);
}
Пример #3
0
void remove_mid(List *list) {
    List_push(list, 12);
    List_push(list, 7);
    List_push(list, 8);
    List_remove(list, List_index(list, 1));
    assert(List_size(list) == 2);
    assert(List_first(list)->data == 12);
    assert(List_last(list)->data == 8);
}
Пример #4
0
/**
 * Moves an item in the list to another spot.
 *
 * @param list the list to use.
 * @param item the item to move.
 * @param pos the index of the new position.
 */
void List_move(List *list, void *item, int pos) {
    int index = List_index(list, item), len = list->count, i;
    void **items = list->items;

    if (pos != index && pos >= 0) {
        if (pos < index) {
            for (i = index; i > pos; i--) {
                items[i] = items[i - 1];
            }
        } else {
            for (i = index; i < pos; i++) {
                items[i] = items[i + 1];
            }
        }
        items[pos] = item;
    }
}
Пример #5
0
/**
 * Adds an item to a specific list. Dynamically making the array larger.
 *
 * @param list the list to add to.
 * @param item the item to add to the list.
 */
void List_add(List *list, void *item) {
    if (List_index(list, item) != -1) { //if the item exists.
        puts("User already exists");
    } else { //else add the user

        //allocate more memory.
        size_t new_size = (size_t) list->count + 1;
        void **new_items = realloc(list->items, new_size * list->_type_size);

        if (new_items) { //if the memory allocation worked then continue.
            list->items = new_items;
            list->items[list->count] = item;
            list->count += 1;
        } else {
            puts("Error users size");
        }
    }
}