samedi 30 janvier 2016

can't under stand why the queue function program works at the first time but don't work like expected after that

this is my code


#include <stdio.h>
#include <stdlib.h>

int Enqueue(int[],int,int*,int);
int Dequeue(int[],int,int*,int);
int show(int[],int*);

int main (void)
{
    int q[]={1,2,3,4,5,6,7,8,9,10};
    int n = sizeof(q)/sizeof(int);
    int numelem = n;
    int l=0;
    int e=0;
    int d=0;
    while(l!=4)
    {
        printf("°°°°menu items°°°°\n");
        printf("1-->enqueue\n");
        printf("2-->dequeue\n");
        printf("3-->show\n");
        printf("4-->exit\n");
        fflush (stdin);
        scanf("%d",&l);
        if(l==1)
        {
            printf("plz enter the element you want to add --> ");
            scanf("%d",&e);
            Enqueue(q,e,&numelem,n);
            //printf("Q");
        }    
        else if(l==2)
        {
            Dequeue(q,d,&numelem,n);
        }
        else if(l==3)
        {
            show(q,&numelem);
        }
        else if(l==4)
        {
            break;
        }
        else
        {
            printf("plz enter a number from the menu!!!!!\n");
        }
    }
    printf("bye bye\n");
}
int Enqueue(int q[],int e,int *numelem,int n)
{
    int inserted=0;
    n++;
    if(*numelem<n)
    {
        inserted=1;
        q[*numelem]=e;
        *numelem+=1;
    }

    return inserted;

}
int Dequeue(int q[],int d,int *numelem,int n)
{
    int deleted=0;
    if(*numelem==n)
    {
        deleted=1;
        d=q[0];
        *numelem-=1;
        n--;
    }
    for (int i=0;i<=n;i++)
    {
        q[i-1]=q[i];
    }
    printf("the deleted item is --> %d\n",d);
    return deleted;
}
int show(int q[],int *numelem)
{
    for (int i=0;i<*numelem;i++)
    {
        printf("%d,",q[i]);
    }
    printf("\n");
    return *numelem;
}


there are no compiler error


and when you enter Equeue of Dequeue for the first time it works


but if you try again in the same time the result is unexpected


I am a student who trying to do his homework so please don't be mean in the comments and thanks any way


plz help me

Aucun commentaire:

Enregistrer un commentaire