我有一个作业要求我用 c 语言实现一个带有二进制堆的优先级队列。我有一个问题,因为基本上,我们得到的输入数据如下所示:
7 - 出现次数
3 4 0 5 8 0 0 - 出现,如果数字为 0,则意味着具有最高优先级的数字被删除(或者如果优先级相同,则存在时间最长的数字 - 我猜只是基本优先级队列)
但是,重要的是输出需要是作业的数量,而不是优先级,因此在上面的示例中它将是 2, 4, 3
我是一名初学者,所以我能想到的唯一方法是为每个作业使用结构,其中有两个整数 - 一个用于作业数量,一个用于其优先级
我遇到的问题是输入,如果我得到的只是数字,那么我怎样才能使每个结构可识别并能够稍后将其排序到堆中?我想也许创建临时结构,然后将它们添加到列表中,但是如何将值传递给临时结构?
这是我现在拥有的代码,我还没有完成所有功能,因为首先我希望能够正确存储和访问这些值
#include <stdio.h>
struct zadanie
{
int nr;
int priorytet;
};
int main()
{
int events;
scanf("%d", &events);
//struct zadanie heap[events];
int n = 1;
for (int i=0; i<events; i++)
{
int x;
scanf("%d", &x);
if (x != 0)
{
struct zadanie tmp;
&tmp.nr = n;
&tmp.priorytet = x;
n += 1;
}
else
{
}
}
}
我尝试使用这些问题作为指导: 使用 scanf 处理具有结构的多个数据 如何使用 scanf 将值分配给动态创建数组的 Struct 类型变量的成员 但我不知道如何让答案对我有用
*编辑: 我得到了用于插入值的代码,现在我在将数组内部的结构传递给 heapify 函数时遇到问题 我知道它可能与指针有关,但我玩了一下它,我得到的只是一个“请求成员‘priorytet’不是结构或联合的东西”错误代码
到目前为止我的代码:
#include <stdio.h>
int size = 0;
struct zadanie
{
int nr;
int priorytet;
};
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void kopcenie(int heap[], int size, int i)
{
if (size == 1){
return;
}
else
{
int max = i;
int l = 2*i;
int p = 2*i+1;
if (l < size && heap[l].priorytet > heap[max].priorytet)
max = l;
if (p < size && heap[p].priorytet > heap[max].priorytet)
max = p;
if (max != i)
{
swap(&heap[i], &heap[max]);
kopcenie(heap, size, max);
}
}
}
int main()
{
int events;
scanf("%d", &events);
struct zadanie heap[events];
int n = 1;
for (int i=0; i<events; i++)
{
int x;
scanf("%d", &x);
if (x != 0)
{
struct zadanie tmp;
tmp.nr = n;
tmp.priorytet = x;
heap[n] = tmp;
n += 1;
}
}
}
**另一个编辑 - 我让它工作了,在 C 中传递结构数组对我帮助很大
我得到了一些东西,但答案在某些数据上不正确,例如对于 10 1 1 1 1 2 0 0 0 0 0 答案应该是 5 1 2 3 4 但对我来说它是 1 5 2 3 4,知道可能出了什么问题吗? 这是我完成的代码:
#include <stdio.h>
struct zadanie
{
int nr;
int priorytet;
};
void swap(struct zadanie *a, struct zadanie *b)
{
struct zadanie temp = *b;
*b = *a;
*a = temp;
}
void kopcenie(struct zadanie heap[], int size, int i)
{
if (size == 1){
return;
}
else
{
int max = i;
int l = 2*i;
int p = 2*i+1;
if ((l < size) && (heap[l].priorytet > heap[max].priorytet ||
(heap[l].priorytet == heap[max].priorytet && heap[l].nr < heap[max].nr)))
max = l;
if ((p < size) && (heap[p].priorytet > heap[max].priorytet) ||
(heap[p].priorytet == heap[max].priorytet && heap[p].nr < heap[max].nr)))
max = p;
if (max != i)
{
swap(&heap[i], &heap[max]);
kopcenie(heap, size, max);
}
}
}
void wstaw(struct zadanie heap[], int *size, struct zadanie i)
{
if (*size == 0) {
heap[0] = i;
*size = 1;
}
else
{
heap[*size] = i;
*size += 1;
for (int i = *size / 2 - 1; i >= 0; i--)
{
kopcenie(heap, *size, i);
}
}
}
void usun(struct zadanie heap[], int *size) {
if (*size == 0)
return;
printf("%d\n", heap[0].nr);
swap(&heap[0], &heap[*size - 1]);
*size -= 1;
for (int i = *size / 2 - 1; i >= 0; i--) {
kopcenie(heap, *size, i);
}
}
int main()
{
int size = 0;
int events;
scanf("%d", &events);
struct zadanie heap[events];
int n = 1;
for (int i=0; i<events; i++)
{
int x;
scanf("%d", &x);
if (x != 0)
{
struct zadanie tmp;
tmp.nr = n;
tmp.priorytet = x;
wstaw(heap, &size, tmp);
n+=1;
}
else
{
usun(heap, &size);
}
}
}