Ejemplo n.º 1
0
void ll_remove(smb_ll *list, int index, smb_status *status)
{
  // Fond the node
  smb_ll_node *the_node = ll_navigate(list, index, status);
  if (*status == SMB_INDEX_ERROR) {
    return; // Return the INDEX_ERROR
  }
  // Remove it (managing the links and the list header)
  ll_remove_node(list, the_node);
  list->length--;
}
Ejemplo n.º 2
0
void ll_destroy(smb_ll *list)
{
  // Iterate through each node, deleting them as we go
  smb_ll_node *iter = list->head;
  smb_ll_node *temp;
  while (iter) {
    temp = iter->next;
    ll_remove_node(list, iter);
    iter = temp;
  }
}
Ejemplo n.º 3
0
DATA ll_pop_front(smb_ll *list, smb_status *status)
{
  *status = SMB_SUCCESS;
  smb_ll_node *first_node = list->head;
  if (first_node) {
    DATA to_return = first_node->data;
    ll_remove_node(list, first_node);
    list->length--;
    return to_return;
  } else {
    *status = SMB_INDEX_ERROR; // The list is empty
    return PTR(NULL);
  }
}
Ejemplo n.º 4
0
DATA ll_pop_back(smb_ll *list, smb_status *status)
{
  *status = SMB_SUCCESS;
  smb_ll_node *last_node = list->tail;
  if (last_node) {
    DATA to_return = last_node->data;
    ll_remove_node(list, last_node);
    list->length--;
    return to_return;
  } else {
    *status = SMB_INDEX_ERROR;
    return PTR(NULL);
  }
}
Ejemplo n.º 5
0
void test_linked_lists()
{
    linked_list list;
    ll_create_list(&list);    

    for (int i = 0; i < 10; i++)
    {
        printf("Writing data. Size: %d\n", list->size);
        point *p = malloc(sizeof(point));
        p->x = 2 * i;
        p->y = 3 * i;
        printf("Size of list: %d\n", sizeof(list));
        list_node node;
        node.data = p;
        ll_add_node(&list, node, TAIL);
    }

    print_linkedlist(list, "\n", print_ll_node);
    printf("\n\n");
    
    point p1;
    p1.x = 100000;
    p1.y = 150000;

    // point p2;
    // p2.x = 20;
    // p2.y = 40;
    
    list_node node;    
    
    node.data = &p1;
    ll_add_node(&list, node, 5);
    // ll_add_node(&list, node, 20);

    ll_remove_node(&list, 1);

    print_linkedlist(list, "\n", print_ll_node);

    for (int i = 0; i < 10; i++)
    {
        ll_remove_node(&list, HEAD);
    }

    ll_remove_node(&list, HEAD);

    print_linkedlist(list, "\n", print_ll_node);

    // node.data = &p2;
    // ll_add_node(list, node);
    // ll_add_node(list, node);
    // ll_add_node(list, node);
    // ll_add_node(list, node);

    // list_node tmp = ll_get_node(list, 2);
    // list_node tmp = *(list->head);

    // void *data = tmp.data;    
    // point *p_tmp = (point *)data;
    // printf("%d %d\n", p_tmp->x, p_tmp->y);
    printf("Size: %d\n", list->size);

}