0

I'm trying to write a code that proves a queue like fifo (first in first out). I have four characters p(rint),e(nqueue),d(equeue) and q(uit). The problem is when I press d a first character must get rid of, but not. When I press d the numbers get double. Where is my wrong ? Thank you for all appreciated answers. Besides, I think my wrong is in the dequeue function.

Example input:
e 2 3 9 8 7
p
2 3 9 8 7
d
p
3 9 8 7
d
p
9 8 7

#include <stdio.h>

void enqueue(int queue[], int newnum, int *tail_p, int maxsize);
void deque(int queue[], int *tail_p, int *elem);
void printqueue(int queue[],int count);

int main(){

    int arr[10];
    int num;
    int tail=0;
    char ch;
    int i;
    int count=0;
    int elem;

    do{
    scanf("%c",&ch);
    if(ch=='e')
    for(i=0;i<10 && (ch!='\n');i++){
        //arr[i]=scanf("%d",&num);
        scanf("%d",&num);
        scanf("%c",&ch);
        enqueue(arr,num,&tail,10);
        count++;
    }
    if(ch=='d'){

        deque(arr,&tail,&elem);

    }
    if(ch=='p')
    printqueue(arr,count);
    }
    while(ch!='q');
    return 0;
}
void printqueue(int arr[],int size){

    int i;

    for(i=0;i<size;i++)
    printf("%d  ",arr[i]);

}

void enqueue(int queue[], int newnum, int *tail_p, int maxsize){

    if(maxsize>*tail_p){
        queue[*tail_p]=newnum;
        *tail_p+=1;
    }
    else
        printf("error! maxsize\n");
}
void deque(int queue[], int *tail_p, int *elem_p){

    int i;

    if(0<*tail_p){

        *elem_p=queue[0];
        for(i=1;i<*tail_p;i++){
            queue[i-1]=queue[i];
            *tail_p-=1;}
    }
    else
        printf("error! tail is greater than 0\n");

}
0

1 Answer 1

2

You should replace the line

printqueue(arr,count);

with

printqueue(arr,tail);
Sign up to request clarification or add additional context in comments.

5 Comments

did you try the code like your saying ? I tried my numbers get double. Before your saying i did it. There is another wrong if it is wrong.
nope. but count is not the value you are decrementing in deque. tail is the value you are incrementing in enqueue and decrementing in dequeue.
okey. when pressed character d counter--; there is another problem
get rid of count. you don't need it. just use tail.
+1, @Soner, if you want us to test your program, assign some random values instead of scanf, nobody wants to type 20 values by hand ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.