What's the best approach to check if in a array of lists a single list is empty after I used calloc?

前端 未结 3 788
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 09:12

Usually, when I use linked lists, I write:

struct node *startPtr = NULL;

so I check later if is it NULL, and if it is, it means that the list is

相关标签:
3条回答
  • 2021-01-26 09:40

    You can try to assign nextPtr to the same element or you can introduce a special global item which will mean an empty list.

    0 讨论(0)
  • 2021-01-26 09:50

    If you use malloc with memset instead of calloc you can set your value as your own "void" value.

    I mean this kind of thing :

    int* example;
    
    example=malloc(100*sizeof(int));    // allocate memory to store 100 int
    
    if(example){
        memset(example,1,100*sizeof(int));  // initialize it with value 1
    }
    
    0 讨论(0)
  • 2021-01-26 09:52

    Working with two linked lists simultaneously is kind of fussy and annoying, but it is doable:

    int move_card(Stack **source, Stack **target, int source_pos, int target_pos) {
        // Walk through the linked list, but in every case stop one short of the
        // insertion point
    
        // Walk through the source chain and identify which pointer needs
        // to be manipulated.
        for (int i = 0; i < source_pos; ++i) {
            if (*source == NULL) {
                return -1;
            }
    
            source = &((*source)->nextPtr);
        }
    
        // Walk through the target chain and identify the insertion point.
        for (int i = 0; i < target_pos - 1; ++i) {
            if (*target == NULL) {
                return 1;
            }
    
            target = &((*target)->nextPtr);
        }
    
        // Capture the pointer we're actually moving
        Stack* moving = *source;
    
        // Skip this link in the chain by reassigning source
        *source = moving->nextPtr;
    
        // Capture the record that's being bumped
        Stack* bumped = *target;
    
        // Reassign the target
        *target = moving;
    
        // Re-link the bumped entry back in the chain
        moving->nextPtr = bumped;
    
        return 0;
    }
    

    Where I've taken the liberty of renaming a few things to make this easier to understand. Notice how it uses a double pointer so it can manipulate the original pointers if necessary. When removing the first card from a linked list, the pointer to the "head" entry must change.

    Here's a more complete "demo" harness for that code:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct stack {
        char card[2];
        struct stack *nextPtr;
    };
    typedef struct stack Stack;
    
    Stack* make_stack(char face, char suit, Stack* nextPtr) {
        Stack* stack = calloc(1, sizeof(Stack));
        stack->card[0] = face;
        stack->card[1] = suit;
        stack->nextPtr = nextPtr;
    
        return stack;
    }
    
    void print_stack(Stack* stack) {
        while (stack) {
            printf("%c%c ", stack->card[0], stack->card[1]);
            stack = stack->nextPtr;
        }
    
        printf("\n");
    }
    
    int main(int argc, char** argv) {
        Stack* source = make_stack('A', 'S', make_stack('2', 'S', make_stack('3', 'S', NULL)));
        Stack* target = NULL;
    
        print_stack(source);
    
        move_card(&source, &target, 2, 0);
    
        print_stack(source);
        print_stack(target);
    
        return 0;
    }
    

    Where that uses a simplified Card model.

    0 讨论(0)
提交回复
热议问题