stack1.h

来自「模拟C++模版的Stack源码」· C头文件 代码 · 共 87 行

H
87
字号
#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 + =
减小字号Ctrl + -
显示快捷键?