簡體   English   中英

分段錯誤:在 C 中實現優先級隊列

[英]Segmentation fault: Implementing Priority Queues in C

我正在嘗試實現優先級。 較高的變量先驗值意味着我的代碼中的優先級較低。 這里是:

#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");
        }
    }
}

入隊后,我在以下行中發現了分段錯誤: (*pq)->next
在 enqueue() function 中。 我無法理解為什么會這樣。 是因為我采用了struct MinMax_PriorityQueue **類型的參數嗎?
任何幫助表示贊賞。

(*pq)->next=temp; 在隊列中enqueue導致段錯誤,因為*pq在第一次調用時是 NULL。

您不應該注釋掉對 NULL 的檢查。 你需要它..但喜歡

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

順便提一句:

在第一次調用enqueue*pq是 NULL 的原因是main中的這段代碼

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

但是你不應該在main開始時使用malloc 只需這樣做:

struct MinMax_PriorityQueue *pq=NULL;  // Empty queue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM