0

我在将参数传递给链表时遇到问题。我等待用户输入然后我解析这个用户输入并将解析的令牌放入动态数组(char **cmd)

之后,我需要将某些用户输入写入链表(例如,我需要将 cmd[1] 传递给链表)并且此过程在 while 循环中重复,直到用户输入“退出”(因此将要求用户输入另一个输入然后我需要再次解析它等等)

但是在第一个循环之后(用户输入输入,我解析它然后发送 cmd[1] 和 cmd[2] 链接列表)我再次解析字符串,然后我再次发送 cmd[1] 和 cmd[2] 到链表。但是,我的链表将所有以前的值覆盖到新值,然后使用新值添加新节点,因此我的所有节点现在都具有相同的值。我一个月前刚开始学习c,所以我可能有指针问题或者它可能是链表,即使我用c ++编写了这个链表,现在我将它转换为那里可能会在我转换后出现一些错误。我还测试了我的链表,只传递了不是来自动态数组的常规参数,它似乎工作正常。

#include <stdio.h>
#include <string.h>
#include <stdbool.h> 
#include <stdlib.h>

//struct for environmental variables and thier values
struct ListNode
{
    //variable and value in the node
    char *var,*value;
    struct ListNode* next;//point to the next node
};
struct ListNode* head;//pointer to the head
// head = malloc(sizeof(struct ListNode));

void printList();
void appendNode(char *v, char *val);

int main(int argc, const char * argv[])
{
    char user_input[20]={0};

    int i=0;
    char c;

    int count=0;//keep count of words the user entered

    char ** cmd  = NULL;//create pointer and set it to NULL
    int size = 0;


    while (strcmp(user_input, "exit") != 0)//check if exit was typed into cmd line, if so then exit
    {

        //input three words separated with space
        scanf("%50[^\n]", user_input);//scan user input 


        size=0;
        count=0;
        const char delimiters[] = " !-";//create an array of delimiters to use to separate string

        char *ptr = strtok (user_input, delimiters);

        cmd = malloc(sizeof(char));//allocate memory for pointer cmd

        while (ptr)//split string into tokens to " !-"
        {
            cmd = realloc (cmd, sizeof(char*)*(++size));//reallocate more memory for an array

            if (cmd == NULL)
                exit (-1); // memory allocation failed

            cmd[size-1] = ptr;

            ptr = strtok (NULL, delimiters);

            count++;//count words in the input
        }


        cmd = realloc (cmd, sizeof(char*)*(size+1));// realloc one extra element for the last NULL
        cmd[size] = 0;

        for (i = 0; i < (size+1); ++i)
            printf ("cmd[%d] = %s\n", i, cmd[i]);


         //free(cmd);//free memory


        while((c = getchar()) != '\n' && c != EOF); //flush buffer


        if (count==3 )//set environmental variable

            {

                appendNode(cmd[1], cmd[2]);
            }
        printList();

    }
     free(cmd);
    return 0;
}


void appendNode(char *v, char *val)
{
    struct ListNode* newNode;//to point to new node
    struct ListNode* nodePtr = NULL;//to move through the list

    //allocate new node and store int there
    newNode = malloc (sizeof( struct ListNode));
    newNode->var=v;//put value of v into new variable
    newNode->value=val;//put value of val into new value
    newNode->next = NULL;
    //if there are no nodes in the list make new node th efirst node
    if(!head)
    {
        head = newNode;

    }
    else
    {
        //initialize nodePtr to head
        nodePtr = head;
        //find the last node in the list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }
        //insert new node as the last node
        nodePtr->next = newNode;
    }

}
void printList()
{
    struct ListNode* nodePtr = head; //position nodePtr at the head
    //while noePtr points to node, traverse the list
    while(nodePtr)
    {
        //display value
        printf("%s\n",nodePtr->var);
        //move to next node
        nodePtr = nodePtr->next;
    }

}
4

1 回答 1

1

问题是由于发送本地指针 cmd 每次都会为主程序中的新字符串重新分配。

这可以通过修改 appendnode API 来解决

void appendNode(char *v, char *val)
{
    struct ListNode* newNode;//to point to new node
    struct ListNode* nodePtr = NULL;//to move through the list

    //allocate new node and store int there
    newNode = malloc (sizeof( struct ListNode));
    newNode->var = malloc(sizeof(v));
    newNode->value = malloc(sizeof(val));   
    strcpy(newNode->var, v);
    strcpy(newNode->value, val);    
    newNode->next = NULL;
    //if there are no nodes in the list make new node th efirst node
    if(!head)
    {
        head = newNode;

    }
    else
    {
        //initialize nodePtr to head
        nodePtr = head;
        //find the last node in the list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }
        //insert new node as the last node
        nodePtr->next = newNode;
    }

}
于 2013-02-11T03:38:13.357 回答