Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
problem of priority queue . i find error on display part.insertion and deletion part is correct .only display part can't run correctly.i can't reduce this problem.please don't give another...

C++
main()
{
    int b=1,item,front[5],rear[5],i,a,n,p,j;
    int m[5][5];
    for(i=0;i<=4;i++)
    {
        front[i]=-1;
        rear[i]=-1;
    }
    printf("enter rhe size of priority queue\n");
    scanf("%d",&n);
    while(b==1)
    {
        printf("enter the option\n");
        printf("1.Insert an item\n");
        printf("2.Delete an item\n");
        printf("3.Display the circular queue\n");
        printf("4.Terminate\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            {
                printf("Enter the item\n");
                scanf("%d",&item);
                printf("Enter the priority of the item\n");
                scanf("%d",&p);
                --p;

                if((front[p]==0 && rear[p]==n-1)||(front[p]==rear[p]+1))
                {
                    printf("Overflow\n");
                    b-=1;
                    break;
                }
                else if(front[p]==-1 && rear[p]==-1)
                {
                    front[p]=0;
                    rear[p]=0;
                }
                else if(rear[p]==n-1)
                rear[p]=0;
                else
                rear[p]=rear[p]+1;
                m[p][rear[p]]=item;
                break;
            }
            case 2:
            {
                if(front[p]==-1 && rear[p]==-1)
                {
                        printf("Underflow\n");
                        b-=1;
                        break;
                }
                else if(front[p]==rear[p])
                {
                    front[p]=-1;
                    rear[p]=-1;
                }
                else if(front[p]==n-1)
                front[p]=0;
                else
                front[p]=rear[p]+1;
                break;
            }
            case 3:
            {
                for(i=0;i<=p;i++)
                {
                    if(rear[i]>=front[i])
                    {
                        for(j=front[i];j<=rear[i];j++)
                        printf("%d\t",m[i][j]);
                    }
                    else
                    {
                        for(j=front[i];j<n;j++)
                        printf("%d\t",m[i][j]);
                        for(j=0;j<=rear[i];j++)
                        printf("%d\t",m[i][j]);
                    }
                }
                printf("\n");
                    break;
            }

            case 4:
            {
                b-=1;
                break;
            }
        }
    }
}
Posted
Updated 30-Aug-12 6:17am
v3
Comments
OriginalGriff 30-Aug-12 6:00am    
There are a few things you need to do here before I will look at this in any depth:
1) Use the "Improve question" widget to edit you question, and move the question into the actual body, so it isn't truncated, and we can work out what you are on about. The subject should be a brief description of the problem, not the whole question.
2) Comment it so we can work out easily what part is what.
3) Indent it properly, so we can tell at a glance what it going on - particularly with your short loops.
4) Change your variable names to reflect the job they do. Single characters may be easy for you to type, but they make maintenance a PITA and looking at your code as an outsider it makes it very hard to work out what is going on, or is supposed to be going on.
5) Describe your problem better: "It doesn't work" is not very helpful, particularly when we know neither what it is doing, nor what it is supposed to do.
ZurdoDev 30-Aug-12 16:24pm    
What's the error or specific line of code causing an issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900