stack.h文件: #define STACK_TYPE int /*创建size大小的堆栈*/ void creat_stack(int size); void destoy_stack(); int is_empty(); int is_full(); void pop(); void push(STACK_TYPE elem); STACK_TYPE top(); stack.c文件: #include "stack.h" #include <stdlib.h> #include <assert.h> static STACK_TYPE *stack; static int stack_size; static int top_element=-1; void creat_stack(int size) { assert(stack_size==0); stack_size=size; stack=malloc(stack_size*sizeof(STACK_TYPE)); assert(stack!=NULL); } void destoy_stack() { assert(stack_size != 0); stack_size = 0; free(stack); stack=NULL; } int is_empty() { assert(stack_size>0); return top_element==-1; } int is_full() { assert(stack_size > 0); return top_element == stack_size - 1; } void pop() { assert(!is_empty()); top_element -= 1; } STACK_TYPE top() { assert(!is_empty()); return stack[top_element]; } void push(STACK_TYPE value) { assert(!is_full()); top_element += 1; stack[top_element] = value; } 反转数组.c /* **利用堆栈将数组反转 */ #include "stack.h" #include <stdlib.h> #include <stdio.h> #define DEBUG 1 int main() { int i; creat_stack(10); /*创建有10个元素的堆栈*/ int a[5] = {1,2,3,4,5}; /*压栈*/ for(i = 0;i < 5;i++) push(a[i]); /*出栈反转*/ for(i = 0;i < 5;i++) { a[i] = top(); pop(); } #if DEBUG puts("反转后的数组:"); for(i = 0;i < 5;i++) printf("a[%d]:%d/n",i,a[i]); #endif system("pause"); }