代码
#include <stdio.h>
#include <stdlib.h>/************************************************************************/
/*栈应用示例--数制转换要求:输入任意的正整数N(十进制),分别输出该整数的二进制、八进制、十六进制的结果算法公式:N = (N div d) * d + N mod d (div表示整除,mod表示求余)(216)(十进制) = (330)(八进制) = (D8)(十六进制) = (11011000)(二进制)短除法N N div 8 N mod 8 216 27 027 3 33 0 3N N div 16 N mod 16216 13 813 0 D注意:十六进制的数字组成:0 1 2 3 4 5 6 7 8 9 A B C D E F关于进制间转换:《七日成蝶-C语言编程基础视频课程》
*/
/************************************************************************/#define STACK_CAPACITY 25typedef struct tag_stack
{char *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, char *elem); //元素入栈,栈顶上升
bool Pop(Stack *pStack,char *elem); //元素出栈,栈顶下降
void StackTraverse(Stack *pStack, bool isFromButtom); //遍历栈中所有元素bool InitStack(Stack **pStack)
{*pStack = (Stack *)malloc(sizeof(Stack));if(*pStack == NULL){return false;}(*pStack)->pBuffer = (char *)malloc(sizeof(char) * 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;}return false;
}bool StackFull(Stack *pStack)
{if(pStack->length == STACK_CAPACITY){return true;}return false;
}int StackLength(Stack *pStack)
{return pStack->length;
}bool Push(Stack *pStack, char *elem)
{if(StackFull(pStack)){return false;}pStack->pBuffer[pStack->top] = *elem;pStack->top++;pStack->length++;return true;
}bool Pop(Stack *pStack,char *elem)
{if(StackEmpty(pStack)){return false;}pStack->top--;*elem = pStack->pBuffer[pStack->top];pStack->length--;return true;
}void StackTraverse(Stack *pStack, bool isFromButtom)
{if(isFromButtom){for(int i = 0; i < pStack->length; i++){printf("%c", pStack->pBuffer[i]);}}else{for (int i = pStack->top - 1; i >= 0; i--){printf("%c", pStack->pBuffer[i]);}}
}int main(void)
{Stack *myStack = NULL;int num = 216;int P = 2;char str[] = "0123456789ABCDEF";if(InitStack(&myStack)){while(num != 0){Push(myStack, &(str[num % P]));num = num / P;}StackTraverse(myStack, false);DestroyStack(myStack);}system("pause");return 0;
}
复合类型栈编码
- pStack->pBuffer[pStack->top].x = elem->x;
- pStack->pBuffer[pStack->top].y = elem->y;
- 等效于
- pStack->pBuffer[pStack->top] = *elem;
- 因为 结构体Coordinate 仅仅包含 int类型,不涉及复杂的指针操作,直接赋值即可
#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 5typedef 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));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;}return false;
}bool StackFull(Stack *pStack)
{if(pStack->length == STACK_CAPACITY){return true;}return false;
}int StackLength(Stack *pStack)
{return pStack->length;
}bool Push(Stack *pStack, Coordinate *elem)
{if(StackFull(pStack)){return false;}//pStack->pBuffer[pStack->top] = *elem;pStack->pBuffer[pStack->top].x = elem->x;pStack->pBuffer[pStack->top].y = elem->y;pStack->top++;pStack->length++;return true;
}bool Pop(Stack *pStack,Coordinate *elem)
{if(StackEmpty(pStack)){return false;}pStack->top--;*elem = pStack->pBuffer[pStack->top];pStack->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;
}