我正在实现QueueADT的单个链表版本。在创建队列时,如果客户端为我们提供了比较功能,我们将使用它来将新数据插入队列。如果客户端没有提供比较功能,我们使用标准队列插入,只插入队列的后面。
我在使用比较功能插入的逻辑上遇到了麻烦。我们只知道比较函数返回的内容。
compare( void*a, void*b)
//compare returns < 0 if a < b
//compare returns = 0 if a == b
//compare returns > 0 if a > b
我有你的标准队列和链接节点结构:
typedef struct queueStruct {
Node *front;
Node *rear;
int count;
int (*compare)(const void*, const void*);
};
typedef struct Node {
void* value;
struct Node *next;
}Node;
这是我对插入功能的尝试。我不认为逻辑是正确的,并且会对此有所了解甚至是伪代码!
void que_insert(queueStruct queue, void *data){
//if the queue is empty
if (que_empty(queue)){
Node *node;
node = malloc(sizeof(Node));
node -> value = data;
node -> next = NULL;
queue->front = node;
queue->rear = node;
queue->count++;
}
else{
//if there is no comparison function, just use FIFO
if (queue->compare == NULL){
printf("Fifo\n");
Node *node;
node = malloc(sizeof(Node));
node->value = data;
node->next = NULL;
queue->rear->next = node;
queue->rear = node;
queue->count++;
}
else{
Node *temp;
temp = queue->front;
//if what we are adding is smaller than the head, then we found our new head
if (queue->compare(data, temp->value) < 0){
printf("Less Than 0\n");
Node *node;
node = malloc(sizeof(Node));
node->value = data;
node->next = queue->front;
queue->front = node;
queue->count++;
return;
}
while (temp->next != NULL){
if (queue->compare(data, temp->value)> 0){
printf("Greater than 0\n");
temp = temp->next;
}
else if (queue->compare(data, temp->value) == 0){
printf("Equals 0\n");
Node *node;
node = malloc(sizeof(Node));
node->value = data;
node->next = temp->next;
temp->next = node;
queue->count++;
return;
}
}
//temp should be at the rear
if (queue->compare(data, temp->value)> 0){
printf("Adding to rear");
Node *node;
node = malloc(sizeof(Node));
node->value = data;
node->next = NULL;
}
}
}
}
测试:
尝试将以下数据插入队列时:
42, 17, -12, 9982, 476, 2912, -22, 3291213, 7782
似乎插入这些值直到最后一个,程序挂起
inserting 7782
Greater than 0
Greater than 0
Greater than 0
Greater than 0
答案 0 :(得分:2)
首先。如果您的比较需要严格的弱排序(它应该),则不需要所有添加的比较器调用。这基本上意味着以下
if (compare(a,b) < 0)
then a is less than b
else if !(compare(b,a) < 0)
then a and b are equal
else b is less than a
这通常用于标准库,因为一方面,它使得比较枚举必须更容易遵循。它还只需要定义一个逻辑操作。 “少”
正如我在一般性评论中所说,整合新节点的分配位置。看来你的队列支持重复(它应该),因此无论你总是添加一个新节点,所以只需从一开始就制作它,然后专注于找到它的位置。
最后,一个指向节点的指针将使你的插入更加简洁(事实上,我打赌你会发现它非常短):
void que_insert(queueStruct* queue, void *data)
{
// going to need this sooner or later
Node *node = malloc(sizeof(*node));
node->value = data;
node->next = NULL;
//if the queue is empty
if (queue->count == 0)
{
queue->front = queue->rear = node;
queue->count = 1;
}
// else not empty, but no comparator
else if (queue->compare == NULL)
{
//if there is no comparison function, just use FIFO
queue->rear->next = node;
queue->rear = node;
queue->count++;
}
// else not empty and we have a comparator
else
{ // walk over chain of node pointers until we find a pointer
// that references a node equal or greater than ours, or EOQ
Node **pp = &queue->front;
while (*pp && queue->compare((*pp)->value, data) < 0)
pp = &(*pp)->next;
// no more nodes means new rear. otherwise link mid-stream
if (!*pp)
queue->rear = node;
else
node->next = *pp;
// either way, this is always done.
*pp = node;
queue->count++;
}
}
如何运作
我们使用指向节点的指针来保存我们正在检查的每个指针的地址,从头指针开始。这有几个优点。我们不需要跟踪指向节点的指针只是为了访问它的“下一个”,因为我们已经有按地址。我们得到自动前插,因为我们从头指针地址开始,唯一需要考虑的是后更新,这仍然必须手动完成,但是这是微不足道的。
指针到指针遍历可能看起来有点令人生畏,但它有一些很棒的特性。没有指针到节点的枚举器。您实际上使用列表中的指针作为枚举变量。不只是他们的“价值”,而是实际的指针和他们的价值观。您所做的只是通过更新指针指针中的地址来改变您正在使用的那个,以反映列表中的中的物理指针(而不仅仅是到您正在处理的列表)。当需要改变某些东西时,你不需要指向你需要改变的“下一个”指针的节点的指针;你已经有你要修改的指针的地址,因此可以立即这样做。
我没有测试附加的代码,但它应该可以工作。只需确保初始queueStruct
设置为零计数,后指针和前指针均为NULL。这显然是重要的。
最后,我强烈建议使用调试器逐步完成此操作。没有什么可以替代“看到”实际指针地址和通过代码飞行的值。铅笔和一张纸有框,箭头到方框,箭头到箭头到方框也有助于理解算法。