Ejemplo n.º 1
0
void main(void)
    {
    char *fname = "NAMECARD.DAT";
    char name[NAME_SIZE];
    int i;
    card *t;
    init_card();
    while ((i = select_menu()) != 8)
        {
        switch (i)
            {
            case 1 : input_card();
                     break;
            case 2 : printf("\n    Input name to delete -> ");
                     gets(name);
                     if (!delete_card(name))
                         printf("\n        Can't find that name.");
                     break;
            case 3 : printf("\n    Input name to search -> ");
                     gets(name);
                     t = search_card(name);
                     if (t == NULL)
                         {
                         printf("\n        Can't find that name.");
                         break;
                         }
                     print_header(stdout);
                     print_card(t, stdout);
                     break;
            case 4 : load_cards(fname);
                     break;
            case 5 : save_cards(fname);
                     break;
            case 6 : t = head->next;
                     print_header(stdout);
                     while (t != tail)
                         {
                         print_card(t, stdout);
                         t = t->next;
                         }
                     break;
            case 7 : t = head->next;
                     print_header(stdprn);
                     while (t != tail)
                         {
                         print_card(t, stdprn);
                         t = t->next;
                         }
                     break;
            }
        }
    printf("\n\nProgram ends...");
    }
Ejemplo n.º 2
0
uint32_t remove_node(cardDeck_s *tree, rbNode_s *match) /*#{{{*/
{
    rbNode_s *child = NULL; /* childs of node to be free */
    rbNode_s *pred = NULL;  /* predecessor of node - match */

    if(match == NULL){
        return 0;} /* no match to find */

    if(match -> child[LEFT] != NULL && match -> child[RIGHT] != NULL)
    {
        /* find in order predecessor and replace the data in node, gets nodes
           address */
        pred = replace_predecessor(match);
        match = pred;
    } /* end if */

    assert(match -> child[LEFT] == NULL || match -> child[RIGHT] == NULL);
    child = (match -> child[RIGHT] == NULL) ? match -> child[LEFT] : match -> child[RIGHT];

    if(match -> cflag == BLACK)
    {
        match -> cflag = (child == NULL) ? BLACK : child -> cflag;
        removal_case_1(tree, match);
    } /* end if */
    
    /* replace match (to free) with its child */
    if(match -> parent == NULL){
        tree -> root = child;} /* end if */
    else
    {
        if(match == match -> parent -> child[LEFT]){
            match -> parent -> child[LEFT] = child;}  /* end if */
        else{
            match -> parent -> child[RIGHT] = child;} /* end else */
    } /* end else */

    if(child != NULL){
        child -> parent = match -> parent;}
    
    /* ensure root is black */
    if(match -> parent == NULL && child != NULL){
        child -> cflag = BLACK;}
    
    delete_card(match -> data);
    free(match);

    return 1;
} /* end remove_node #}}} */
Ejemplo n.º 3
0
uint32_t remove_rbTree(rbNode_s **node)/*#{{{*/
{
    if(*node == NULL){
        return 0;}
    
    remove_rbTree(&(*node) -> child[LEFT]);
    remove_rbTree(&(*node) -> child[RIGHT]);

    /* if we are at a leaf, deallocate it */
    if(!(*node) -> child[LEFT] && !(*node) -> child[RIGHT])
    {
        /* free data */
        delete_card((*node) -> data);
        /* free node */
        free(*node);
        /* set to NULL for parent node to become a leaf */
        *node = NULL;
    } /* end if */

    return 1;
} /* end remove_rbTree #}}} */