先看效果图:
话不多说,直接上代码!!!
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<time.h>
#define MAX 10
typedef struct Data {
const char* name;
int Weight;//权值
}*DATA,DaTA;
typedef struct Queue {
Data* pMemory;
int CurSize;//万金油参数
}*LPQUEUE,QUEUE;
LPQUEUE CreateQueue() {
LPQUEUE queue = (LPQUEUE)malloc(sizeof(QUEUE));
assert(queue);
queue->pMemory = (DATA)malloc(sizeof(DaTA) * MAX);
assert(queue->pMemory);
queue->CurSize = 0;
return queue;
}
void Push(LPQUEUE queue, DaTA data) {
if (queue->CurSize == MAX)
return;
queue->pMemory[queue->CurSize++] = data;
}
void Pop(LPQUEUE queue, DATA data) {
if (queue->CurSize == 0)
return;
DaTA Max = queue->pMemory[0];
int pos = 0;
for (int i = 0; i < queue->CurSize; i++)
if (queue->pMemory[i].Weight > Max.Weight) {
Max = queue->pMemory[i];
pos = i;
}
*data = Max;
for (int i = pos+1; i < queue->CurSize; i++)
queue->pMemory[i - 1] = queue->pMemory[i];
queue->CurSize--;
}
bool temp(LPQUEUE queue) {
return queue->CurSize == 0;
}
int main() {
srand((unsigned)time(NULL));
LPQUEUE queue = CreateQueue();
DaTA TempData;
DaTA array[5] = {
"冠西",rand() % 50,
"一凡",rand() % 50,
"志翔",rand() % 50,
"冰冰",rand() % 50,
"俊洁",rand() % 50
};
for (int i = 0; i < 5; i++)
Push(queue, array[i]);
printf("VIP等级\t用户\n");
while (!temp(queue)) {
Pop(queue, &TempData);
printf("%d\t%s\n", TempData.Weight, TempData.name);
}
return 0;
}#include<stdio.h>