微调与更新栈
This commit is contained in:
		
							
								
								
									
										68
									
								
								数据结构/栈/Stack.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								数据结构/栈/Stack.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| /* | ||||
| *@文件:Stack.c | ||||
| *@作者:‘你遇了我’ | ||||
| *@time:2023/10/18 | ||||
| *@联系:QQ:321640253 | ||||
| *@版本:V1.0 | ||||
| *@描述:顺序泛型栈 | ||||
| */ | ||||
| #include "Stack.h" | ||||
|  | ||||
| /* | ||||
|  * @简介:初始化一个栈 | ||||
|  * @参数: | ||||
|  *      *S:栈结构体指针 | ||||
|  *      Maxsize:栈容器的长度 | ||||
|  *      NodeSize:栈容器每个元素的空间大小 | ||||
|  *      *container:容器指针 | ||||
|  * @返回值:无 | ||||
|  * */ | ||||
| void InitStack(SqStack *S,uint16_t MaxSize,uint16_t NodeSize) { | ||||
|    S->container = calloc(MaxSize,NodeSize); | ||||
|    S->Top=-1; | ||||
|    S->MaxSize=MaxSize; | ||||
|    S->NodeSize=NodeSize; | ||||
|    S->size = 0; | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * @简介:进栈 | ||||
|  * @参数: | ||||
|  *      *S:栈结构体指针 | ||||
|  * @返回值:可进栈空间地址 | ||||
|  * */ | ||||
| void *PushStack(SqStack *S) { | ||||
|     if(S->size>=S->MaxSize)return NULL; | ||||
|     S->Top++; | ||||
|     S->size++; | ||||
|     return S->container + S->Top*S->NodeSize; | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * @简介:出栈 | ||||
|  * @参数: | ||||
|  *      *S:栈结构体指针 | ||||
|  * @返回值:出栈元素地址空间 | ||||
|  * */ | ||||
| void *PopStack(SqStack *S) { | ||||
|    if (S->size == 0) { | ||||
|        return NULL; | ||||
|    } | ||||
|    S->Top--; | ||||
|    S->size--; | ||||
|    return S->container+S->Top*S->NodeSize; | ||||
| } | ||||
|  | ||||
| /* | ||||
|  * @简介:销毁栈 | ||||
|  * @参数: | ||||
|  *      *S:栈结构体指针 | ||||
|  * @返回值:无 | ||||
|  * */ | ||||
| void DestroyStack(SqStack *S) { | ||||
|    free(S->container); | ||||
|    S->container = NULL; | ||||
|    S->Top = -1; | ||||
|    S->MaxSize = 0; | ||||
|    S->size = 0; | ||||
| } | ||||
							
								
								
									
										31
									
								
								数据结构/栈/Stack.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								数据结构/栈/Stack.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| /* | ||||
| *@文件:Stack.h | ||||
| *@作者:‘你遇了我’ | ||||
| *@time:2023/10/18 | ||||
| *@联系:QQ:321640253 | ||||
| *@版本:V1.0 | ||||
| *@描述:顺序泛型栈 | ||||
| */ | ||||
| #ifndef _STACK_H_ | ||||
| #define _STACK_H_ | ||||
|  | ||||
| #include <stdlib.h> | ||||
| #include <stdint.h> | ||||
|  | ||||
| typedef struct { | ||||
|    void *container; | ||||
|    uint16_t size; | ||||
|    uint16_t MaxSize; | ||||
|    uint16_t NodeSize; | ||||
|    int Top; | ||||
| } SqStack; | ||||
|  | ||||
|  | ||||
|  | ||||
| /**********************函数声明区************************/ | ||||
| void InitStack(SqStack *S,uint16_t MaxSize,uint16_t NodeSize);  //初始化栈 | ||||
| void *PushStack(SqStack *S);                                    //进栈 | ||||
| void *PopStack(SqStack *S);                                     //出栈 | ||||
| void DestroyStack(SqStack *S);                                  //销毁栈 | ||||
|  | ||||
| #endif | ||||
							
								
								
									
										23
									
								
								数据结构/栈/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								数据结构/栈/main.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| #include <stdio.h> | ||||
| #include "Stack.h" | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| int main(){ | ||||
|     int *a; | ||||
|     SqStack stack; | ||||
|     InitStack(&stack,20,sizeof(int)); | ||||
|      | ||||
|     for(int i=0;i<20;i++){ | ||||
|         a=(int *)PushStack(&stack); | ||||
|         *a=i; | ||||
|     } | ||||
|     for(int i=0;i<20;i++){ | ||||
|         printf("%d ",*(int *)(stack.container+i*stack.NodeSize)); | ||||
|     } | ||||
|  | ||||
|  | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user