C 使用链表的优先级队列
我一直试图在链表的帮助下实现优先级队列。但是,当我在下面的程序中使用add()函数时,我无法将数据添加到列表中。帮点忙就好了 该程序要求我将各种数据排序到单独的队列中..同一队列中的所有元素具有相同的优先级 i、 e:数据:A,优先级:1 数据:B,优先级:2 数据:C,优先级:1 然后,它应按如下方式存储数据: 问题1:A,C 问题2:B 我的程序如下。我想我把作为参数发送给函数add的指针搞乱了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>
`#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;
我看到了几个问题:
非常感谢!我也意识到了这一点
while((*n)->next!=NULL) n=&(*n)->next;