diff --git a/SeqStack.c b/SeqStack.c index e6846d6dd95bde4f0f2f55b794f1b253358e82a4..7fd5fb8a468317f53fadba8006b5abe4c6a0ea00 100644 --- a/SeqStack.c +++ b/SeqStack.c @@ -1,10 +1,8 @@ /* * @Description: 顺序栈 * @Author: 大熊人 - * @Date: 2020-10-26 17:26:52 - * @LastEditTime: 2020-10-29 00:03:16 + * @LastEditTime: 2020-11-02 23:10:46 */ - #include #include #include "includes/SeqStack.h" @@ -17,7 +15,7 @@ int InitStack(SeqStack *S) { /* - 小白笔记: + 当时的错误,顺便记下笔记: S必须是一个结构体指针才能完成栈的初始化 因为如果只是结构体变量的话 那么它就是一个形参(局部变量) 局部变量的改变是不会影响到函数间同名变量的改变的 因此我们需要传结构体变量S的地址 简单理解就是通过S的地址 修改结构体成员变量 入栈 出栈 都一样 @@ -95,6 +93,16 @@ int Pop(SeqStack *S, STACK_DATA_TYPE *X) return TRUE; } +/** + * @description: 返回元素个数 + * @param {struct}S + * @return {int} + */ +int SeqStackLength(SeqStack S) +{ + return S.top - S.base; +} + /** * @description: 测试顺序栈 */ @@ -109,12 +117,16 @@ void TestSeqStack() printf("初始化失败!\n"); for (i = 1; i <= 5; i++) { - printf("Push:%d %s\n", i, Push(&S, i) ? "入栈成功" : "入栈失败"); + printf("Push:%d %s\n", i, Push(&S, i) ? "TRUE" : "FALSE"); } + printf("\nSeqStackLength=%d\n", SeqStackLength(S)); + GetTop(&S, &value); printf("Top:%d\n", value); while (Pop(&S, &value)) { printf("Pop:%d ", value); } + printf("\nSeqStackLength=%d\n", SeqStackLength(S)); + free(S.base); } \ No newline at end of file diff --git a/includes/SeqStack.h b/includes/SeqStack.h index 15397e6a9580c08802df5fff3706ff678ab4826e..9dbfc6d13f69adf40c3d494189cc3b94ebbbbe67 100644 --- a/includes/SeqStack.h +++ b/includes/SeqStack.h @@ -1,8 +1,7 @@ /* * @Description: 顺序栈 * @Author: 大熊人 - * @Date: 2020-10-25 19:19:02 - * @LastEditTime: 2020-10-26 22:45:32 + * @LastEditTime: 2020-11-02 23:11:32 */ #define SEQSTACK_MAX 100 //栈的最大值 #define ADD_SIZE 10 //每次追加的值 @@ -10,8 +9,8 @@ #define FALSE 0 #define STACK_DATA_TYPE int -/* 顺序栈结构类型 */ -typedef struct SeqSrack +/* 顺序栈结构体类型 */ +typedef struct SeqStack { STACK_DATA_TYPE *base; //栈底指针 STACK_DATA_TYPE *top; //栈顶指针