dynamicStack

动态栈的实现:

定义头文件:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int *stack;
int top=-1; 
int capacity=1;

void push(int ele);
int pop();
void stackEmpty();
void stackFull();

  动态栈函数的操作:

void push(int ele){

	if(top==capacity-1){
		stackFull(); 
	} 
	stack[++top]=ele; 
}
int pop(){
	if(top<0){
		stackEmpty(); 
	} 
	return stack[top--]; 
} 
void stackFull(){
	realloc(stack,2*capacity*sizeof(int*));
	capacity*=2; 
} 
void stackEmpty(){
	fprintf(stderr,"stack is empty!");
	exit(EXIT_FAILURE); 
} 

 主函数测试:

int main(void)
{
	stack=malloc(sizeof(int*));
	push(3); 
	
	printf("%d\n",pop());
	pop(); 
	return 0;
} 

                                                            2016-10-24    00:26:18

posted @ 2016-10-24 00:26  dtdyq  阅读(142)  评论(0编辑  收藏  举报