链栈异常
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
typedef struct Node
{
int data;
struct Node * next;
}Node;
typedef struct Stack
{
Node * top;
Node * base;
}Stack;
void Inti(Stack *s)
{
s->top=(Node *)malloc(sizeof(Node));
s->base=s->top;
s->top->next=NULL;//这里必须是s->top->next=NULL;
printf("Inti Success!\n");
}
void Push(Stack *s,int e)
{
Node *p=(Node*)malloc(sizeof(Node));
p->data=e;p->next=s->top->next;
s->top=p;
printf("Push Success!\n");
}
void print(Stack *s)
{
Node * p = s->top;
if(s->top==s->base)
{
printf("The stack is empty!\n");
}
while (p !=s->base)
{
printf("%d\n", p->data);
p =p->next;
}
printf("\n");
return;
}
int main()
{
Stack s;
//s=(Stack *)malloc(sizeof(Stack));
Inti(&s);
Push(&s,1);
Push(&s,2);
Push(&s,3);
Push(&s,4);
Push(&s,5);
print(&s);
return 0;
}