gpt4 book ai didi

c - 链接列表不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:27 25 4
gpt4 key购买 nike

我有以下代码,当我添加节点时,为什么它会将所有节点重置为最新的节点数据?假设代码中的其他所有内容都正常工作,解析等任何人都可以识别列表中的问题。查看底部的输出。

typedef char* string;
typedef struct linked_list listType;
typedef struct linked_list* linked_list;
typedef struct node NodeType;
typedef struct node* Node;

typedef enum boolean{
true = 1,
false = 0
}boolean;

struct node{
Node next;//next node in the list
string *transData;//listAdd--->string data[]
};

struct linked_list{
Node head;//first node in the list
int size;//size of list, number of nodes in list
};

boolean add(linked_list list, string *data)
{
boolean retval = false;//default retval is false
Node nod;//node to add
nod = (Node)malloc(sizeof(NodeType));
nod->transData = data;
nod->next = NULL;

//list size is 0
if(list->head == NULL)
{
list->head = nod;
printf("First Node added: '%s'\n", nod->transData[0]);fflush(stdout);
retval = true;
}

//nodes already in list
else
{
printf("List with nodes already\n");
fflush(stdout);
Node node = list->head;

//iterating through nodes
while(node->next != NULL)
{
node = node->next;
}//while

node->next = nod;
printf("Node added: '%s'\n", nod->transData[0]); fflush(stdout);
retval = true;
}

list->size+=1;
return retval;//success
}

/**
* Returns the size of the list.
*/
int listSize(linked_list list)
{
return list->size;
}

/**
*Can only print with 2 or more elements
*/
void printList(linked_list list)
{
int i=0;
Node nod = list->head;
int length = listSize(list);
printf("ListSize:%d\n",listSize(list));
fflush(stdout);

while(nod->next != NULL)
{
printf("Node %d's data: %s\n", i, nod->transData[0]);
fflush(stdout);
nod = nod->next;
i++;
}
}//printlist

输出:

trans 5 5  
Argument 1: trans
Argument 2: 5
Argument 3: 5
Last spot(4) is: (null)
name of command: trans
ID 1
First Node added: 'trans'
ListSize:1
>>check 4
Argument 1: check
Argument 2: 4
Last spot(3) is: (null)
name of command: check
ID 2
List with nodes already
Node added: 'check'
ListSize:2
Node 0's data: check
>>trans 4 4
Argument 1: trans
Argument 2: 4
Argument 3: 4
Last spot(4) is: (null)
name of command: trans
ID 3
List with nodes already
Node added: 'trans'
ListSize:3
Node 0's data: trans
Node 1's data: trans
>>check 5
Argument 1: check
Argument 2: 5
Last spot(3) is: (null)
name of command: check
ID 4
List with nodes already
Node added: 'check'
ListSize:4
Node 0's data: check
Node 1's data: check
Node 2's data: check

最佳答案

(OP 告诉我节点需要有字符串数组,所以指向指针的指针实际上是正确的)。

无论如何,看起来你的问题是每个节点都有一个指向同一个缓冲区的指针。您只需将不同的字符串复制到同一个缓冲区中,然后将指向该缓冲区的指针地址分配给每个新节点。

首先,我会去掉那些 typedef。我不知道他们真正增加了多少清晰度。

接下来,您需要为每个字符串分配新的存储空间。标准库 strdup() 函数是一种方便的方法:

//  Turns out it's a fixed-size array, so we can blow off some memory 
// management complexity.
#define CMDARRAYSIZE 21

struct node {
Node next;
char * transData[ CMDARRAYSIZE ];
};

// Node initialization:

int i = 0;

struct node * nod = (struct node *)malloc( sizeof( struct node ) );
// Initialize alloc'd memory to all zeroes. This sets the next pointer
// to NULL, and all the char *'s as well.
memset( nod, 0, sizeof( struct node ) );

for ( i = 0; i < CMDARRAYSIZE; ++i ) {
if ( NULL != data[ i ] ) {
nod->transData[ i ] = strdup( data[ i ] );
}
}

...但是请确保当您释放每个节点时,您为 nod->transData 中的每个非 NULL 字符串指针调用 free(nod->transData[n])。 C 不是一种省力的语言。

关于c - 链接列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5263620/

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