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

📄 stack1.h

📁 模拟C++模版的Stack源码
💻 H
字号:
#ifndef _STACK_H_
#define _STACK_H_

struct CommonStack
{
	char *stack;
	int maxcount;
	int count;
	int size;
};

typedef struct CommonStack stack;

void stackInit(stack *s, int maxcount, int size);
void stackPush(stack *s, void *element);
void stackPop(stack *s, void *element);
int stackEmpty(stack *s);
int stackFull(stack *s);


void stackInit(stack *s, int maxcount, int size)
{
	s->count = 0;
	s->maxcount = maxcount;
	s->size = size;
	s->stack = (char*)malloc(maxcount*size);
	if(s==NULL)
		printf("Stack Initialized Failed!\n");
    return;
}

void stackPush(stack *s, void *element)
{
	if(s->count == s->maxcount)
	{
		printf("The Stack is Full!");
		return;
	}
	if(s->count > s->maxcount)
	{
		printf("The Stack is Invalid!");
		return;
	}
	memcpy(s->stack + s->size*s->count, element, s->size);
	s->count++;
	return;
}

void stackPop(stack *s, void *element)
{
	if(s->count == 0)
	{
		printf("The Stack is Empty!");
		return;
	}
	if(s->count > s->maxcount)
	{
		printf("The Stack is Invalid!");
		return;
	}
	s->count--;
	memcpy(element, s->stack + s->size*s->count, s->size);
	return;
}

int stackEmpty(stack *s)
{
	if(s->count > s->maxcount)
	{
		printf("The Stack is Invalid!");
		return -1;
	}
	return (s->count==0);
}

int stackFull(stack *s)
{
	if(s->count > s->maxcount)
	{
		printf("The Stack is Invalid!");
		return -1;
	}
	return (s->count==s->maxcount);
}


#endif

⌨️ 快捷键说明

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