栈的基本功能实现

防止系统重定义,我们自己定义

1.一个是顺序栈

2.一个是链栈

#ifndef _STACK_H_
#define _STACK_H_
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdbool.h>

//顺序栈
#define Stack_ElemType char
#define STACK_DEFAULT_SIZE 8

typedef struct SeqStack
{
	Stack_ElemType* base;    //栈空间
	size_t          capacity;//栈容量
	int             top;     //栈顶指针
}SeqStack;

//函数申明
void SeqStackInit(SeqStack* pst);
bool SeqStackFull(SeqStack* pst);
bool SeqStackEmpty(SeqStack* pst);

Stack_ElemType SeqStackTop(SeqStack* pst);
void SeqStackPush(SeqStack* pst, Stack_ElemType v);
void SeqStackPop(SeqStack* pst);
void SeqStackShow(SeqStack* pst);
void SeqStackDestroy(SeqStack* pst);

//函数定义

void SeqStackInit(SeqStack* pst)
{
	assert(pst != NULL);
	pst->base = (Stack_ElemType*)malloc(sizeof(Stack_ElemType)* STACK_DEFAULT_SIZE );
	assert(pst->base != NULL);

	pst->capacity = STACK_DEFAULT_SIZE;
	pst->top = 0;
}

bool SeqStackFull(SeqStack* pst)
{
	assert(pst != NULL);
	return pst->top >= pst->capacity;
}

bool SeqStackEmpty(SeqStack* pst)
{
	assert(pst != NULL);
	return pst->top == 0;
}

Stack_ElemType SeqStackTop(SeqStack* pst)
{
	assert(pst != NULL && !SeqStackEmpty(pst));
	return pst->base[pst->top-1];//因为插入时先 赋值再++ ,所以top在栈顶元素之上
}

void SeqStackPush(SeqStack* pst, Stack_ElemType v)
{
	assert(pst != NULL );
	if (SeqStackFull(pst))
	{
		printf("栈已满,%c 不能入栈.\n", v);
		return;
	}
	pst->base[pst->top++] = v;
}

void SeqStackPop(SeqStack* pst)
{
	assert(pst != NULL);
	if (SeqStackEmpty(pst))
	{
		printf("栈已空, 不能出栈.\n");
		return;
	}
	pst->top--;
}

void SeqStackShow(SeqStack* pst)
{
	assert(pst != NULL);
	int i = 0;
	while (i<= pst->top-1)//避免对栈顶指针的操作,否则影响后续操作;
	{
		printf("%c\n", pst->base[pst->top-1-i]);
		++i;
	}
	//assert(pst != NULL);
	//for (int i = pst->top - 1; i >= 0; --i)
		//printf("%c\n", pst->base[i]);
}

void SeqStackDestroy(SeqStack* pst)
{
	assert(pst != NULL);
	free(pst->base);
	pst->base = NULL;
	pst->capacity = pst->top = 0;
}




//链栈
typedef struct StackNode
{
	Stack_ElemType data;
	struct StackNode* next;
}StackNode;

typedef StackNode* LinkStack;

//函数申明
void LinkStackInit(LinkStack* pst);
bool LinkStackEmpty(LinkStack pst);

Stack_ElemType LinkStackTop(LinkStack pst);
void LinkStackPush(LinkStack* pst, Stack_ElemType v);
void LinkStackPop(LinkStack* pst);
void LinkStackShow(LinkStack pst);
void LinkStackDestroy(LinkStack* pst);

void LinkStackInit(LinkStack* pst)
{
	assert(pst != NULL);
	*pst = NULL;
}

bool LinkStackEmpty(LinkStack pst)
{
	return pst == NULL;
}

Stack_ElemType LinkStackTop(LinkStack pst)
{
	assert(pst != NULL);
	return pst->data;
}

void LinkStackPush(LinkStack* pst, Stack_ElemType v)
{
	assert(pst != NULL);
	StackNode* s = (StackNode*)malloc(sizeof(StackNode));
	s->data = v;

	s->next = *pst;
	*pst = s;
}

void LinkStackPop(LinkStack* pst)
{
	assert(pst != NULL);
	StackNode* s = *pst;
	*pst = (*pst)->next;
	free(s);
}

void LinkStackShow(LinkStack pst)
{
	while (pst != NULL)
	{
		printf("%c\n", pst->data);
		pst = pst->next;
	}
}

void LinkStackDestroy(LinkStack* pst)
{
	assert(pst != NULL);
	while (!LinkStackEmpty(*pst))
	{
		StackNode* s = *pst;
		*pst = (*pst)->next;
		free(s);
	}
}

#endif /* _STACK_H_ */

主函数的测试

#include"stack.h"
/*void main()
{
	LinkStack st;
	LinkStackInit(&st);

	LinkStackPush(&st, 'A');
	LinkStackPush(&st, 'B');
	LinkStackPush(&st, 'C');
	LinkStackPush(&st, 'D');
	LinkStackPush(&st, 'E');
	LinkStackShow(st);
	printf("=====\n");

	LinkStackPop(&st);
	LinkStackShow(st);

	printf("=====\n");
	LinkStackPop(&st);
	LinkStackShow(st);

	LinkStackDestroy(&st);
}*/
/*
void main()
{
	SeqStack st;
	SeqStackInit(&st);

	SeqStackPush(&st, 'A');
	SeqStackPush(&st, 'B');
	SeqStackPush(&st, 'C');
	SeqStackPush(&st, 'D');
	SeqStackPush(&st, 'E');
	SeqStackShow(&st);
	printf("top = %c\n",SeqStackTop(&st));
	printf("=========\n");

    SeqStackPop(&st);
	SeqStackShow(&st);
	printf("top = %c\n", SeqStackTop(&st));
	printf("=========\n");

	SeqStackPop(&st);
	SeqStackShow(&st);
	printf("top = %c\n", SeqStackTop(&st));
	SeqStackDestroy(&st);
}*/

/*void main()
{
	SeqStack st;
	SeqStackInit(&st);

	SeqStackPush(&st, 'B');
	SeqStackPush(&st, 'Y');
	SeqStackPush(&st, 'Z');
	SeqStackPush(&st, 'O');
	SeqStackPush(&st, 'L');
	SeqStackPush(&st, 'L');
	SeqStackPush(&st, 'E');
	SeqStackPush(&st, 'H');

	SeqStackPush(&st, 'k');
	SeqStackShow(&st);
	SeqStackDestroy(&st);
}
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值