首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >链表不能在C中正确打印/添加

链表不能在C中正确打印/添加
EN

Stack Overflow用户
提问于 2012-07-08 15:51:04
回答 2查看 140关注 0票数 1

这只是一个粗略的代码,所以还没有免费的代码。我只是想找出哪里弄乱了我的链表。

以下函数的目的是接受如下内容:

代码语言:javascript
运行
复制
add 1 2

代码语言:javascript
运行
复制
add 1 "some quote" maybe more stuff

并生成包含元素的链表..在第一种情况下:

代码语言:javascript
运行
复制
[add]->[1]->[2]

在第二种情况下:

代码语言:javascript
运行
复制
[add]->[1]->["some quote" maybe more stuff]

我知道它实际上是在做这些步骤,因为'count/total‘就在输出中。但是,当我尝试遍历链表时,它只打印第一个元素。

代码语言:javascript
运行
复制
typedef struct command{
    char* args;
    struct command *next;
}command;
typedef struct commands_list{
    command *head;  /*Start of the queue*/
    int total;  /*Total commands passed*/
}commands_list;

commands_list* process_command(char *command){
    char curr_char;                 /*Keeps track of current character*/
    int start_pos;
    int i;
    int len;    

    /*Length of user input*/
    int quote=0;
    int empty =1;
    commands_list *commands;
    struct command *conductor;

    len = strlen(command);              /*Calculate length*/    
    /*Initialize the List*/
    commands=malloc(sizeof(commands_list));         /*Allocate memory for the linked list*/
    commands->head = malloc(sizeof(struct command));
    conductor = commands->head;

    for(i=0,start_pos=0;i<strlen(command);i++){
        curr_char = command[i];
        if (empty==0){
            conductor = malloc(sizeof(struct command));
        }
        if (curr_char == ' '){      /*If there was a space found copy the stuff before the space*/
            if ( i>0 && command[i-1]==' ') {
                start_pos++;
                continue;
            }
            conductor->args = malloc(i-start_pos+1*(sizeof(char))); /*Allocate memory for the word to be copied*/
            strncpy(conductor->args,command+start_pos,i-start_pos); /*Copy the word/command to the memory allocated*/
            conductor->args[i-start_pos+1]='\0';            /*Add null terminator at end*/
            commands->total++;              /*Increase total # of commands*/
            conductor=conductor->next;          /*Conductor points to the first element now*/
            start_pos =i+1;
            if (empty==1){
                empty=0;
            }
        }
        else if (curr_char == '\"'){        /*If a quote was found, copy the rest of the string and exit loop*/
            conductor->args = malloc(len-i+1*(sizeof(char)));
            strncpy(conductor->args,command+i,len-i);
            conductor->args[len-i+1]='\0';
            conductor->next=NULL;
            commands->total++;
            quote=1;
            //empty_queue = 0;
            conductor = conductor->next;
            if (empty==1){
                empty=0;
            }
            break;
        }
    }
    if (quote==0){                  /*If there was no quote in the string, get the last element*/
        if (empty==0){
            conductor = malloc(sizeof(struct command));
        }
        conductor->args = malloc(len-start_pos+1*(sizeof (char)));
        strncpy(conductor->args,command+start_pos,len-start_pos);
        conductor->args[len-start_pos+1]='\0';
        conductor->next=NULL;
        commands->total++;
    }   /*Finish find quote*/
    printf("%d commands found\n",commands->total);
    //free(conductor);
    return commands;    
}

和一个我用来打印链表的临时方法:

代码语言:javascript
运行
复制
int print_list(commands_list **headNode){
    commands_list *top = *headNode;
    struct command *temp = top->head;           /*Temporary variable for command*/
    while(temp!=NULL){
        printf("I was here to print: [%s]\n",temp->args);
        temp = temp->next;
    }
    printf("It was all null\n");

    free(temp);
}

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-08 16:38:59

代码中有许多问题,使得调试变得很困难。

主要的问题是如何将struct command *添加到列表的末尾。这条线

代码语言:javascript
运行
复制
        conductor=conductor->next;

我只是将conductor赋值为NULL (或者malloc做过的任何事情)。您从不将conductor->指定到任何对象的旁边。

当您malloc一个新的struct命令*时,您需要更新新分配的元素旁边的旧conductor->。因此,不是:

代码语言:javascript
运行
复制
        conductor = malloc(sizeof(struct command));

你需要这样的东西:

代码语言:javascript
运行
复制
        struct command *tmp = malloc(sizeof(struct command));
        conductor->next = tmp;
        conductor = tmp;

此外,如果您在上面的最后一行之后添加了线导线->args=“尚未分配#1";,可能会对您的调试有所帮助。这很粗俗,不应该出现在生产代码中,但可以帮助您调试问题。

票数 1
EN

Stack Overflow用户

发布于 2012-07-08 16:21:36

如果它只打印第一个元素,您必须断定第一个元素的next成员是空的;conductor被分配了conductor->next,但conductor->next本身从未被分配到除NULL之外的任何值。

在添加到end时,必须为当前end的next成员分配新项的地址。这似乎不是正在发生的事情。

我强烈建议您使用符号调试器来分析这段代码。它将允许您在监视变量状态的同时遍历每一行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11381562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档