Esempio n. 1
0
/* --- Function: void create_random_nodes(Dlist list, int nr_of_nodes) --- */
void create_random_nodes(Dlist list, int nr_of_nodes)
{
  int i=0, *pi, retval;

  my_clearscrn();

  /* Initialize the list.. */
  printf("--- CREATED A DOUBLY-LINKED LIST(%d NODES) - RANDOM INTEGER DATA ---", NR_OF_ITEMS);

  do
    {
      pi = (int *)malloc(sizeof(int));
      MALCHK(pi);

      *pi = rand_int(1,50);

      /* Defensive programming... */
      if (!DLISTsize(list))
        retval=DLISTinsprev(list, NULL, pi);
      else
        retval=DLISTinsprev(list, DLISThead(list), pi);

      assert(retval == OK);

    } while (++i < nr_of_nodes);

  /* Display the list... */
  printf("\n\nCurrent list status(%d nodes): ", DLISTsize(list));
  printf("\nAscending : ");
  DLISTtraverse(list, print, DLIST_FWD);
  printf("\nDescending: ");
  DLISTtraverse(list, print, DLIST_BWD);
  prompt_and_pause("\n\n");
}
Esempio n. 2
0
int DLISTremove(Dlist list, DlistNode node, void **data)
{
  /* --- If trying to remove a NULL node or if list is empty --- */
  if (node == NULL || DLISTsize(list) == 0)
    return -1;

  /* --- Remove and return data --- */
  *data = node->data;

  if (node == DLISThead(list)) /* --- If 'node' is head of list.. --- */
    {
      /* --- Handle removal of 'node' accordingly --- */
      list->head = node->next;

      if (list->head == NULL) /* --- 'list->head' now pointing at NULL --- */
        list->tail = NULL;
      else
        node->next->prev = NULL;
    }
  else /* --- Handle removal when 'node' is inside list --- */
    {
      node->prev->next = node->next;
      
      if (node->next == NULL) /* --- If 'node' is tail.. --- */
        list->tail = node->prev;
      else
        node->next->prev = node->prev;
    }
  
  /* --- Free memory occupied by 'node' --- */
  free(node);
  
  /* --- Decrease number of nodes in the list --- */
  list->size--;

  return 0;
}