c语言数据结构栈
【摘要】
#include <stdio.h>
#include <stdlib.h>
/*************************************************...
#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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
文章来源: blog.csdn.net,作者:贵哥的编程之路(热爱分享),版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_37805832/article/details/124848519
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)