链栈逻辑错误,无法输出,哪里需要修改?


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node; 

Node *InitStack(Node *top){
    top=NULL;
    return top;
}
//创建链栈,我感觉是这错了,不知道怎么修改
void Create(Node *top){
    Node *p;
    int i=1;
    ElemType e;
    while(i!=0){
        scanf("%d",&e);
        if(e!=9999){
            p=(Node *)malloc(sizeof(Node));
            p->data=e; 
            p->next=top;
            top=p;
        }else i=0;
    }
}

void push(Node *top,ElemType e){
    Node *s=(Node *)malloc(sizeof(Node));
    s->data=e;
    s->next=top;
    top=s;
    printf("元素%d进栈!\n",s->data); 
}

void pop(Node *top){
    Node *s=top;
    if(s==NULL) printf("该链栈为空,无法出栈!\n");
    else{
        int e=s->data;
        printf("元素%d出栈!\n",e);
        top=s->next;
        free(s);
    }
}

void print(Node *top){
    Node *p=top;
    if(p==NULL) printf("该链栈为空,无法打印!\n");
    else{
        printf("该链表的内容为:");
        while(p!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
}

int main(){
    Node *s;
    s=InitStack(s);
    printf("链栈初始化成功!\n");
    printf("请输入新链栈的值:\n");
    Create(s);
    print(s);
    push(s,15);
    print(s);
    pop(s);
    print(s);    
    return 0;
}

img

Create函数是不对的,不能修改外部的top指针,用返回值,修改如下:

#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node; 
 
Node *InitStack(Node *top){
    top=NULL;
    return top;
}
//创建链栈,我感觉是这错了,不知道怎么修改
Node * Create(Node *top){
    Node *p;
    int i=1;
    ElemType e;
    while(i!=0){
        scanf("%d",&e);
        if(e!=9999){
            p=(Node *)malloc(sizeof(Node));
            p->data=e; 
            p->next=top;
            top=p;
        }else i=0;
    }
    return top;
}
 
Node* push(Node *top,ElemType e){
    Node *s=(Node *)malloc(sizeof(Node));
    s->data=e;
    s->next=top;
    top=s;
    printf("元素%d进栈!\n",s->data); 
    return top;
}
 
Node * pop(Node *top){
    Node *s=top;
    if(s==NULL) printf("该链栈为空,无法出栈!\n");
    else{
        int e=s->data;
        printf("元素%d出栈!\n",e);
        top=s->next;
        free(s);
    }
    return top;
}
 
void print(Node *top){
    Node *p=top;
    if(p==NULL) printf("该链栈为空,无法打印!\n");
    else{
        printf("该链表的内容为:");
        while(p!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
}
 
int main(){
    Node *s = NULL;
    s=InitStack(s);
    printf("链栈初始化成功!\n");
    printf("请输入新链栈的值:\n");
    s = Create(s);
    print(s);
    s=push(s,15);
    print(s);
    s=pop(s);
    print(s);    
    return 0;
}

要注意是对地址的操作,修改处见注释,供参考:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node;

Node *InitStack(Node *top){
    top=NULL;
    return top;
}
//创建链栈,我感觉是这错了,不知道怎么修改
void Create(Node **top){  //修改
    Node *p;
    int i=1;
    ElemType e;
    while(i!=0){
        scanf("%d",&e);
        if(e!=9999){
            p=(Node *)malloc(sizeof(Node));
            p->data=e;
            p->next=(*top); //修改
            (*top)=p;       //修改
        }else i=0;
    }
}

void push(Node**top,ElemType e){ //修改
    Node *s=(Node *)malloc(sizeof(Node));
    s->data=e;
    s->next=(*top); //修改
    (*top)=s;       //修改
    printf("元素%d进栈!\n",s->data);
}

void pop(Node**top){ //修改
    Node *s=(*top);  //修改
    if(s==NULL) printf("该链栈为空,无法出栈!\n");
    else{
        int e=s->data;
        printf("元素%d出栈!\n",e);
        (*top)=s->next; //修改
        free(s);        //修改
    }
}
 
void print(Node *top){
    Node *p=top;
    if(p==NULL) printf("该链栈为空,无法打印!\n");
    else{
        printf("该链表的内容为:");
        while(p!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
}
 
int main(){
    Node *s;
    s=InitStack(s);
    printf("链栈初始化成功!\n");
    printf("请输入新链栈的值:\n");
    Create(&s);  //修改
    print(s);
    push(&s,15); //修改
    print(s);
    pop(&s);     //修改
    print(s);
    return 0;
}