C 我在实现堆栈时遇到问题
我对我的代码有一个问题:我试图编写一个函数,它的名称是C 我在实现堆栈时遇到问题,c,stack,C,Stack,我对我的代码有一个问题:我试图编写一个函数,它的名称是take,该函数只能获取一个int参数,并且必须返回插入的中间数字。该功能必须在尽可能少的内存中使用。我试着在一堆中使用。这是我的实现。问题是程序在第三次插入后不返回值 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> int take (int); typedef struct stack { int num; st
take
,该函数只能获取一个int
参数,并且必须返回插入的中间数字。该功能必须在尽可能少的内存中使用。我试着在一堆中使用。这是我的实现。问题是程序在第三次插入后不返回值
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int take (int);
typedef struct stack
{
int num;
struct stack *next;
}stack;
stack first;
bool isit = true;
int counter = -1;
int main()
{
printf("%d",take(5));
printf("%d", take(6));
printf("%d", take(7));
return 0;
}
int take(int value)
{
if (isit)
{
isit = false;
first.num = value;
first.next = NULL;
}
else
{
static stack newone;
newone.num = value;
newone.next = NULL;
stack temp = first;
while (temp.next != NULL)
{
temp = *temp.next;
}
temp.next = &newone;
}
stack *temp1 = malloc(sizeof(stack));
*temp1 = first;
counter++;
if (counter > 1 && counter % 2 == 0)
{
temp1 = temp1->next;
}
return (temp1->num);
}
#包括
#包括
#包括
int-take(int);
typedef结构堆栈
{
int-num;
结构堆栈*下一步;
}堆叠;
先堆叠;
bool-isit=true;
int计数器=-1;
int main()
{
printf(“%d”,取(5));
printf(“%d”,取(6));
printf(“%d”,取(7));
返回0;
}
整数取值(整数值)
{
如果(isit)
{
isit=假;
first.num=值;
first.next=NULL;
}
其他的
{
静态堆栈newone;
newone.num=值;
newone.next=NULL;
堆栈温度=第一;
while(temp.next!=NULL)
{
温度=*下一个温度;
}
temp.next=&newone;
}
stack*temp1=malloc(sizeof(stack));
*temp1=第一;
计数器++;
如果(计数器>1&&计数器%2==0)
{
temp1=temp1->next;
}
返回(temp1->num);
}
代码中的一个大问题是在不需要全局变量的地方使用全局变量
他们。这会产生意想不到的问题,如:
int take(int value)
{
...
static stack newone;
newone.num = value;
newone.next = NULL;
stack temp = first;
while (temp.next != NULL)
{
temp = *temp.next;
}
temp.next = &newone;
typedef struct stack
{
int stack[20];
size_t len;
size_t size;
} Stack;
void stack_init(Stack *stack)
{
if(stack == NULL)
return;
stack->size = sizeof stack->stack / sizeof stack->stack[0];
stack->len = 0;
}
int stack_is_empty(Stack *stack)
{
if(stack == NULL)
return 1;
return stack->len == 0;
}
int stack_is_full(Stack *stack)
{
if(stack == NULL)
return 0;
return stack->len == stack->size;
}
int stack_push(Stack *stack, int value)
{
if(stack == NULL)
return 0;
if(stack_is_full(stack))
return 0;
stack->stack[stack->len++] = value;
return 1;
}
int stack_pop(Stack *stack, int *val)
{
if(stack == NULL)
return 0;
if(stack_is_empty(stack))
return 0;
stack->len--;
if(val)
*val = stack->stack[stack->len];
return 1;
}
int take(Stack *stack, int value)
{
if(stack == NULL)
return 0;
if(stack_push(stack, value) == 0)
fprintf(stderr, "stack is full, cannot push\n");
return stack->stack[stack->len / 2];
}
int main(void)
{
Stack stack;
stack_init(&stack);
printf("%d", take(5));
printf("%d", take(6));
printf("%d", take(7));
return 0;
}
静态堆栈newone
是一个静态变量,这意味着它将始终是
每次调用take
,都会覆盖这些值,
特别是next
指针
因此,当您可以完全声明时,请避免使用全局变量
在main
函数中输入它们,并将它们传递给其他函数
另外,您的malloc
part没有任何意义。你想要最小的内存占用
但是您分配在temp1=temp1->next之后丢失的内存代码>
如果您想要最小的内存占用,并且不必使用
malloc
,则可以声明固定长度的数组并将其用作堆栈,
大概是这样的:
int take(int value)
{
...
static stack newone;
newone.num = value;
newone.next = NULL;
stack temp = first;
while (temp.next != NULL)
{
temp = *temp.next;
}
temp.next = &newone;
typedef struct stack
{
int stack[20];
size_t len;
size_t size;
} Stack;
void stack_init(Stack *stack)
{
if(stack == NULL)
return;
stack->size = sizeof stack->stack / sizeof stack->stack[0];
stack->len = 0;
}
int stack_is_empty(Stack *stack)
{
if(stack == NULL)
return 1;
return stack->len == 0;
}
int stack_is_full(Stack *stack)
{
if(stack == NULL)
return 0;
return stack->len == stack->size;
}
int stack_push(Stack *stack, int value)
{
if(stack == NULL)
return 0;
if(stack_is_full(stack))
return 0;
stack->stack[stack->len++] = value;
return 1;
}
int stack_pop(Stack *stack, int *val)
{
if(stack == NULL)
return 0;
if(stack_is_empty(stack))
return 0;
stack->len--;
if(val)
*val = stack->stack[stack->len];
return 1;
}
int take(Stack *stack, int value)
{
if(stack == NULL)
return 0;
if(stack_push(stack, value) == 0)
fprintf(stderr, "stack is full, cannot push\n");
return stack->stack[stack->len / 2];
}
int main(void)
{
Stack stack;
stack_init(&stack);
printf("%d", take(5));
printf("%d", take(6));
printf("%d", take(7));
return 0;
}
您所做的唯一内存分配是针对单个结构的。也许你很不幸,第二次插入似乎有效。我希望看到一个固定大小的堆栈,或者使用realloc
。或者这实际上是一个链表?下面是处理类似问题的很好的建议。我真的很想看到/读你向自己或任何人解释你的每一行代码。我希望结果应该比当前代码多出几行注释。注释行的当前计数:0。这对于理解/调试有问题的任何代码来说都太少了。第二个好建议(实际上是第一个建议的一部分,但只是强调一点):