Vote count:
0
I've been working on some project with queue and priority queue, but I can't find a solution to a problem with priority queue. When a priority queue gets big it does not return me all the values I ask (to totalRegWait). I cut out the part where I only work with queue.
Any help regarding this question would be greatly appreciated.
Main
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "header.h"
int main()
{
ADT *attend; //Attendants
ADT *vipAttend; //VIP attendants
ADT *regAttend; //Regular attendants
ADT *vipQueue; //VIP queue
ADT *regQueue; //Regular queue
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 priority;
DATA value;
int error;
QDATA qValue;
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(®P, &vipP, ®A, &vipA, ®T, &vipT, &bankT);
sumA=regA+vipA;
create(&attend);
for(i=0;i<sumA;i++){
enq(&attend, 0);
}
create(&vipAttend);
for(i=0;i<vipA;i++){
enq(&vipAttend, 0);
}
create(®Attend);
for(i=0;i<regA;i++){
enq(®Attend, 0);
}
create(®Queue);
create(&vipQueue);
createEmpty(&pQueue);
srand(time(NULL));
if(regA<=0 || vipA<=0){
printf("Error. No attendant's are given or wrong number. Terminating...\n");
return 0;
}
if(regP<0 || vipP<0){
printf("Error. Client probabilities are wrong. Terminating...\n");
return 0;
}
if(regP==0 && vipP==0){
printf("Error. Client probabilities are wrong. Terminating...\n");
return 0;
}
if(regP>100 || vipP>100){
printf("Error. Client probabilities are wrong. Terminating...\n");
return 0;
}
for(i=0;i<bankT;i++){
if(rand()%100<regP){
regCount++;
add(&pQueue, 0, 0);
enq(®Queue, 0);
}
if(rand()%100<vipP){
vipCount++;
add(&pQueue, 1, 0);
enq(&vipQueue, 0);
}
//1st method
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
}
}
}}
//Calculating overtime
temp1=attend;
while(temp1!=NULL){
overtime=overtime+(temp1->var);
temp1=temp1->next;
}
temp2=pQueue;
while(temp2!=NULL){
if((temp2->priority)==1){
overtime=overtime+vipT; //Waiting VIP client's time is added to attender's
}
else{
overtime=overtime+regT; //Waiting regular client's time is added to attender's overtime
}
temp2=temp2->next; //Next client
}
temp1=vipAttend;
while(temp1!=NULL){
overtime2=overtime2+(temp1->var);
temp1=temp1->next;
}
temp1=vipQueue;
while(temp1!=NULL){
overtime2=overtime2+vipT;
temp1=temp1->next;
}
temp1=regAttend;
while(temp1!=NULL){
overtime2=overtime2+(temp1->var);
temp1=temp1->next;
}
temp1=regQueue;
while(temp1!=NULL){
overtime2=overtime2+regT;
temp1=temp1->next;
}
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);
printf("Second method:\n");
printResults(attWait2, overtime2, totalRegWait2, regCount, vipWait2, vipCount);
return 0;
}
Functions
#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 destroy(LINK **head) {
LINK *current;
LINK *next;
current=(*head);
while(current->next){
next=current->next;
free(current);
current=next;
}
(*head)=NULL;
}
void print(LINK *head){ //Working
LINK *curr=head;
while(curr!=NULL) {
printf("%d >> ",curr->value);
curr = curr->next;
}
printf("NULL\n\n");
}
//-------------------------------
void create(ADT **front){ //Working
*front = NULL;
}
int queuesize(ADT **front){ //Working
ADT *temp = *front;
int x = 0;
while(temp != NULL)
{
temp = temp->next;
x++;
}
return x;
}
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 deq(ADT **front){ //Should be good
ADT *temp = *front;
if (temp->next != NULL){
temp = temp->next;
free(*front);
*front = temp;
}
else{
free(*front);
*front = NULL;
}
}
DATA frontelement(ADT *front){ //Working
return front->var;
}
int empty(ADT *front){ //Working
if (front == NULL)
return 1;
else
return 0;
}
void delete(ADT **front){ //Should be good
ADT *temp;
if(*front != NULL)
{
while(*front != NULL)
{
temp = *front;
*front = (*front)->next;
free(temp);
}
free(*front);
}
}
//--------------------------------
void getArguments(int *regP, int *vipP, int *regA, int *vipA, int *regT, int *vipT, int *bankT){ //Working
FILE *fp;
fp=fopen("duom.txt", "r");
fscanf(fp, "%d", &(*regP));
fscanf(fp, "%d", &(*vipP));
fscanf(fp, "%d", &(*regA));
fscanf(fp, "%d", &(*vipA));
fscanf(fp, "%d", &(*regT));
fscanf(fp, "%d", &(*vipT));
fscanf(fp, "%d", &(*bankT));
fclose(fp);
}
void printResults(int attWait, int overtime, int totalRegWait, int regCount, int vipWait, int vipCount){ //Working
printf("Sum of attenders waiting time - %d\n", attWait);
printf("Doubled attenders overtime - %d\n", 2*overtime);
if(regCount==0){
printf("There were no regular clients.\n");
}
else{
printf("Tripled average of regular clients waiting - %.3f\n", (float)3*totalRegWait/regCount);
}
if(vipCount==0){
printf("There were no VIP clients.\n");
}
else{
printf("Times ten to maximum VIP client waiting time - %d\n", 10*vipWait);
}
}
Header
#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;
QDATA frontelement(ADT *front);
void enq(ADT **front, QDATA variable);
void deq(ADT **front);
int empty(ADT *front);
void create(ADT **front);
int queuesize(ADT **front);
void delete(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);
void print(LINK *head);
int checkIfEmpty(LINK *head);
void destroy(LINK **head);
//END OF PRIOR. QUEUE FUNCTIONS
void printResults(int attWait, int overtime, int totalRegWait, int regCount, int vipWait, int vipCount);
void getArguments(int *regP, int *vipP, int *regA, int *vipA, int *regT, int *vipT, int *bankT);
#endif
asked 1 min ago
Malfunctioning priority queue
Aucun commentaire:
Enregistrer un commentaire