链接列表不起作用

时间:2011-03-10 17:46:18

标签: c linked-list

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

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  

3 个答案:

答案 0 :(得分:3)

(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-&gt; transData中的每个非空字符串指针调用free(nod-&gt; transData [n])。 C不是省力的语言。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

而不是while(nod->next != NULL)使用while(nod != NULL)