#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;
}
c语言数据结构栈
最新推荐文章于 2024-03-01 17:09:03 发布