麻烦大佬看看
程序代码:
#include <iostream> using namespace std; #include <cstdlib> typedef struct Linknode { int data; //数据域 struct Linknode *next; //指针域 }*LiStack; //初始化栈 void InitLiStack(LiStack *head) { LiStack p = *head; for(int i=0;i<5;i++) { Linknode *s = (Linknode *)malloc(sizeof(Linknode)); s->data = i; //请问为啥把下面的*head换成p就不可以了呢? if(*head) { s->next = *head; *head = s; } else { *head = s; s->next = NULL; } } } //入栈 void Insert(LiStack *head,int data) { Linknode *s; s=(Linknode *)malloc(sizeof(Linknode)); s->data = data; if(*head) { s->next = *head; *head=s; } else { *head = s; s->next = NULL; } } //出栈 void ListPop(LiStack *head) { LiStack p = *head; if(p==NULL) printf("栈已为空栈"); else { cout<<p->data<<endl; *head=p->next; free(p); } } void PrintStack(LiStack head) { Linknode *p; for(p=head;p;p=p->next) cout<<p->data<<endl; } int main(void) { LiStack head; head = NULL; InitLiStack(&head); /* Insert(&head,101); Insert(&head,102); Insert(&head,103);*/ for(int i=0;i<8;i++) { ListPop(&head); } system("pause"); return 0; }