Exemple #1
0
int
linked_list_alloc(linked_list** ll)
{
    node* h, *t;
    *ll = malloc(sizeof(linked_list));

    if (*ll == NULL)
    {
        perror("malloc failed");
        return -1;
    }

    if (node_alloc(&h) == -1)
        return -1;

    if (node_alloc(&t) == -1)
        return -1;

    /* Set nodes type */
    h->t = head;
    t->t = tail;
    /* Set head and tail node */
    (*ll)->head = h;
    node_set_next(h, t);
    (*ll)->tail = t;
    return 0;
}
Exemple #2
0
int
linked_list_add_first(linked_list* ll, matrix* matr)
{
    node* n;

    if (node_alloc(&n) == -1)
        return -1;

    node_set_elem(ll->head, matr);
    node_set_next(n, ll->head);
    ll->head->t = unreserved;
    ll->head = n;
    n->t = head;
    return 0;
}
Exemple #3
0
int
linked_list_add_last(linked_list* ll, matrix* matr)
{
    node* n;

    if (node_alloc(&n) == -1)
        return -1;

    node_set_elem(ll->tail, matr);
    ll->tail->t = unreserved;
    node_set_next(ll->tail, n);
    n->t = tail;
    ll->tail = n;
    return 0;
}
Exemple #4
0
bool queue_push(Queue *queue, const Article *article) {
    if (queue != 0) {
        Node *node = node_new(article);
        if (node != 0) {
            if (queue_isempty(queue)) {
                queue->top = node;
                queue->bottom = queue->top;
            } else {
                node_set_next(queue->bottom, node);
                queue->bottom = node;
            }
            queue->size++;
            return true;
        }
    }
    return false;
}
Exemple #5
0
int main() {
  int a = 5;
  int b = 10;

  Node* node_a = node_create(&a);
  Node* node_b = node_create(&b);
  
  assert(&a == node_value(node_a));
  assert(&b == node_value(node_b));

  assert(NULL == node_next(node_a));
  assert(NULL == node_next(node_b));

  node_set_next(node_a, node_b);

  assert(node_b == node_next(node_a));
  assert(NULL == node_next(node_b));

  node_destroy(node_a);
  node_destroy(node_b);

  return 0;
}