Jayajit
Jayajit

Reputation: 25

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
in the 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 **?
Any help is appreciated.

Upvotes: 0

Views: 49

Answers (1)

4386427
4386427

Reputation: 44274

(*pq)->next=temp; in the enqueue function causes segfault as *pq is NULL on the first call.

You shouldn't have commented out the check for 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

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. Simply do:

struct MinMax_PriorityQueue *pq=NULL;  // Empty queue

Upvotes: 1

Related Questions