Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用链表的优先级队列_C_Linked List_Priority Queue - Fatal编程技术网

C 使用链表的优先级队列

C 使用链表的优先级队列,c,linked-list,priority-queue,C,Linked List,Priority Queue,我一直试图在链表的帮助下实现优先级队列。但是,当我在下面的程序中使用add()函数时,我无法将数据添加到列表中。帮点忙就好了 该程序要求我将各种数据排序到单独的队列中..同一队列中的所有元素具有相同的优先级 i、 e:数据:A,优先级:1 数据:B,优先级:2 数据:C,优先级:1 然后,它应按如下方式存储数据: 问题1:A,C 问题2:B 我的程序如下。我想我把作为参数发送给函数add的指针搞乱了 `#include<stdio.h> #include<conio.h>

我一直试图在链表的帮助下实现优先级队列。但是,当我在下面的程序中使用add()函数时,我无法将数据添加到列表中。帮点忙就好了

该程序要求我将各种数据排序到单独的队列中..同一队列中的所有元素具有相同的优先级

i、 e:数据:A,优先级:1 数据:B,优先级:2 数据:C,优先级:1

然后,它应按如下方式存储数据:

问题1:A,C 问题2:B

我的程序如下。我想我把作为参数发送给函数add的指针搞乱了

`#include<stdio.h>
 #include<conio.h>
 struct node{
   char data[3];
   struct node *next;
   };
   void del(struct node *);
   void add(struct node *,struct node **);
   void display(struct node *);
   int main()
   {
       int i;
       struct node *list[5];
       struct node *q;
       int pr,n;
       for(i=0;i<5;i++)
       list[i]=NULL;
       printf("enter the no.of elements");
       scanf("%d",&n);
       for(i=0;i<n;i++)
       {
                       q=(struct node*)malloc(sizeof(struct node));
                       printf("Enter data");
                       scanf("%s",&(q->data));
                       printf("\npriority :");
                       scanf("%d",&pr);
                       pr--;
                       add(q,&list[pr]);
       }
       for(i=0;i<5;i++)
       {
                       display(list[i]);
       }
       for(i=0;i<5;i++)
                       del(list[i]);
                       getch();
       return 0;
       }
       void add(struct node *q,struct node **n)
       {
            if(*n==NULL)
            {
                       *n=q;
                       return;
            }
            while((*n)->next!=NULL)
            *n=(*n)->next;
            (*n)->next=q;
            q->next=NULL;
            return;
       }
       void del(struct node *q)
       {
            if(q==NULL)
            {
                       printf("Queue empty");
                       return;
            }
            while(q->next->next!=NULL)
            q=q->next;
            q->next=NULL;
       }
       void display(struct node *q)
       {
            while(q!=NULL)
            {
                          printf("%s\t",q->data);
                          q=q->next;
            }
            printf("\n");
            return;
       }`
`include
#包括
结构节点{
字符数据[3];
结构节点*下一步;
};
void del(结构节点*);
void add(结构节点*,结构节点**);
无效显示(结构节点*);
int main()
{
int i;
结构节点*列表[5];
结构节点*q;
int-pr,n;
对于(i=0;不精确=q;
q->next=NULL;
返回;
}
void del(结构节点*q)
{
if(q==NULL)
{
printf(“队列为空”);
返回;
}
while(q->next->next!=NULL)
q=q->next;
q->next=NULL;
}
无效显示(结构节点*q)
{
while(q!=NULL)
{
printf(“%s\t”,q->data);
q=q->next;
}
printf(“\n”);
返回;
}`

提前感谢!:)

函数“添加”中的循环如何

你不是这个意思吗

while((*n)->next!=NULL) n=&(*n)->next;

我看到了几个问题:

  • 在main中,“q=(struct node*)malloc(sizeof(struct node))”之后的q->next应该设置为null,否则它可以是任何内容
  • 在del中,您实际上没有删除任何内容(所有由“malloc”分配的资源都应该由“free”释放)
  • 同样在del中,“q->next”可能为空,因此“q->next->next”可能会使程序崩溃

  • 非常感谢!我也意识到了这一点
    while((*n)->next!=NULL) n=&(*n)->next;