void enQ(int* q, int value){
if(q[0] == -1){
q[0] = value;
q[1] = -1;
return;
}
int found = 0;
int last_index;
int count = 0;
while(q[count] != -1){
count++;
}
last_index = count;
int i = 0;
while(q[i] != -1){
if(q[i] < value){
int j = last_index;
while(j != i){
q[j+1]=q[j];
j--;
}
q[i]=value;
found = 1;
break;
}
}
if(found == 0){
q[last_index] = value;
q[last_index+1] = -1;
}
}
int main(int argc, char* argv[])
{
int* q;
q[0] = -1; // initialize queue by making first element of new queue -1...
enQ(q,1);
}
我在 C 中创建了一个简单的优先级队列。问题是当我在 int* 上调用 enQ 时,程序会出现段错误。我不知道为什么。如何修复段错误?