故障优先级队列

时间:2015-04-19 18:15:29

标签: c queue priority-queue

我一直在研究一些带队列和优先级队列的项目,但我找不到优先级队列问题的解决方案。当优先级队列变大时,它不会返回我所要求的所有值(对于totalRegWait)。我删除了我只使用队列的部分。

非常感谢有关此问题的任何帮助。

主要

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

#include "header.h"

int main()
{

    ADT *attend;       //Attendants
    LINK *pQueue;      //Priority queue

    ADT *temp1;     //Temporary
    LINK *temp2;    //Temporary

    int regP;     //Regular probability
    int vipP;     //VIP probability

    int regA;     //Regular attendants
    int vipA;     //VIP attendants
    int sumA;     //Combined attendants

    int regT;     //Regular time
    int vipT;     //VIP time
    int bankT;    //Bank working time

    int i;

    int attWait=0, attWait2=0;              //Attendants wait time
    int overtime=0, overtime2=0;            //Attendants overtime
    int totalRegWait=0, totalRegWait2=0;    //Total regular client waiting time
    int regCount=0;                         //Regular person count
    int vipWait=0, vipWait2=0;              //Maximum VIP waiting time
    int vipCount=0;                         //Amount of VIP

    getArguments(&regP, &vipP, &regA, &vipA, &regT, &vipT, &bankT);
    sumA=regA+vipA;

    create(&attend);
    for(i=0;i<sumA;i++){
        enq(&attend, 0);
    }

    createEmpty(&pQueue);

    srand(time(NULL));

    for(i=0;i<bankT;i++){
        if(rand()%100<regP){
            regCount++;
            add(&pQueue, 0, 0);
        }
        if(rand()%100<vipP){
            vipCount++;
            add(&pQueue, 1, 0);
        }
//1st method
    first(&attend, &pQueue, vipT, regT, &vipWait, &totalRegWait, &attWait);
}
        printf("%d %d\n", regCount, vipCount);
        printf("%d %d\n", totalRegWait, totalRegWait2);
    //Printing results
        printf("First method:\n");
        printResults(attWait, overtime, totalRegWait, regCount, vipWait, vipCount);
        return 0;
    }

功能

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

#include "header.h"

void createEmpty(LINK **head) {
    (*head)=NULL;
}

void add(LINK **head, int p, DATA v){
    LINK *curr=(*head);
    LINK *beforeCurr=(*head);
    LINK *newElement;
    newElement=(LINK*)malloc(sizeof(LINK));
    if(newElement==NULL){
        printf("Error. Not enough memory to allocate.\n");
    }
    newElement->priority=p;
    newElement->value=v;

    newElement->next=NULL;
    if((*head)==NULL){
        (*head)=newElement;
    }
    else if(p<(*head)->priority){
        newElement->next=curr;
        (*head)=newElement;
    }

    else{
        while(p>(curr->priority) && curr->next!=NULL){
            beforeCurr=curr;
            curr=curr->next;
        }

        if(curr->next==NULL && p>(curr->priority))
            (curr->next)=newElement;

        else if(curr==(*head)){
            newElement->next=curr;
            (*head)=newElement;
        }
        else{
            beforeCurr->next=newElement;
            newElement->next=curr;
        }
    }
}

void pop(LINK **head, int *p, DATA *v, int *error){
    LINK *curr=(*head);
    LINK *currB=(*head);
    if(*head==NULL){
        (*error)=1;
    }
    else if(curr->next==NULL){
        (*p)=curr->priority;
        (*v)=curr->value;
        free(curr);
        (*head)=NULL;
    }
    else{
        while(curr->next!=NULL) {
            currB=curr;
            curr=curr->next;
        }
        (*p)=curr->priority;
        (*v)=curr->value;
        free(curr);
        currB->next=NULL;
    }
}

int checkIfEmpty(LINK *head){ //Working
    if(head==NULL)
        return 1;
    else
        return 0;
}

//-------------------------------
void create(ADT **front){ //Working
    *front = NULL;
}

