【发布时间】:2012-01-27 05:26:30
【问题描述】:
我一直在从事一项涉及实现包含 void 指针的队列的任务,以便可以将它们推广到任何类型的数据。我目前遇到了一个奇怪的问题,虽然出队节点会减少列表的大小,但不会返回我期望的节点。在出队操作中省略对 free() 的调用可以解决此问题,但是由于我想释放出队的节点,因此这是不可取的。有什么建议吗?
测试运行:routine.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"
int main() {
queue test = make_queue();
enqueue("One", test);
enqueue("Two", test);
printf("Item is %s!\n", (char *)dequeue(test));
printf("Item is %s!\n", (char *)dequeue(test));
return 0;
}
queue.h
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
/* A queue is implemented as a pointer to a structure not specified here. */
typedef struct queue_structure *queue;
struct node {
struct node * next;
void * data;
};
struct queue_structure {
struct node * head;
struct node * tail;
};
/* List of function protocols. */
bool is_empty_queue(queue q);
/* The make_queue function returns a newly created queue with no values
stored in it.
*/
queue make_queue() {
queue newQueue = malloc(sizeof(struct queue_structure));
return newQueue;
}
/* The enqueue function adds a value to a queue. Although this function
does not change the pointer q, fields of the structure to which q
points may be modified in the course of a call to this function.
*/
void enqueue(void *value, queue q) {
struct node * newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if(is_empty_queue(q))
q->tail = newNode;
newNode->next = q->head;
q->head = newNode;
}
/* The dequeue function removes a value from a queue and returns it.
Although this function does not change the pointer q, fields of the
structure to which q points may be modified in the course of a call to
this function.
It is a precondition of this function that at least one value is stored
in the queue.
*/
void *dequeue(queue q) {
if(!q->head->next) { // Only a single item in the queue.
printf("Only one item in queue!\n");
struct node * to_dequeue = q->tail;
void * data = q->head->data;
free(to_dequeue);
q->head = NULL;
q->tail = NULL;
return data;
}
else { // Multiple items in the queue.
printf("Several items in queue!\n");
struct node * to_dequeue = q->tail;
void * data = q->tail->data;
struct node * trace = q->head;
while(trace->next && trace->next != q->tail)
trace = trace->next;
free(to_dequeue);
q->tail = trace;
q->tail->next = NULL;
return data;
}
}
/* The front_of_queue function returns the value at the front of a queue
(that is, the one least recently added to the queue) without removing
that value from the queue. It has no side effect.
It is a precondition of this function that at least one value is stored
in the queue.
*/
void *front_of_queue(queue q) {
return q->head->data;
}
/* The is_empty_queue function determines whether a queue is empty,
returning the true Boolean value if no values are stored in the queue
and the false Boolean value if one or more values are stored in the
queue.
*/
bool is_empty_queue(queue q) {
if(q->head)
return 1;
return 0;
}
【问题讨论】:
-
一个小提示:您通过将所有内容都放在一个头文件中来混合接口和实现。这意味着你不能让两个不同的源文件使用队列,因为你会得到链接器错误。将实现(函数中的实际代码)放在单独的源文件中,头文件中只有结构和函数原型。
-
另一个注意事项:您期望函数的参数是有效的,但它们可能不是。此外,例如在
dequeue中,您必须检查是否有空队列。对于一个简单的家庭作业应用程序来说可能没问题,但是好习惯(比如检查论点和类似的东西)最好早点学习。 :)