【发布时间】:2017-03-14 11:04:28
【问题描述】:
我有两个使用 inputFront、inputRear、outputFront 和 outputRear 跟踪的链接列表。当我将数据从输入排入输出时,我的问题就出现了。 Printf 仅打印输入中的第一项。例如:我的输入是“a b c”,它只会返回“a”。我会很感激帮助。谢谢。
struct linked_list{
char *operand;
struct linked_list *next;
};
void enqueue(struct linked_list** queueFront, struct linked_list** queueRear,
char* token);
char* dequeue(struct linked_list** queueFront, struct linked_list** queueRear);
int main(int argc, char *argv[]){
struct linked_list *inputFront = NULL, *inputRear = NULL;
struct linked_list *outputFront = NULL, *outputRear = NULL;
for (int i = 1; i < argc; i++)
enqueue(&inputFront, &inputRear, argv[i]);
for (struct linked_list *p = inputFront; p != NULL; p = p->next)
enqueue(&outputFront, &outputRear, dequeue(&inputFront, &inputRear));
for (struct linked_list *p = outputFront; p != NULL; p = p->next)
printf("%s \n", dequeue(&outputFront, &outputRear));
}
void enqueue(struct linked_list** queueFront, struct linked_list** queueRear,
char* token){
struct linked_list* newNode = (struct linked_list*) malloc(
sizeof(struct linked_list));
newNode->operand = token;
newNode->next = NULL;
if (*queueRear == NULL && *queueFront == NULL){
*queueFront = *queueRear = newNode;
return;
}
(*queueRear)->next = newNode;
*queueRear = newNode;
}
char* dequeue(struct linked_list** queueFront, struct linked_list** queueRear){
if (*queueFront != NULL){
struct linked_list *remv = *queueFront;
char *remOperand = (*queueFront)->operand;
*queueFront = remv->next;
if (remv == (*queueRear))
*queueRear = NULL;
free(remv);
return remOperand;
}
else
return NULL;
}
【问题讨论】:
-
请发minimal reproducible example 并附上输入、实际输出和预期输出的示例。
-
好吧,你得到了帮助。提供反馈是否对您有帮助是有礼貌的。
标签: c linked-list queue