⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stack.h

📁 数据结构 输入表达式
💻 H
字号:
#include "stdio.h"
#include "stdlib.h"

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10 
#define Status int

/***********************************************************************************************************************
                                                        字符栈
***********************************************************************************************************************/

typedef struct{
	char *base;
	char *top;
	int stacksize;
}SqStack1;

/*****************************************************构造空栈***************************************/

Status InitStack1(SqStack1 &S){
	S.base=(char *)malloc(STACK_INIT_SIZE*sizeof(char));
	if(!S.base) exit(0);
	S.top=S.base;
	S.stacksize=STACK_INIT_SIZE;
	return 1;
}//InitStack;

/***************************************************e返回栈顶元素*************************************/

char GetTop1(SqStack1 S){
	char e;
	if(S.top==S.base) return 0;
	e=*(S.top-1);
	return e;
}//GetTop

/*******************************************插入e为新的栈顶元素***************************************/

Status Push1(SqStack1 &S,char e){
	if(S.top-S.base>=S.stacksize){
		S.base=(char*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char));
	if(!S.base) exit(0);
	S.top=S.base+S.stacksize;
	S.stacksize+=STACKINCREMENT;
	}
	*S.top++=e;
	return 1;
}//Push

//******************************************栈顶元素出栈,用e返回该元素*******************************/

Status Pop1(SqStack1 &S,char &e){
	if(S.top==S.base) return 0;
	e=*--S.top;
	return 1;
}//Pop


/*********************************************判空****************************************************/

Status StackEmpty1(SqStack1 &S){
	if(S.base==S.top) return 1;
	else return 0;
}

/**************************************从栈底至栈顶输出元素*******************************************/

void PrintElement1(SqStack1 S){
	char *p;
	p=S.base;
	while(p!=S.top){
		printf("%c",*p);
		p++;
	}
	printf("\t\t\t");
}

/***********************************************************************************************************************
                                                 整型数据栈
***********************************************************************************************************************/

typedef struct{
	int *base;
	int *top;
	int stacksize;
}SqStack2;

/*****************************************************构造空栈***************************************/

Status InitStack2(SqStack2 &S){
	S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
	if(!S.base) exit(0);
	S.top=S.base;
	S.stacksize=STACK_INIT_SIZE;
	return 1;
}//InitStack;

/***************************************************e返回栈顶元素*************************************/

int GetTop2(SqStack2 S){
	int e;
	if(S.top==S.base) return 0;
	e=*(S.top-1);
	return e;
}//GetTop

/*******************************************插入e为新的栈顶元素***************************************/

Status Push2(SqStack2 &S,int e){
	if(S.top-S.base>=S.stacksize){
		S.base=(int*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
	if(!S.base) exit(0);
	S.top=S.base+S.stacksize;
	S.stacksize+=STACKINCREMENT;
	}
	*S.top++=e;
	return 1;
}//Push

//******************************************栈顶元素出栈,用e返回该元素*******************************/

Status Pop2(SqStack2 &S,int &e){
	if(S.top==S.base) return 0;
	e=*--S.top;
	return 1;
}//Pop


/*********************************************判空****************************************************/

Status StackEmpty2(SqStack2 &S){
	if(S.base==S.top) return 1;
	else return 0;
}

/**************************************从栈底至栈顶输出元素*******************************************/

void PrintElement2(SqStack2 S){
	int *p;
	p=S.base;
	while(p!=S.top){
		printf("%d ",*p);
		p++;
	}
	printf("\t\t\t");
}

/***********************************************************************************************************************
                                                          end...
************************************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -