简单的栈实现
程序代码:
//静态数组实现: #define StackElementType int //不用typedef的目的是为了提高灵活性,在编译阶段可以通过命令行指定元素类型 //如果是多文件,typedef将绝对优于#define void push( StackElementType value ); //将一个值压入栈 void pop( void ); //将一个值从栈中删除,但不弹出 StackElementType top( void ); //将一个值从栈中弹出,但不删除 int is_empty( void ); //栈空检查 int is_full( void ); //栈满检查
程序代码:
#include "stack.h" #include <assert.h> #define STACK_MAX_SIZE 100 static StackElementType Stack[ STACK_MAX_SIZE ]; static int StackTop = -1; void push( StackElementType value ) { if( is_full() ) { printf("栈已满\n"); return; } Stack[ ++StackTop ] = value; } void pop( void ) { if( is_empty() ) { printf("空栈,无法删除栈顶元素\n"); return; } --StackTop; } StackElementType top( void ) { assert( !is_empty() ); return Stack[ StackTop ]; } int is_empty( void ) { return StackTop == -1; } int is_full( void ) { return StackTop == STACK_MAX_SIZE - 1; }
程序代码:
//动态数组实现: #define StackElementType int void create_stack( void );//创建栈 void destroy_stack( void );//销毁栈 void push( StackElementType value );//将一个值压入栈 void pop( void );//将一个值从栈中删除,但不弹出 StackElementType top( void );//将一个值从栈中弹出,但不删除 int is_empty( void );//栈空检查 int is_full( void );//栈满检查
程序代码:
#include <stdlib.h> #include "MallocStack.h" #include <stdio.h> #include <assert.h> #define STACKSIZE 124 static int StackCurrentSize = STACKSIZE; static int StackTop = -1; static StackElementType *Stack; void create_stack( void ) { if( NULL == Stack ) { Stack = ( StackElementType * )malloc( StackCurrentSize * sizeof( StackElementType) ); if( NULL == Stack ) { printf( "无法创建栈\n" ); return; } } else { printf( "栈已存在,无法创建\n" ); return; } } void destroy_stack( void ) { if( NULL != Stack ) { free( Stack ); Stack = NULL; StackTop = -1; StackCurrentSize = STACKSIZE; } else { printf( "栈不存在,不能销毁\n" ); return; } } int is_empty( void ) { if( NULL == Stack ) return 1; return StackTop == -1; } int is_full( void ) { StackElementType *temp; temp = NULL; if( StackTop == StackCurrentSize - 1 ) { StackCurrentSize += STACKSIZE; temp = ( StackElementType * )realloc( Stack, StackCurrentSize * sizeof(StackElementType ) ); if( NULL == temp ) { StackCurrentSize -= STACKSIZE; return 1; } else { Stack = temp; return 0; } } if( NULL == Stack ) return 1; return 0; } void push( StackElementType value ) { assert( !is_full() ); Stack[ ++StackTop ] = value; } void pop( void ) { assert( !is_empty() ); --StackTop; } StackElementType top( void ) { assert( !is_empty() ); return Stack[ StackTop ]; }
程序代码:
//链表实现 #define StackElementType int void destroy_stack( void ); void push( StackElementType value ); void pop( void ); StackElementType top( void ); int is_empty( void ); int is_full( void );
程序代码:
#include "ListStack.h" #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct Node{ StackElementType value; struct Node *Next; }StackType; static StackType *Stack; int is_empty( void ) { return Stack == NULL; } int is_full( void ) { return 0; } void destroy_stack( void ) { StackType *Temp, *This; This = Stack; Stack = NULL; while( NULL != This ) { Temp = This; This = This->Next; free( Temp ); } } void push( StackElementType value ) { StackType *NewCell; NewCell = ( StackType * )malloc( sizeof( StackType ) ); if( NULL == NewCell ) return; NewCell->value = value; NewCell->Next = Stack; Stack = NewCell; } void pop( void ) { StackType *Temp; if( is_empty() ) return; Temp = Stack; Stack = Stack->Next; free( Temp ); } StackElementType top( void ) { assert( !is_empty() ); return Stack->value; }
[此贴子已经被作者于2017-3-27 08:32编辑过]