该程序有什么不打印任何结果?

use*_*844 -9 c

struct node{
    int data; struct node *next;
};

void push(struct node* head, struct node* n){
    if(n!= NULL){
        if(head==NULL)
            head = n;
        else {
            n->next = head;
            head = n;
        }
    } else printf("Cannot insert a NULL node");
}

struct node* pop(struct node* head){
    if(head!=NULL){
        struct node *n = head;
        head = head->next;
        return n;
    } else {
        printf("The stack is empty");
        return NULL;
    }
}

int main(){
    int i;
    struct node *head = NULL, *n;
    for(i=15;i>0;i--){
        struct node *temp = malloc(sizeof(struct node));
        temp -> data = i;
        temp->next = NULL;
        push(head,temp);
    }
    n = head;
    while(n!=NULL){
        printf("%d ",n->data);
        n=n->next;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

jsa*_*aji 5

您需要将指针头的地址传递给函数push.我的情况是头部没有被修改,因为你只是传递头部的值.

  void push(struct node** head, struct node* n){
if(n!= NULL){
    if(*head==NULL)
        *head = n;
    else {
        n->next = *head;
        *head = n;
    }
} else printf("Cannot insert a NULL node");}


int main(){
int i;
struct node *head = NULL, *n;
for(i=15;i>0;i--){
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp -> data = i;
    temp->next = NULL;
    push(&head,temp);
}
n = head;
while(n!=NULL){
    printf("%d ",n->data);
    n=n->next;
}
return 0;}
Run Code Online (Sandbox Code Playgroud)