gpt4 book ai didi

c - 链接列表有一个指向所需数据的指针

转载 作者:行者123 更新时间:2023-11-30 19:46:24 26 4
gpt4 key购买 nike

我有一个链接列表,其中包含完整信息

    typdef struct details
{
struct details *head, *next, *curr;
char* values;
}


Details *fyllinfo ; //(this strcuct has full information)

/* fyllinfo 已填充了许多值*/

现在我需要将指针复制到新列表,该列表只能指向我所需或匹配的数据 详细信息*必填;

    char* myValue;


details *pstPtr =fyllinfo->head;

details *required = (details*) malloc(
sizeof(details));
required->head = required->next = NULL;

while(pstPtr != NULL)
{
if(NULL == pstPtr->values) goto NEXT;
printf("UUID FOUND IS [%s] and comapre with [%s] ",pstPtr->values,myvalue);

if(strCmp(pstPtr->values,myvalue)==0)
{

if(required->head == NULL)
{
required->head = required->curr = pstPtr;
}
else
{

required->next = pstPtr;
required->curr->next = pstPtr;
}
}
NEXT: pstPtr = pstPtr->next;
}

我所需的 ptr 应该具有仅指向匹配值的指针

我的代码只指向第一个匹配的数据,并且在reuired->next指针中有不匹配的数据

最佳答案

这段代码现在可以工作了......你只需要花一些努力来理解它。您应该能够打印这些值。查看现在如何存储值。

details *pstPtr = fyllinfo;

details *required = NULL; //(details*)malloc(
details *head=NULL;
while (pstPtr != NULL)
{
if (1 == pstPtr->values)
printf("UUID FOUND IS [%d] and comapre with [%d] \n", pstPtr->values, 1);

if (pstPtr->values==1)
{

if (required == NULL)
{
required = (details*)malloc( sizeof(details));
head = required;
required->head = required->curr = pstPtr;
required->next = NULL;
}
else
{

required->next = (details*)malloc(sizeof(details));
required = required->next;
required->next = NULL;
required->curr = pstPtr;
}
}
pstPtr = pstPtr->next;
}




void display(struct details *r)
{
// r = fyllinfo;
if (r == NULL)
{
return;
}
while (r != NULL)
{
printf("%d ", r->curr->values);
r = r->next;
}
printf("\n");
}

关于c - 链接列表有一个指向所需数据的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23946815/

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