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

时间: 2024-10-12 10:29:18

dynamicStack的相关文章

链表栈的C语言实现

#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { False = 0, True,}Bool; typedef int ElemType; #define QUEUE_MAX_SIZE 10 #define STACK_INIT_SIZE 10#define STACK_INCREMENT_SIZE 2 #define Null ((void *)0) ty

不定长链表队列C语言实现

#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { False = 0, True,}Bool; typedef int ElemType; #define QUEUE_MAX_SIZE 10 #define STACK_INIT_SIZE 10#define STACK_INCREMENT_SIZE 2 #define Null ((void *)0) ty