2012-07-08 101 views
1

這只是一個粗略的代碼,所以目前還沒有免費的。 我只是想弄清楚它搞亂了我的鏈表。鏈接列表不打印/在C中正確添加

以下功能的目的是接受這樣的:

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

感謝

回答

1

代碼中有許多問題使得它很難調試。

主要問題是如何將結構命令*添加到列表的末尾。該行

 conductor=conductor->next; 

將只是指定NULL(或任何malloc做)。你永遠不會分配指揮 - >旁邊的任何東西。

當你malloc一個新的struct命令*時,你需要更新舊的conductor-> next到新分配的元素。因此,而不是:

 conductor = malloc(sizeof(struct command)); 

你需要的東西,如:

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

此外,它可以幫助你調試,如果你增加了行 舉辦商> ARGS = 「未分配尚未#1」 ;上面最後一行之後的 。這是嚴重的,不應該出現在生產代碼中,但會幫助你調試你的問題。

1

如果只打印的第一要素,必須得出這樣的結論的的next成員第一個元素是NULL; conductor被分配爲conductor->next,但conductor->next本身從不被分配NULL以外的任何內容。

添加到最後時,必須爲當前結尾的next成員分配新項目的地址。似乎並沒有發生這種情況。

我強烈建議您使用符號調試器來分析此代碼。它將允許您在監視變量狀態的同時遍歷每一行。