顺序栈与链式栈虽然形不似,但神似,大致一样,比较简单,以备后查,特此留证: #include <stdio.h> #include <stdlib.h> #include <string.h> #define NUM 10 #define DataSize 10 typedef struct _SeqStack { int top; //栈当前位置 int array[DataSize]; //放置数据 }SeqStack; /** * 栈置空 * */ void InitStack( SeqStack *s ) { s->top = -1; } /** * 判断栈是否为空 * */ int IsStackEmpty( SeqStack *s ) { return s->top == -1; } /** * 判断栈是否满 * */ int IsStackFull( SeqStack *s ) { return ( s->top > ( DataSize-1 ) ); } /** * 进栈 * */ void Push( SeqStack *s, int data ) { if( IsStackFull( s ) ) { printf( "Stack overflow!/n" ); exit( 0 ); } s->array[++(s->top)] = data; } /** * 出栈 * */ int Pop( SeqStack *s ) { int x; if( IsStackEmpty( s ) ) { printf( "Stack underflow!/n" ); exit( 0 ); } x = s->array[s->top]; --( s->top ); return x; } /** * 取栈顶元素 * */ int GetTopStack( SeqStack *s ) { if( IsStackEmpty( s ) ) { printf( "Stack is empty" ); exit( 0 ); } return s->array[ s->top ]; } int main( void ) { SeqStack *s; int i = 0; s = ( SeqStack * )malloc( sizeof( SeqStack ) ); InitStack( s ); while( i < 10 ) { Push( s, i*2+1 ); i++; } //获取顶元素 i = GetTopStack( s ); printf( "栈顶元素为:%d/n", i ); printf( "出栈元素为:" ); //出栈元素 while( !IsStackEmpty( s ) ) { i = Pop( s ); printf( "%d ", i ); } return 0; }