数据结构的函数借口基本不变,变得是数据存储方式(用静态数组)~~~~ /************************************************************************/ /* author : thomas E-mail: chenhua308@gmail.com */ /************************************************************************/ //use the stactic array relize the stack data-structure #include <stdio.h> //#include <malloc.h> //#include <assert.h> #define MAX_SIZE 50 typedef int T; typedef struct tag { int Top, MaxSize; T elements[MAX_SIZE]; } STACK; void creat(STACK *stack) { stack -> Top = -1; stack -> MaxSize = MAX_SIZE -1; } int is_empty(STACK stack) { if ( stack.Top == -1 ) { //printf("the stack is empty! /n"); return 1; } else { //printf("the stack is not empty! /n"); return 0; } } int is_full( STACK stack ) { if ( stack.Top == stack.MaxSize ) { // printf("the stack is full! /n"); return 1; } else { //printf("the stack is not full! /n"); return 0; } } int top( STACK stack ) { if ( stack.Top == -1 ) { printf("the stack is empty! /n"); return 10000; } else { return stack.elements[stack.Top]; } } void pop( STACK *stack ) { //assert( !is_empty(*stack) ); if( 1 == is_empty( *stack ) ) { printf("the stack is empty! pop is not available /n"); } else { stack -> Top--; } } void push( STACK *stack, T value ) { //assert( !is_full( *stack ) ); if ( 1 == is_full(*stack) ) { printf("the stack is full! /n"); } else { stack -> Top++; stack -> elements[stack -> Top] = value; } } void printstack( STACK stack ) { int i = 0, count; count = stack.Top; if ( stack.Top == -1 ) { printf("the stack is empty! /n"); } else { printf("the number of the stack is: %d /n", stack.Top + 1); for ( i; i <= stack.Top; i++ ) { printf("the %d number is %d /n", i, stack.elements[i]); } } } //test the functions void main(int argc, char *argv[]) { STACK stack; creat(&stack); //is_empty(stack); pop(&stack); push(&stack, 3); printstack(stack); push(&stack, 4); printstack(stack); }