基于数组的栈实现

基于数组的栈实现(函数)
1.入栈
2.出栈
3.初始栈
4.打印栈
This commit is contained in:
Jdhggg 2025-03-21 18:54:51 +08:00
parent 361849b19f
commit 72d649b09f
16 changed files with 129 additions and 26 deletions

View File

@ -19,12 +19,14 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="array_stack.c" />
<ClCompile Include="linked_list.c" />
<ClCompile Include="linked_list_stack.c" />
<ClCompile Include="main.c" />
<ClCompile Include="sq_list.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="array_stack.h" />
<ClInclude Include="linked_list.h" />
<ClInclude Include="linked_list_stack.h" />
<ClInclude Include="sq_list.h" />

View File

@ -27,6 +27,9 @@
<ClCompile Include="sq_list.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="array_stack.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sq_list.h">
@ -38,5 +41,8 @@
<ClInclude Include="linked_list_stack.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="array_stack.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,4 @@
C:\code\lencode\Project2\array_stack.c;C:\code\lencode\Project2\Project2\Debug\array_stack.obj
C:\code\lencode\Project2\linked_list.c;C:\code\lencode\Project2\Project2\Debug\linked_list.obj
C:\code\lencode\Project2\linked_list_stack.c;C:\code\lencode\Project2\Project2\Debug\linked_list_stack.obj
C:\code\lencode\Project2\main.c;C:\code\lencode\Project2\Project2\Debug\main.obj

View File

@ -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

58
array_stack.c Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#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");
}

22
array_stack.h Normal file
View File

@ -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

View File

@ -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;

43
main.c
View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;