C语言练习stack
#define _CRT_SECURE_NO_DEPRECATE #include<stdio.h> #include<stdlib.h> #define datatype int typedef struct node{ datatype data; struct node* next; }STACK; //initial the stack STACK* Init(STACK* s){ s = NULL; return s; } //push in stack STACK* Push(STACK* s, datatype d){ STACK *p = (STACK*)malloc(sizeof(STACK)); p->data = d; p->next = s; s = p; return s; } //print the stack void Print(STACK* s){ STACK *p = s; while (p){ printf("%4d", p->data); p = p->next; } printf("\n"); } void Free(STACK* s){ STACK *p = s; while (p){ s = p; p = p->next; free(s); } } int main() { int d; STACK *stk = NULL; stk = Init(stk); while (1){ scanf("%d", &d); if (d == 0)break; stk = Push(stk, d); } Print(stk); Free(stk); system("pause"); return 0; }
世上无难事,只要肯登攀。