c语言数据结构栈

#include <stdio.h>
#include <stdlib.h>

/************************************************************************/
/* 坐标栈
	  实现操作坐标数据类型的栈
	  坐标为二维坐标{x, y}
*/
/************************************************************************/

typedef struct tag_coordinate
{
	int x;
	int y;
}Coordinate;

void printCoordinate(Coordinate *coor)
{
	printf("(%d, %d)\n", coor->x, coor->y);
}

#define STACK_CAPACITY 5

typedef struct tag_stack
{
	Coordinate *pBuffer;    //指向栈中用于存放数据的内存
	int top;          //栈顶
	int length;       //栈中实际元素个数
}Stack;

bool InitStack(Stack **pStack);                       //分配内存初始化栈空间,设定栈容量,栈顶
void DestroyStack(Stack *pStack);                     //回收栈空间内存
bool StackEmpty(Stack *pStack);                       //判定栈是否为空,为空返回true,非空返回false
bool StackFull(Stack *pStack);                        //判定栈是否已满,为满返回true,不满返回false
void ClearStack(Stack *pStack);                       //清空栈
int StackLength(Stack *pStack);                       //已有元素的个数
bool Push(Stack *pStack, Coordinate *elem);                 //元素入栈,栈顶上升
bool Pop(Stack *pStack,Coordinate *elem);                   //元素出栈,栈顶下降
void StackTraverse(Stack *pStack, bool isFromButtom); //遍历栈中所有元素

bool InitStack(Stack **pStack)
{
	*pStack = (Stack *)malloc(sizeof(Stack));//分配(容器)内存.类型为Stack *.一重栈指针
	if(*pStack == NULL)
	{
		return false;
	}

	(*pStack)->pBuffer = (Coordinate *)malloc(sizeof(Coordinate) * STACK_CAPACITY);//栈内的元素的内存(一个一个的元素分配内存)
	if((*pStack)->pBuffer == NULL)
	{
		return false;
	}
	//(*pStack)->top = 0;
	//(*pStack)->length = 0;
	ClearStack(*pStack);
	return true;
}


void DestroyStack(Stack *pStack)
{
	free(pStack->pBuffer);//释放容器释放掉
	pStack->pBuffer = NULL;
	free(pStack);//释放掉容器中一个一个的元素的内存释放掉
	pStack = NULL;
}

void ClearStack(Stack *pStack)
{
	pStack->length = 0;
	pStack->top = 0;
}

bool StackEmpty(Stack *pStack)
{
	if(pStack->length == 0)//判断当前栈是否为空
	{
		return true;//为空代表是的。true为空
	}
	return false;
}

bool StackFull(Stack *pStack)
{
	if(pStack->length == STACK_CAPACITY)//判断栈的长度是不是与#define STACK_CAPACITY 5一样,一样代表满了
	{
		return true;
	}
	return false;
}

int StackLength(Stack *pStack)
{
	return pStack->length;//获取length长度
}

bool Push(Stack *pStack, Coordinate *elem)
{//top的位置就是栈顶.bottom的位置就是栈底
	if(StackFull(pStack))
	{
		return false;//如果满了,就false。
	}
	//pStack->pBuffer[pStack->top] = *elem;
	pStack->pBuffer[pStack->top].x = elem->x;
	pStack->pBuffer[pStack->top].y = elem->y;
	pStack->top++;//入栈了,就top++
	pStack->length++;//长度也要++
	return true;
}

bool Pop(Stack *pStack,Coordinate *elem)
{
	if(StackEmpty(pStack))//如果栈为空,就返回flase,代表没数据出栈
	{
		return false;
	}
	pStack->top--;//出栈后栈顶--
	*elem = pStack->pBuffer[pStack->top];//为什么这样,因为栈顶是在元素的左上角,栈底是在元素的右下角,,因为是出栈pop,所以栈顶得--1,因为栈顶在左上角,出的是没有元素,得栈顶下来。才能出栈.才能移动元素
	pStack->length--;//length--长度--
	return true;
}

void StackTraverse(Stack *pStack, bool isFromButtom)
{
	if(isFromButtom)
	{
		for(int i = 0; i < pStack->length; i++)
		{
			//printf("%c ", pStack->pBuffer[i]);
			//printf("(%d, %d)\n", pStack->pBuffer[i].x, pStack->pBuffer[i].y);
			printCoordinate(&(pStack->pBuffer[i]));
		}
	}
	else
	{
		for (int i = pStack->top - 1; i >= 0; i--)
		{
			//printf("%c ", pStack->pBuffer[i]);
			//printf("(%d, %d)\n", pStack->pBuffer[i].x, pStack->pBuffer[i].y);
			printCoordinate(&(pStack->pBuffer[i]));
		}
	}
}

int main(void)
{
	Stack *myStack = NULL;

	Coordinate ch1 = {2, 3};
	Coordinate ch2 = {4, 5};
	Coordinate ch3 = {6, 7};
	Coordinate ch4 = {8, 9};
	Coordinate ch5 = {1, 0};

	Coordinate ch = {0, 0};

	if(InitStack(&myStack))
	{
		if(StackEmpty(myStack))
		{
			printf("\n当前栈为空\n");
		}
		Push(myStack, &ch1);
		Push(myStack, &ch2);
		Push(myStack, &ch3);
		Push(myStack, &ch4);
		Push(myStack, &ch5);

		StackTraverse(myStack, true);

		if(StackFull(myStack))
		{
			printf("\n当前栈为满\n");
		}

		Pop(myStack, &ch);

		printCoordinate(&ch);

		StackTraverse(myStack, false);


		printf("StackLength = %d\n", StackLength(myStack));
		DestroyStack(myStack);
	}

	system("pause");
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贵哥的编程之路(热爱分享)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值