比较奇怪的是,我把dequeue操作放到迭代里面按顺序打印字符,但是失败了,只打印前两个字符,但是当我在迭代外使用这个dequeue操作时,就没事了!
我将出队定义如下:
char *C_dequeue(struct COMMON_QUEUE *q) {
if (C_get_queue_length(q)==0) {
fprintf(stderr, "Linked queue is empty!!!");
return NULL;
}
struct QUEUE_NODE *p;
p = malloc(sizeof(struct QUEUE_NODE));
if (p==NULL) {
return NULL;
}
p = q->q_node;
char *temp;
temp = p->next->string;
p = p->next;
q->q_node->next = p->next;
free(p);
p->next = NULL;
p->string = NULL;
return temp;
}
并在 main.c 文件中使用它:
struct COMMON_QUEUE *test10;
test10 = C_init_queue();
C_enqueue(test10, "Someone ");
C_enqueue(test10, "like ");
C_enqueue(test10, "you!");
printf("length: %d\n", C_get_queue_length(test10));
int j;
/* it prints the first two characters */
for (j = 0; j <= C_get_queue_length(test10)+1; ++j) {
printf("[%d %s]", j, C_dequeue(test10));
}
printf("\n");
/* it prints all the characers */
C_dequeue(test10);
C_dequeue(test10);
C_dequeue(test10);
C_destroy_queue(test10);
我想我可能犯了一些愚蠢的错误!!!