简体   繁体   English

分段错误:在 C 中实现优先级队列

[英]Segmentation fault: Implementing Priority Queues in C

I'm trying to implement priority.我正在尝试实现优先级。 A higher value of variable prior implies a lower priority in my code.较高的变量先验值意味着我的代码中的优先级较低。 Here it is:这里是:

#include <stdio.h>
#include <stdlib.h>

struct MinMax_PriorityQueue{
    int ele, prior;
    struct MinMax_PriorityQueue *next;
};

int isEmpty(struct MinMax_PriorityQueue **pq){
    return ((*pq)==NULL);
}

int checkPriority(int p){
    return(p>0);
}

void enqueue(struct MinMax_PriorityQueue **pq, int x, int p){
    struct MinMax_PriorityQueue *temp=malloc(sizeof(*temp));
    //struct MinMax_PriorityQueue *temp1=*pq;
    if(!checkPriority(p)){
        printf("Priority should be greater than 0");
        return;
    }
    temp->ele=x;
    temp->prior=p;
    temp->next=NULL;
    /*if(isEmpty(pq)){
        *pq=temp;
    }
    while(temp1->next!=NULL){
        temp1=temp1->next;
    }*/
    (*pq)->next=temp;
    printf("The item %d with priority %d has been enqueued into the priority queue\n", x, p);
}

int maxPriority(struct MinMax_PriorityQueue **pq){
    struct MinMax_PriorityQueue *temp=malloc(sizeof(*temp));
    temp=*pq;
    int maxp=temp->prior;
    while(temp!=NULL){
        if(temp->prior<=maxp)
            maxp=temp->prior;
        temp=temp->next;
    }
    return maxp;
}

void dequeue(struct MinMax_PriorityQueue **pq){
    if(isEmpty(pq)){
        printf("The priority queue is empty. No more elements can be removed!\n");
    }
    int maxp=maxPriority(pq);
    struct MinMax_PriorityQueue *temp=*pq;
    while(temp!=NULL){
        if(temp->prior==maxp){
            printf("The item %d with priority %d has been dequeued from the priority queue\n", temp->ele, temp->prior);
            free(temp);
            break;
        }
        temp=temp->next;
    }
}

void minSearch(struct MinMax_PriorityQueue **pq){
    struct MinMax_PriorityQueue *temp=malloc(sizeof(*temp));
    temp=*pq;
    int minp=0;
    while(temp!=NULL){
        if(temp->prior>=minp)
            minp=temp->prior;
        temp=temp->next;
    }
    temp=*pq;
    while(temp!=NULL){
        if(temp->prior==minp){
            printf("The element %d has minimum priority\n", temp->ele);
        }
        temp=temp->next;
    }
}

void maxSearch(struct MinMax_PriorityQueue **pq){
    int maxp=maxPriority(pq);
    struct MinMax_PriorityQueue *temp=*pq;
    while(temp!=NULL){
        if(temp->prior==maxp){
            printf("The element %d has maximum priority\n", temp->ele);
        }
        temp=temp->next;
    }
}

void display(struct MinMax_PriorityQueue *pq){
    struct MinMax_PriorityQueue *temp=pq;
    printf("The contents of the priority queue are:\n");
    if(isEmpty(&temp)){
        printf("Nothing to be shown, the priority queue is empty.\n");
        return;
    }
    for(int i=0;temp!=NULL;temp=temp->next){
        if(i){
            printf(" ------ \n");
        }
        printf("|  %d  |\n", temp->ele);
        i=1;
    }
}

int main()
{
    int choice, element, priority;
    printf("LET'S START WITH AN EMPTY QUEUE\n\n");
    struct MinMax_PriorityQueue *pq=malloc(sizeof(*pq));
    pq=NULL;
    while(1){
        printf("\nMENU\n");
        printf("----\n");
        printf("\t1. Enqueue\n");
        printf("\t2. Dequeue\n");
        printf("\t3. Display queue\n");
        printf("\t4. Search minimum priority\n");
        printf("\t5. Search maximum priority\n");
        printf("\t6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice){
            case 1: printf("Enter the element to be enqueued: ");
                    scanf("%d", &element);
                    printf("Enter its priority: ");
                    scanf("%d", &priority);
                    enqueue(&pq, element, priority);
                    break;
            case 2: dequeue(&pq);
                    break;
            case 3: display(pq);
                    break;
            case 4: minSearch(&pq);
                    break;
            case 5: maxSearch(&pq);
                    break;
            case 6: printf("Program terminated successfully!\n");
                    return 0;
            default: printf("Invalid input");
        }
    }
}

Upon enqueuing, I find a segmentation fault in the line: (*pq)->next入队后,我在以下行中发现了分段错误: (*pq)->next
in the enqueue() function.在 enqueue() function 中。 I can't wrap my head around why that is happening.我无法理解为什么会这样。 Is it because I took an argument of type struct MinMax_PriorityQueue ** ?是因为我采用了struct MinMax_PriorityQueue **类型的参数吗?
Any help is appreciated.任何帮助表示赞赏。

(*pq)->next=temp; in the enqueue function causes segfault as *pq is NULL on the first call.在队列中enqueue导致段错误,因为*pq在第一次调用时是 NULL。

You shouldn't have commented out the check for NULL.您不应该注释掉对 NULL 的检查。 You need it.. but like你需要它..但喜欢

if(isEmpty(pq)){
    *pq=temp;
    return;      // Add this
}

BTW:顺便提一句:

The reason the *pq is NULL on the first call of enqueue is this code in main在第一次调用enqueue*pq是 NULL 的原因是main中的这段代码

struct MinMax_PriorityQueue *pq=malloc(sizeof(*pq));
pq=NULL;    // You set pq back to NULL  (and leaks memory)

But you shouldn't do the malloc in main to start with.但是你不应该在main开始时使用malloc Simply do:只需这样做:

struct MinMax_PriorityQueue *pq=NULL;  // Empty queue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM