gpt4 book ai didi

c - 从 C 中的 FIFO 链接列表中弹出函数不起作用

转载 作者:行者123 更新时间:2023-11-30 17:35:13 25 4
gpt4 key购买 nike

几天后,我将参加一场关于 C 语言的非常重要的考试。我很棒的老师给了我一点帮助,除了其他事情之外,还给了我这次考试。这里我必须让函数“pop”。我几乎整个周末都在尝试,但仍然不起作用,这让我感到沮丧。也许你们可以帮助我。我希望 pop 是 FIFO 的。

#include <stdio.h>

typedef struct test NODE;
typedef NODE *NV;

struct test
{
int data;
NV next;
};

int empty(NV);
int geeftop(NV);
void pop(NV, NV *, NV *);
void push(NV *, NV *, int);

int main(void)
{
int k, number;
int controle;
NV start;
NV staart;
staart = NULL;
start = NULL;

do
{
system("CLS");
printf("\nKeuzemenu:\n");
printf("[1] = ADD.\n");
printf("[2] = DELETE.\n");
printf("[3] = IS IT EMPTY?.\n");
printf("[4] = Stop.\n\n");
printf("type your number: ");

do
{
scanf("%d", &k);
} while (k <= 0 || k >= 5);

switch (k)
{
case 1: printf("give a number: ");
scanf("%d", &number);
push(&start, &staart, number);
break;
case 2: if (!empty(start))
{
pop(start, &start ,&staart);
printf("\ntop is deleted.\n\n");
}
else
printf("\nerror.\n\n");
system("PAUSE");
break;
case 3: controle = empty(start);
if (controle == 1)
printf("\nits emtpy.\n\n");
else
printf("\nniet empty.\n\n");
system("PAUSE");
break;
default: break;
}
} while (k != 4);

system("PAUSE");
return 0;
}

int empty(NV phuidige)
{
return (phuidige == NULL);
}

void pop(NV phuidige2, NV *phuidige, NV *pstaart)
{
if ((phuidige2) -> next == NULL)
{
NV weg;
NV weg2;
weg = *pstaart;
weg2 = *phuidige;
pstaart = NULL;
phuidige2 = NULL;
free (pstaart);
free (phuidige2);
}
else
{
while ((phuidige2) -> next -> next != (NULL))
{
phuidige2 = phuidige2 -> next;
}
}

{
phuidige2 -> next = NULL;
NV weg;
weg = *pstaart;
*pstaart = NULL;
free (weg);
}
}

void push(NV *phuidige, NV *pstaart, int x)
/* pre er is ruimte op de stack;
post stack(post) = stack(pre) U {x}; */
{
NV hulp;

hulp = malloc(sizeof(NODE));
hulp -> data = x;
hulp -> next = *phuidige;
*phuidige = hulp;

if (*pstaart == NULL)
{
*pstaart = hulp;
}
else
{
}
}

最佳答案

您确实应该检查 scanf 的返回值,k 的值未初始化。基本上 pop 应该只返回你的头指针,并将头指针前进到下一个元素 - 随时检查 null 。

关于c - 从 C 中的 FIFO 链接列表中弹出函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23049591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com