链接列表无法在C中正确打印/添加

时间:2012-07-08 07:51:04

标签: c linked-list

这只是一个粗略的代码,所以那里还没有免费的。 我只想弄清楚它在搞乱链表的位置。

以下功能的目的是接受以下内容:

add 1 2

add 1 "some quote" maybe more stuff

并使用元素制作链接列表.. 在第一种情况下:

[add]->[1]->[2]

在第二种情况下:

[add]->[1]->["some quote" maybe more stuff]

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

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;    
}

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

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

由于

2 个答案:

答案 0 :(得分:1)

如果只打印第一个元素,则必须断定第一个元素的next成员为NULL; conductor已分配conductor->next,但conductor->next本身永远不会分配除NULL以外的任何内容。

添加到结尾时,必须为当前结尾的next成员分配新项目的地址。似乎并没有发生这种情况。

我强烈建议您使用符号调试器来分析此代码。它允许您在监视变量状态的同时逐步执行每一行。

答案 1 :(得分:1)

代码中存在许多问题,使其难以调试。

主要问题是如何在列表末尾添加struct命令*。这条线

        conductor=conductor->next;

只会将导体分配给NULL(或任何malloc所做的)。你永远不会在任何东西旁边分配指挥 - &gt;

当你使用malloc一个新的struct命令*时,你需要更新新分配元素旁边的旧指示符&gt;。所以而不是:

        conductor = malloc(sizeof(struct command));

您需要以下内容:

        struct command *tmp = malloc(sizeof(struct command));
        conductor->next = tmp;
        conductor = tmp;

此外,如果您添加了该行,它可能有助于您的调试             conductor-&gt; args =“尚未分配#1 ”; 在上一行之后。这很糟糕,不应出现在生产代码中,但可以帮助您调试问题。