Ejemplo n.º 1
0
node* createListFromNumberReverse(int n)
{
      node* head = (node*) malloc(sizeof(node));
      head = NULL;
      while (n > 0)
      {
            insertNodeAtEnd(&head, n % 10);
            n /= 10;
      } 
      return head;
}
Ejemplo n.º 2
0
static cbline_t* bufferNewLine(CBuffer* cb)
{
    cbnode_t* node;
    cbline_t* line;

    assert(cb);

    // Do we have any used nodes we can reuse?
    if(cb->used)
    {
        node = cb->used;
        cb->used = node->next;
        line = node->data;
    }
    else
    {
        // Allocate another line.
        line = (cbline_t*)malloc(sizeof *line);
        if(!line) Con_Error("CBuffer::NewLine: Failed on allocation of %lu bytes for new cbline.", (unsigned long) sizeof *line);
        node = (cbnode_t*)malloc(sizeof *node);
        if(!node) Con_Error("CBuffer::NewLine: Failed on allocation of %lu bytes for new cbline node", (unsigned long) sizeof *node);
        node->data = line;

        line->text = NULL;
        line->len = 0;
    }
    assert(node->data);

    cb->numLines++;

    line->flags = 0;

    // Link it in.
    insertNodeAtEnd(cb, node);

    // Check if there are too many lines.
    if(cb->numLines > cb->maxLines)
    {
        // Drop the earliest.
        removeNode(cb, cb->head);
        cb->numLines--;
    }

    return line;
}
Ejemplo n.º 3
0
/**
 * Insert data as a new element at the end of the given list.
 *
 * @param llist     Ptr to the list being added to.
 * @param data      Ptr to the element being added.
 * @return          Non-zero iff successfull.
 */
int List_InsertBack(LinkList *llist, const void *data)
{
    if(llist)
    {
        LinkList     *list = (LinkList*) llist;
        listnode_t *n;

        n = newNode(list);
        n->data = (void *) data;
        list->state->numElements++;

        // Link it in.
        insertNodeAtEnd(list, n);
        return 1;
    }

    return 0;
}
Ejemplo n.º 4
0
node* calculateSumReverse(node* a, node* b)
{
      node* sum = (node*) malloc(sizeof(node));
      sum = NULL;
      node* p = a;
      node* q = b;
      int carry = 0;
      while (q != NULL || p != NULL || carry != 0)
      {
            if (q != NULL)
            {
                  carry += q->data;
                  q = q->next;
            }
            if (p != NULL)
            {
                  carry += p->data;
                  p = p->next;
            }
            insertNodeAtEnd(&sum, carry % 10);
            carry /= 10;
      }
      return sum;
}