From 72d649b09feeb419d4d967238e4bdee0bd216f2a Mon Sep 17 00:00:00 2001 From: Jdhggg <24016020834@stu.nsu.edu.cn> Date: Fri, 21 Mar 2025 18:54:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E6=95=B0=E7=BB=84=E7=9A=84?= =?UTF-8?q?=E6=A0=88=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基于数组的栈实现(函数) 1.入栈 2.出栈 3.初始栈 4.打印栈 --- Project2.vcxproj | 2 + Project2.vcxproj.filters | 6 ++ Project2/Debug/Project2.Build.CppClean.log | 1 + Project2/Debug/Project2.log | 10 +-- .../Debug/Project2.tlog/CL.command.1.tlog | Bin 2958 -> 3702 bytes Project2/Debug/Project2.tlog/CL.read.1.tlog | Bin 12520 -> 15610 bytes Project2/Debug/Project2.tlog/CL.write.1.tlog | Bin 3572 -> 2276 bytes Project2/Debug/Project2.tlog/Cl.items.tlog | 1 + .../Debug/Project2.tlog/link.command.1.tlog | Bin 1690 -> 1866 bytes Project2/Debug/Project2.tlog/link.read.1.tlog | Bin 3750 -> 3976 bytes .../Debug/Project2.tlog/link.secondary.1.tlog | 2 +- .../Debug/Project2.tlog/link.write.1.tlog | Bin 624 -> 736 bytes array_stack.c | 58 ++++++++++++++++++ array_stack.h | 22 +++++++ linked_list_stack.c | 10 +++ main.c | 43 ++++++++----- 16 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 array_stack.c create mode 100644 array_stack.h diff --git a/Project2.vcxproj b/Project2.vcxproj index 6248ffa..79156a2 100644 --- a/Project2.vcxproj +++ b/Project2.vcxproj @@ -19,12 +19,14 @@ + + diff --git a/Project2.vcxproj.filters b/Project2.vcxproj.filters index 4622cd9..c70b33b 100644 --- a/Project2.vcxproj.filters +++ b/Project2.vcxproj.filters @@ -27,6 +27,9 @@ 源文件 + + 源文件 + @@ -38,5 +41,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/Project2/Debug/Project2.Build.CppClean.log b/Project2/Debug/Project2.Build.CppClean.log index 0ace058..2f60bb0 100644 --- a/Project2/Debug/Project2.Build.CppClean.log +++ b/Project2/Debug/Project2.Build.CppClean.log @@ -4,6 +4,7 @@ c:\code\lencode\project2\project2\debug\sq_list.obj c:\code\lencode\project2\project2\debug\main.obj c:\code\lencode\project2\project2\debug\linked_list_stack.obj c:\code\lencode\project2\project2\debug\linked_list.obj +c:\code\lencode\project2\project2\debug\array_stack.obj c:\code\lencode\project2\debug\project2.exe c:\code\lencode\project2\debug\project2.pdb c:\code\lencode\project2\project2\debug\project2.ilk diff --git a/Project2/Debug/Project2.log b/Project2/Debug/Project2.log index f7f75f8..eafe481 100644 --- a/Project2/Debug/Project2.log +++ b/Project2/Debug/Project2.log @@ -1,9 +1,3 @@ - linked_list_stack.c - sq_list.c -C:\code\lencode\Project2\sq_list.c(102,9): warning C4033: “locate_list”必须返回值 -C:\code\lencode\Project2\sq_list.c(112,9): warning C4033: “locate_list”必须返回值 -C:\code\lencode\Project2\sq_list.c(166,34): warning C4047: “函数”:“int”与“sq_list *”的间接级别不同 -C:\code\lencode\Project2\sq_list.c(166,34): warning C4024: “locate_list”: 形参和实参 2 的类型不同 - 正在生成代码... -C:\code\lencode\Project2\sq_list.c(116,1): warning C4715: “locate_list”: 不是所有的控件路径都返回值 + array_stack.c +C:\code\lencode\Project2\array_stack.c(13,5): warning C4013: “memset”未定义;假设外部返回 int Project2.vcxproj -> C:\code\lencode\Project2\Debug\Project2.exe diff --git a/Project2/Debug/Project2.tlog/CL.command.1.tlog b/Project2/Debug/Project2.tlog/CL.command.1.tlog index d6b583e7b37a22a637b185b794584d7af753a653..f35c9529884da54de0dc69df706d216455691b01 100644 GIT binary patch delta 62 zcmeAZ|0c6Rj(Ktqht_5hwrfnA<(SVfZGOeEglY0CCKFCah9DqxWQd%+kVAB1NCgwB F2mpXi5>NmD delta 30 kcmew+(Kj5waG2%AA;#k520JS#@$p8QV diff --git a/Project2/Debug/Project2.tlog/CL.read.1.tlog b/Project2/Debug/Project2.tlog/CL.read.1.tlog index a48189b38b361b350d364ea8f4e56935785e93ea..2d1da449c7629268d08747aa9d035e543eb736f4 100644 GIT binary patch delta 344 zcmZutI|{-;6dVhSETj*J7K)vS_(QCW->5OsBz_5qcd)Ut@dn%3=nWKd9S>k>DOQdF z6+~V!?=!P+-dI)I~uKwO(*b-c?x> zMdd@6CoMLgq6-U6*qBo6qXUz9osXEDEQlV_CdcR8=Au_^k@u~eXrWE!m>Q3ZEut$& z(X>29cH4T$_i4ut?afd@nd&#@kwXc2b{f=;$tL{2W`a-AH*B{o@rlVxK-9`oc6>;Xu!={(6mnMFVqYLjpA z%$stL{7Y!zA+?^6(KNLoXvbd$i^ditP_vOO`gEE3jjoi2ciG~ diff --git a/Project2/Debug/Project2.tlog/link.read.1.tlog b/Project2/Debug/Project2.tlog/link.read.1.tlog index 4b35156704737f08a14106ad9abae8af50207ba1..eeb33b4eada9725eb83808057ec575447a3b073d 100644 GIT binary patch delta 46 scmZ1`+aW(8gwv5B2nZb+A}8KVpZtMSZevj$Cqi&?IalW951iXr07*y=X8-^I delta 18 acmeB>UnV;tWaE)JoD+}8ZT`SDg9QLiG6*FA diff --git a/Project2/Debug/Project2.tlog/link.secondary.1.tlog b/Project2/Debug/Project2.tlog/link.secondary.1.tlog index 578ed71..1eb78f1 100644 --- a/Project2/Debug/Project2.tlog/link.secondary.1.tlog +++ b/Project2/Debug/Project2.tlog/link.secondary.1.tlog @@ -1,2 +1,2 @@ -^C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\MAIN.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\SQ_LIST.OBJ +^C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\ARRAY_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\LINKED_LIST_STACK.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\MAIN.OBJ|C:\CODE\LENCODE\PROJECT2\PROJECT2\DEBUG\SQ_LIST.OBJ C:\code\lencode\Project2\Project2\Debug\Project2.ilk diff --git a/Project2/Debug/Project2.tlog/link.write.1.tlog b/Project2/Debug/Project2.tlog/link.write.1.tlog index 67bcf279596a979d4c62df10130a1f4c7fd49a15..76fb829501c8dedf5136b694d236cf0de0a71425 100644 GIT binary patch delta 23 dcmeys@_=tL{7Y!zA;3G2>@EO2RZ-% delta 10 RcmaFB`hjIa$i^cwOaL0;1W^D0 diff --git a/array_stack.c b/array_stack.c new file mode 100644 index 0000000..72e00ce --- /dev/null +++ b/array_stack.c @@ -0,0 +1,58 @@ +#include +#include +#include "array_stack.h" + +// ʼջ +array_stack* init_array_stack(void) +{ + array_stack* new_stack = (array_stack*)malloc(sizeof(array_stack)); + if (new_stack == NULL) { + printf("ڴʧ[init_array_stack()]\n"); + return NULL; + } + memset(new_stack, 0, sizeof(array_stack)); + new_stack->top = -1; + return new_stack; +} + +// ջ +void push_array_stack(array_stack* stack, elem_type value) +{ + if (stack->top + 1 > MAX_S) + { + printf("ջ[push_array_stack()]\n"); + return; + } + stack->top++; + stack->data[stack->top] = value; +} + +// ջ +int pop_array_stack(array_stack* stack) +{ + if (stack->top == -1) + { + printf("ջΪ[pop_array_stack()]\n"); + return -1; + } + int value = stack->data[stack->top]; + stack->top--; + return value; +} + +// ӡջ +void print_array_stack(array_stack* stack) +{ + if (stack->top == -1) + { + printf("ջΪ[print_array_stack()]\n"); + return; + } + printf("ջԪ: %d\n", stack->data[stack->top]); + printf("-----\n"); + for (int i = stack->top; i >= 0; i--) + { + printf("| %d |\n", stack->data[i]); + } + printf("-----\n"); +} \ No newline at end of file diff --git a/array_stack.h b/array_stack.h new file mode 100644 index 0000000..a7eee27 --- /dev/null +++ b/array_stack.h @@ -0,0 +1,22 @@ +#ifndef ARRAY_SATCK +#define ARRAY_SATCK +#define MAX_S 100 +#define elem_type int +typedef struct array_stack +{ + elem_type data[MAX_S]; + int top; +}array_stack; +// ʼջ +array_stack* init_array_stack(void); + +// ջ +void push_array_stack(array_stack* s, elem_type value); + +// ջ +int pop_array_stack(array_stack* s); + +// ӡջ +void print_array_stack(array_stack* s); + +#endif diff --git a/linked_list_stack.c b/linked_list_stack.c index d1e8e90..283a999 100644 --- a/linked_list_stack.c +++ b/linked_list_stack.c @@ -6,6 +6,11 @@ stack_linked* init_stack_linked(void) { stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked)); + if (s == NULL) + { + printf("ڴʧܣ\n"); + return NULL; + } s->top = NULL; s->size = 0; return s; @@ -15,6 +20,11 @@ stack_linked* init_stack_linked(void) void push_stack_linked(stack_linked* s, elem_type value) { stack_node* node = (stack_node*)malloc(sizeof(stack_node)); + if (node == NULL) + { + printf("ڴʧܣ\n"); + return NULL; + } node->value = value; node->next = s->top; s->top = node; diff --git a/main.c b/main.c index e0e8a3c..32def12 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ #include #include #include +#include "array_stack.h" #include "sq_list.h" int main(void) @@ -8,23 +9,31 @@ int main(void) - sq_list* L = init_sq_list(); - sq_list* N = init_sq_list(); - for (int i = 1; i <= 3; i++) - { - N->data[i - 1] = i+10; - } - for (int i = 1; i <= 10; i++) - { - L->data[i - 1] = i; - } - N->length = 3; - L->length = 10; - print_sq_list(N); - print_sq_list(L); - mer_ge_sq_list(L,N); - print_sq_list(L); - printf("L->length:[%d]\n", L->length); + //sq_list* L = init_sq_list(); + //sq_list* N = init_sq_list(); + //for (int i = 1; i <= 3; i++) + //{ + // N->data[i - 1] = i+10; + //} + //for (int i = 1; i <= 10; i++) + //{ + // L->data[i - 1] = i; + //} + //N->length = 3; + //L->length = 10; + //print_sq_list(N); + //print_sq_list(L); + //mer_ge_sq_list(L,N); + //print_sq_list(L); + //printf("L->length:[%d]\n", L->length); + + + // ջ + array_stack* stack = init_array_stack(); + push_array_stack(stack, 1); + push_array_stack(stack, 2); + push_array_stack(stack, 3); + print_array_stack(stack); printf("Hello World!\n"); system("pause"); return 0;