Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Homework
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...
 
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 30-Aug-12 0:52am
Edited 30-Aug-12 7:17am
(no name)645.1K
v3
Comments
OriginalGriff at 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.
ryanb31 at 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)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100