#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdlib.h> typedef struct stack_node{ int data; struct stack_node * next; } STstacknode; /*声明一个结构体来存储栈顶,有更强的可读性*/ typedef struct{ STstacknode *top; }stack; int push(stack *s, int data_in) { STstacknode *p=(STstacknode *) malloc (sizeof(STstacknode)); if (p == NULL) return 0; // 分配失 p->data=data_in; p->next=s->top; s->top=p; } int pop(stack *s, int *data_in) { if(s->top==NULL) return 1; STstacknode *p=s->top; *data_in=p->data; s->top=p->next; return 0; } int stack_reviw(stack *s) { if(s->top==NULL) { printf("栈空\r\n"); return 1; } STstacknode *p=s->top; while(p!=NULL) { printf("data: %d\r\n",p->data); p=p->next; } return 0; } void initStack(stack *s) { s->top = NULL; } int main() { // datain[10]={1,2,3,4,5}; int temp; stack s; initStack(&s); stack_reviw(&s); push(&s, 1); stack_reviw(&s); push(&s, 3); stack_reviw(&s); pop(&s, &temp); printf("->out data: %d\r\n",temp); stack_reviw(&s); // push(s,int datain[0]); }