void RandQueueInital(int);
void RandQueuePush(int);
int RandQueuePop();
void RandQueueFree();
//随机队列:就是顺序插入,随机删除任意一项
#include <string.h>
#include <math.h>
#include <time.h>
int *randQueue;
int randCount;
int randTotal;
void RandQueueInital(int aMax)
{
randQueue = (int*)malloc(sizeof(int)*aMax);
if (!randQueue)
{
printf("");
return;
}
memset(randQueue, 0, sizeof(int)*aMax);
randCount = 0;
randTotal = aMax;
}
void RandQueuePush(int aElement)
{
if (randCount >= randTotal)
{
randQueue = realloc(randQueue, (randCount+randTotal/2)*sizeof(int));
randTotal = randCount+randTotal/2;
}
randQueue[randCount++] = aElement;
}
int RandQueuePop()
{
if (--randCount < 0)
{
return -1;
}
int index = -1;
srand((unsigned)time(NULL));
do
{
index = rand()%randTotal;
} while (!randQueue[index]);
int temp = randQueue[index];
randQueue[index] = 0;
return temp;
}
void RandQueueFree()
{
if (randQueue)
{
free(randQueue);
}
randTotal = 0;
randCount = 0;
}