敲了下线性栈实现的代码,有必要保存起来
#include <stdio.h> #define MAX_STACK_SIZE 100 typedef int ElemType; typedef struct { ElemType stack_array[MAX_STACK_SIZE]; int top; }SqStack; SqStack Init_Stack() { SqStack S; S.top = -1; return S; } bool Push(SqStack &S, ElemType e) { if(S.top == MAX_STACK_SIZE-1) { printf("栈满无法进栈\n"); return 0; } else { S.top++; S.stack_array[S.top] = e; //printf("S.stack_array[%d] = %d\n", S.top, S.stack_array[S.top]); return 1; } } bool Pop(SqStack &S, ElemType &e) { if(S.top == -1) { printf("栈空, 无法出栈\n"); return 0; } else { e = S.stack_array[S.top]; S.top--; return 1; } } void Print(SqStack S) { for(int i = 0; i <= S.top; i++) { if(i % 10 == 0 && i) printf("\n"); printf("%d\t", S.stack_array[i]); } } int main() { SqStack S; int e; S = Init_Stack(); for(int i = 0; i < MAX_STACK_SIZE; i++) Push(S, i); Push(S, 2); Print(S); Pop(S, e); printf("%d出栈\n", e); Pop(S, e); printf("%d出栈\n", e); Print(S); return 0; }
原文:http://www.cnblogs.com/rain-1/p/4923028.html