优先级队列LL在C语言中的实现错误?继续链接回其他说明

优先级队列LL在C语言中的实现错误?继续链接回其他说明,c,linked-list,queue,priority-queue,C,Linked List,Queue,Priority Queue,当我尝试使用我实现为链表的优先级队列类时,我发现我的指针一直指向彼此。我通过在线找到的这个简单队列实现对其进行了修改: 这是我的队列和节点结构: typedef struct Node { Instruction pointer; struct Node *next; } Node; typedef struct Queue { Node *head; Node *tail; void (*push) (struct Queue *, Instruction);

当我尝试使用我实现为链表的优先级队列类时,我发现我的指针一直指向彼此。我通过在线找到的这个简单队列实现对其进行了修改:

这是我的队列和节点结构:

typedef struct Node
{
  Instruction pointer;
  struct Node *next;
} Node;

typedef struct Queue
{
  Node *head;
  Node *tail;

  void (*push) (struct Queue *, Instruction);
    Instruction (*pop) (struct Queue *);
    Instruction (*peek) (struct Queue *);
    Instruction (*removeNode) (struct Queue *, Instruction);
  int size;
} Queue;
指令是指向我已实现的另一个名为Command的结构的指针。 这是我的推送和弹出方法:

void
push (Queue * queue, Instruction instr)
{

  Node *n = (Node *) malloc (sizeof (Node));
  n->pointer = instr;
  n->next = NULL;

  if (queue->head == NULL
      || n->pointer->priority > queue->head->pointer->priority)
    {
      n->next = queue->head;
      queue->head = n;
      queue->size++;
    }
  else
    {
      Node *temp = queue->head;
      Node *p = temp->next;
      while (p != NULL)
        {
          if (p->pointer->priority < n->pointer->priority)
            {

              n->next = p;
              temp->next = n;
              queue->size++;
              return;
            }
          p = p->next;
          temp = temp->next;
        }

      n->next = NULL;
      temp->next = n;
      queue->size++;
    }
}

Instruction
pop (Queue * queue)
{

  Node *head = queue->head;
  Instruction node = head->pointer;

  queue->head = head->next;
  queue->size--;

  free (head);
  return node;
}
这将打印出来 说明2 说明3 说明2 说明3 说明3 等等,无休止地


提前感谢您的帮助

您复制的示例是一个简单队列,而不是优先级队列。假设您忠实地复制了它,并且它可以正常工作,那么问题可能出在您的优先级队列代码中(即您对
推送的修改)。我建议您为它生成一个简单的单元测试,看看它是否失败。如果是,请将整个单元测试作为可编译源代码发布到此处。例如,您需要对
指令
typedef struct
进行一个简单的定义,以便它至少具有一个优先级。好的,我会这样做的,谢谢。我对它进行了单元测试,并意识到是我的其他代码片段导致了seg故障。谢谢你的帮助
Node *n = ready.head;
while (n != NULL)
  {
    puts ("right here now");
    printNode (n->pointer);
    n = n->next;
  }