链接列表不起作用

发布于 2024-10-21 18:02:01 字数 2838 浏览 1 评论 0原文

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

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  

I have the following code, when I add nodes why does it reset all nodes to the most recent nodes data? Assuming everything else in the codes works correctly, parsing etc. can anyone identify a problem with the list. See output at the bottom.

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

Output:

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-10-28 18:02:01

(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 ] );
    }
}

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

(The OP tells me that the nodes need to have arrays of strings, so the pointer-to-a-pointer thing is actually correct).

Anyhow, it looks like your problem is that every node has a pointer to the same buffer. You just keep copying different strings into the same buffer, and then you assign the address of a pointer to that buffer to each new node.

First, I'd get rid of those typedefs. I don't know how much clarity they're really adding.

Next, you need to allocate new storage for each string. The standard library strdup() function is a handy way to do that:

//  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 ] );
    }
}

...but then be sure that when you free each node, you call free( nod->transData[n] ) for every non-NULL string pointer in nod->transData. C is not a labor-saving language.

━╋う一瞬間旳綻放 2024-10-28 18:02:01

您的问题可能是由于指针复制造成的。您可能在所有节点中引用相同的缓冲区。例如,尝试使用 strdup 复制字符串数据。

Your issue is probably due to pointer copying. You are likely referencing the same buffer in all your nodes. Try to make a copy of your string data with strdup for example.

在你怀里撒娇 2024-10-28 18:02:01

不是主要问题,但您的 printList 不会打印出最后一个元素。

使用 while(nod != NULL) 代替 while(nod->next != NULL)

Not the main issue, but your printList won't print out the last element.

Instead of while(nod->next != NULL) use while(nod != NULL)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文