简体   繁体   English

使用数组进行队列制作

[英]queue making using arrays

I'm trying to write a code that proves a queue like fifo (first in first out). 我正在尝试编写证明像fifo(先进先出)的队列的代码。 I have four characters p(rint),e(nqueue),d(equeue) and q(uit). 我有四个字符p(rint),e(nqueue),d(equeue)和q(uit)。 The problem is when I press da first character must get rid of, but not. 问题是,当我按da时必须删除第一个字符,但不能删除。 When I press d the numbers get double. 当我按d时,数字会翻倍。 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");

}

You should replace the line 您应该更换线

printqueue(arr,count);

with

printqueue(arr,tail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM