c-如何退出队列中的while循环



如何退出while循环?

我尝试过NULL、"\0"one_answers"\n",但都不起作用。如果用户键入空行或空字符,我将尝试退出循环。我使用了==NULL,但它没有进入循环。

我应该如何退出fgets循环?

代码

typedef struct node{
    char *num;
    struct node *next;
}Node, *NodePtr;

NodePtr makeNode(char *n){
    
    NodePtr np = (NodePtr) malloc(sizeof(Node));
    np -> num = n;   // (*np).num
    np -> next = NULL;
    return np;
}
// prints all the items in the list
void printList(NodePtr np){
    while(np != NULL){ // as long as there's a node
        printf("%sn",np ->num );
        np = np -> next; //go  on to the next node
    }
}// end print list

// main function
int main(void){
    char n[10];
    NodePtr top,np,last;
    top = NULL;
    if(fgets(n,sizeof n,stdin) != NULL){
    
        while(n != ''){
            np = makeNode(n);    // create a new node containing n
            if(top == NULL){     // set top if first node
                top = np;
            }
            else{
                last->next = np; // set last-> next for other nodes
            }
            last = np;           //keepin track of last node
            if(fgets(n,sizeof n,stdin) != NULL){
                //do  nothing
                
                printf("User enter nulln" );
            }
            
        }
    }
    printList(top);
    return 0;
} // end main

看起来n是一个指向字符数组的指针。考虑尝试检查

while(!(n[0] == '') && !(n[0] == 'n'))

检查新行和空字符

最新更新