循环队列模拟

时间:2016-10-10 17:41:05

标签: c queue

我已经制作了循环队列并通过它,我已经进行了一个模拟,它将从数组中取出元素并从中取出元素。在输出中它会告诉我们时间,客户没有。和时间持续。 但在我的输出客户没有。再次重复或它包含一些垃圾值,这是截图。 enter image description here

这是我的代码。

#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<stdbool.h>
//CQUEUE

struct CQueueADT
{
    int head, tail, capacity;
    int data[50];
};


void init(struct CQueueADT *S)
{
    S->capacity=50;
    S->head=0;
    S->tail=0;
}


bool isEmpty(struct CQueueADT *S)
{
    if (S->head==S->tail)
        return true;

    return false;
}

bool isFull(struct CQueueADT *S)
{
    if(S->head==(S->tail+1)%S->capacity)
        return true;
    return false;
}

bool Enque(struct CQueueADT *S, int num)
{
    if(isFull(S))
        return false;
    else
    {
        S->data[S->tail]= num;
        S->tail= (S->tail+1)%(S->capacity);
    }
}
//Dequeue

bool Deque(struct CQueueADT *S, int *num)
{
    if(isFull(S))
        return false;
    else{
        *num = S->data[S->head];
        S->head= (S->head+1)%(S->capacity);
        return true;
    }
}


void SimulateCustomersLine(int min){
    struct CQueueADT CQ;
    init(&CQ);
    int minutes;
    int k;
    int customers = 0;
    int last_time = 0;
    int wait = 0;
    int position = 1;
    for(minutes =0; minutes<min; minutes++){
        printf("%d minutes\n", minutes );
        if(isEmpty(&CQ) == false){
            printf("Dequeuing customer %d\n", position);
            Deque(&CQ, &position);
            wait = customers/100;
            printf("customer %d stayed %d minutes and lasted %d minutes\n", position, wait, last_time);

        }
    // For random number
    k = rand()%4;
    printf("Random number is %d\n", k);
    if(k == 1){
        (customers ++);
        //last_time = (customers % 100);
        printf("Enqueuing customer %d at %d minutes\n", customers,minutes);
        Enque(&CQ, customers);
    }

    else if(k  == 2){
        (customers ++);
        last_time = ( customers % 100);
        printf("Enqueuing customer %d at %d minutes\n", customers,minutes);
        Enque(&CQ, customers);
        (customers ++);
        last_time = (customers % 100);
        printf("Enqueuing customer %d at %d minutes again\n", customers,minutes);
        Enque(&CQ, customers);
    }

    else{
        printf("No customer added at %d minute\n\n", minutes);
    }
    last_time = customers;
    customers = customers + 1;
}


}

int main(){
 int mins;
 struct CQueueADT CQ;
 init(&CQ);
 printf("Enter how many minutes of data you want? ");
 scanf("%d", &mins);
 printf("\n\n");
 SimulateCustomersLine(mins);

return 0;
}

0 个答案:

没有答案