GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

can someone suggest why i am getting runtime error in my code ?

#include #include #define MAX 5 struct data { char job[MAX]; int pri; int order; }; struct que { struct data d[MAX]; int rear; int front; }; void initque(struct que *); void add(struct que *,struct data); struct data dele(struct que *); void main() { struct que q; struct data dt,temp; int i,j=0; initque(&q); printf("enter job description n its pri\n"); printf("Job pri\n"); for(i=0;ifront=pq->rear=-1; for(i=0;id[i].job,'\0'); pq->d[i].pri=pq->d[i].order=0; } } void add(struct que *pq,struct data dt) { struct data temp; int i,j; if(pq->rear==MAX-1) { printf("\n Quq is full"); return; } pq->rear++; pq->d[pq->rear]=dt; if(pq->front==-1) pq->front=0; for(i=pq->front;irear;i++) { for(j=i+1;j<=pq->rear;j++) { if(pq->d[i].pri>pq->d[j].pri) { temp=pq->d[i]; pq->d[i]=pq->d[j]; pq->d[j]=temp; } else { if((pq->d[i].pri==pq->d[j].pri) && (pq->d[i].order>pq->d[j].order)) { temp=pq->d[i]; pq->d[i]=pq->d[j]; pq->d[j]=temp; } } } } } struct data dele(struct que *pq) { struct data t,f; strcpy(t.job,""); t.pri=0; t.order=0; if(pq->front==-1) { printf("\n quq is empty"); return t; } f=pq->d[pq->front]; pq->d[pq->front]=t; if(pq->front==pq->rear) pq->front=pq->rear=-1; else pq->front++; return f; }

asked Aug 18, 2016 by qdg804

plz give the question

and use code Snippet for posting the code

 

#include<stdio.h>
#include<conio.h>
#include<string.h>

#define MAX 5
struct data
{
    char job[MAX];
    int pri;
    int order;
};

struct que
{
    struct data d[MAX];
    int rear;
    int front;
};

void initque(struct que *);
void add(struct que *,struct data);
struct data dele(struct que *);

void main()
{
    struct que q;
    struct data dt,temp;
    int i,j=0;

    initque(&q);

    printf("enter job description n its pri\n");
    printf("Job pri\n");

    for(i=0;i<MAX;i++)
    {
        scanf("%s %d",&dt.job,&dt.pri);
        dt.order=j++;
        add(&q,dt);
    }
    printf("\n");

    for(i=0;i<MAX;i++)
    {
        temp=dele(&q);
        printf("%s\t%d\n",temp.job,temp.pri);
    }
    printf("\n");
}


void initque(struct que *pq)
{
    int i;
    pq->front=pq->rear=-1;
    for(i=0;i<MAX;i++)
    {
        strcpy(pq->d[i].job,'\0');
        pq->d[i].pri=pq->d[i].order=0;

    }
}


void add(struct que *pq,struct data dt)
{
    struct data temp;
    int i,j;

    if(pq->rear==MAX-1)
    {

        printf("\n Quq is full");
        return;
    }

    pq->rear++;
    pq->d[pq->rear]=dt;

    if(pq->front==-1)
        pq->front=0;

    for(i=pq->front;i<pq->rear;i++)
    {

        for(j=i+1;j<=pq->rear;j++)
        {

            if(pq->d[i].pri>pq->d[j].pri)
            {

                temp=pq->d[i];
                pq->d[i]=pq->d[j];
                pq->d[j]=temp;
            }
            else
            {
                if((pq->d[i].pri==pq->d[j].pri) && (pq->d[i].order>pq->d[j].order))
                {
                    temp=pq->d[i];
                    pq->d[i]=pq->d[j];
                    pq->d[j]=temp;

                }
            }
        }
    }
}


struct data dele(struct que *pq)
{
 struct data t,f;
 strcpy(t.job,"");
 t.pri=0;
 t.order=0;

 if(pq->front==-1)
 {
     printf("\n quq is empty");

     return t;

 }

 f=pq->d[pq->front];
 pq->d[pq->front]=t;
if(pq->front==pq->rear)
        pq->front=pq->rear=-1;
else
    pq->front++;

    return f;
}

here is the code snippet.....the question was insertion and deletion of element in priority queue

1 Answer

make sure u didn't access the elements in out of memeory

answered Aug 19, 2016 by anonymous
...