void enq(ADT **front, QDATA variable){ //Should be good
    ADT *temp;
    ADT *rear = (ADT *)malloc(sizeof(ADT));
    if(rear==NULL){
        printf("Error. Not enough memory to allocate.\n");
    }
    if(*front == NULL)
    {
        *front = (ADT *)malloc(sizeof(ADT));
        if(*front==NULL){
            printf("Error. Not enough memory to allocate.\n");
        }
        (*front)->var = variable;
        (*front)->next = NULL;
    }
    else
    {
        temp = *front;
        while(temp->next != NULL){
            temp = temp->next;
        }
        rear->var = variable;
        rear->next = NULL;
        temp->next = rear;
    }
}
//--------------------------------

void first(ADT **attend, LINK **pQueue, int vipT, int regT, int *vipWait, int *totalRegWait, int *attWait){
    ADT *temp1;
    LINK *temp2;
    int priority;
    DATA value;
    int error=0;

    if(checkIfEmpty(*pQueue)==1){ //If client's queue is empty, time is changed to attenders and increased att. waiting time
        temp1=(*attend);
        while(temp1 != NULL){
            if((temp1->var)>0){
                (temp1->var)--;
            }
            else{
                (*attWait)++;
            }
            temp1 = temp1->next;
        }
    }
    else{                                   //If there are clients, look for not occupied attenders
        temp1=(*attend);
        temp2=(*pQueue);
        while(temp1 != NULL){                //While loop which goes through all the attenders
            if((temp1->var)>0){              //If attender is busy
                (temp1->var)--;              //Yes, then it's value is decreased by 1
            }
            else{                            //If attender value is not busy
                if(checkIfEmpty(*pQueue)==1){ //Check if priority line is empty
                    (*attWait)++;               //If yes, waiting time for attenders is added
                }
                else{                        //If priority line is not empty
                    pop(&(*pQueue), &priority, &value, &error); //Client is removed from queue
                    printf("%d %d\n", priority, value);
                    if(error==1){            //If pop fails it shows an error and quit
                        printf("Error\n");
                        return 0;
                    }
                    else{                    //If pop does not fail
                        if(priority==1){     //Check if client is VIP
                            temp1->var=vipT; //If yes, then attender is given VIP time to his value
                            if((*vipWait)<value){ //If VIP client's waiting time is higher than earlier value
                                (*vipWait)=value; //Earlier VIP waiting time is changed
                            }
                        }
                        if(priority==0){                //If client is regular
                            temp1->var=regT;
                            (*totalRegWait)=(*totalRegWait)+value; //Removed client's waiting value is added to a total value
                            (temp1->var)--;
                        }
                        (temp1->var)--;  //Attender time is lowered by 1 (end of minute)
                    }
                }
            }
            temp1 = temp1->next;        //Going for another attender
        }
        if(checkIfEmpty(*pQueue)==0){    //Check if priority queue is empty
            while(temp2!=NULL) {
                (temp2->value)++;         //Waiting time to a client is added
                temp2 = temp2->next;    //Next client
            }
        }
    }
}

标题

    #ifndef HEADER_H_
    #define HEADER_H_

    typedef int DATA;
    typedef int QDATA;

    typedef struct priorityQueue{
        int priority;
        DATA value;
        struct priorityQueue *next;
    }LINK;

    typedef struct list
    {
        QDATA var;
        struct list *next;
    }ADT;

    void enq(ADT **front, QDATA variable);
    void create(ADT **front);

//PRIORITY QUEUE FUNCTIONS
    void createEmpty(LINK **head);
    void add(LINK **head, int p, DATA v);
    void pop(LINK **head, int *p, DATA *v, int *error);
    int checkIfEmpty(LINK *head);
//END OF PRIOR. QUEUE FUNCTIONS

    void first(ADT **attend, LINK **pQueue, int vipT, int regT, int *vipWait, int *totalRegWait, int *attWait);
    #endif

0 个答案:

没有答案