链接列表未在C中正确打印/添加
这只是一个粗略的代码,所以还没有免费的。 我只是想弄清楚它把我的链表搞砸了 以下函数的目的是接受以下内容:链接列表未在C中正确打印/添加,c,linked-list,C,Linked List,这只是一个粗略的代码,所以还没有免费的。 我只是想弄清楚它把我的链表搞砸了 以下函数的目的是接受以下内容: add 1 2 或 并创建一个包含元素的链表。。 在第一种情况下: [add]->[1]->[2] 在第二种情况下: [add]->[1]->["some quote" maybe more stuff] 我知道它实际上是在执行这些步骤,因为“count/total”在输出中是正确的。但是,当我尝试遍历链表时,它只打印第一个元素 typedef struct
add 1 2
或
并创建一个包含元素的链表。。
在第一种情况下:
[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;
}
谢谢如果它只打印第一个元素,您必须断定第一个元素的
下一个
成员为空<代码>导体已分配导体->下一个
,但导体->下一个
本身从未分配过除NULL以外的任何内容
添加到结尾时,必须为当前结尾的下一个成员分配新项目的地址。看来情况并非如此
我强烈建议您使用符号调试器来分析此代码。它将允许您在监视变量状态时单步遍历每一行 代码中存在许多问题,使调试变得困难
主要问题是如何将struct命令*添加到列表的末尾。线路
conductor=conductor->next;
只会将导体分配给NULL(或malloc所做的任何事情)。您从未将导体->分配到任何对象旁边
在malloc一个新的struct命令*时,需要更新新分配元素旁边的旧conductor->。因此,不是:
conductor = malloc(sizeof(struct command));
你需要像这样的东西:
struct command *tmp = malloc(sizeof(struct command));
conductor->next = tmp;
conductor = tmp;
此外,如果您添加了行,它可能有助于调试
指挥->args=“尚未分配#1”;
在上面最后一行之后。这很糟糕,不应该出现在生产代码中,但会帮助您调试问题
conductor = malloc(sizeof(struct command));
struct command *tmp = malloc(sizeof(struct command));
conductor->next = tmp;
conductor = tmp;