# include "stdio.h"
# include "malloc.h"
# include "stdlib.h"
typedef struct Queue
{
int data;
int Priority;
Queue * Next;
}* PQUEUE;
bool insert(PQUEUE p,int i, int j);
bool pop(PQUEUE p);
void sort(PQUEUE p);
int length = 0;
PQUEUE pT;
int main(void)
{
PQUEUE pH = (PQUEUE)malloc(sizeof(Queue));
insert(pH, 75, 8);
insert(pH, 54, 4);
insert(pH, 75, 6);
insert(pH, 23, 5);
insert(pH, 81, 4);
insert(pH, 65, 3);
insert(pH, 43, 4);
insert(pH, 34, 2);
sort(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
return 0;
}
bool insert(PQUEUE p,int i, int j)
{
if(i>= 0 && i<= 100 && j>=0 && j<=100)
{
PQUEUE pNew = (PQUEUE)malloc(sizeof(Queue));
if(length == 0)
{
pT = NULL;
}
if(pT == NULL)
{
pT = p;
}
if(NULL == pNew)
{
printf("动态内存分配失败~!");
exit(-1);
}
pNew->data = i;
pNew->Priority = j;
pT->Next = pNew;
pNew->Next = NULL;
pT = pNew;
length++;
return true;
}
return false;
}
PQUEUE p2;
bool pop(PQUEUE p)
{
if(length != 0)
{
p2 = p;
p = p->Next;
printf("%d,", p->data);
printf("%d\n", p->Priority);
p2->Next = p->Next;
length--;
return true;
}
return false;
}
void sort(PQUEUE p)
{
if(length != 0)
{
PQUEUE w,q;
int i, j, t1,t2;
for(i=0,w=p->Next; i < length-1; ++i,w = w->Next)
{
for(j=i+1,q=w->Next; j < length; ++j,q = q->Next)
{
if(w->Priority < q->Priority)
{
t1 = w->data;
w->data = q->data;
q->data = t1;
t2 = w->Priority;
w->Priority = q->Priority;
q->Priority = t2;
}
}
}
}
return;
}
/*
都满足你的要求了,以上是使用链表结构的队列
*/
#include
#include
//数据列表结点结构体
typedef struct ListNode
{
int ndata;
int npriority;
struct ListNode *pNext;
};
//数据列表结构体
typedef struct DataList
{
ListNode *pHead;
ListNode *pLast;
int arPriorityInfoStore[11];
};
//生成链表变初始化,使用数据队列首先调用
DataList *CreateList()
{
int i;
//内存申请
DataList *list = (DataList*)malloc(sizeof(DataList));
//结构体初始化
list->pHead = list->pLast = 0;
for(i=0;i<11;i++)
{
list->arPriorityInfoStore[i] = 0;
}
return list;
}
/*!数据入队
* 参数list:数据队列链表
* 参数data:待入队的数据
* 参数prt :待入队数据的优先级
* 返回值:布尔型,true为入队成功,false失败
*/
bool Insert(DataList *list,int data, int prt)
{
//内存申请
ListNode *pNewNode = (ListNode *)malloc(sizeof(ListNode));
//内在申请失败,无法入队
if(pNewNode = 0)
return false;
//新结点初始化
pNewNode->ndata = data;
pNewNode->npriority = prt;
pNewNode->pNext = 0;
list->arPriorityInfoStore[prt]++;
//区别对待第一个结点
if(list->pHead == 0)
{
list->pHead = list->pLast = pNewNode;
}
else
{
list->pLast->pNext = pNewNode;
list->pLast = pNewNode;
}
}
/*! 数据出队
* 参数list:数据队列链表
* 返回值:整型,优先最大的数据
*/
int Pop(DataList *list)
{
int nMaxPriority;
ListNode *pCursortNode;
//找出队列中的最大优先级
for(nMaxPriority = 10; nMaxPriority >= 0; nMaxPriority--)
{
if(list->arPriorityInfoStore[nMaxPriority]>0)
break;
}
//由于数据有效范围为0到100,故取-1作为无效值表示此时列表为空
if(nMaxPriority < 0)
return -1;
//找到优先级最大的点,取值返回
for(pCursortNode = list->pHead; pCursortNode->pNext!=0; pCursortNode = pCursortNode->pNext)
{
if(pCursortNode->npriority != nMaxPriority)
continue;
else
break;
}
return pCursortNode->ndata;
}
未进行测试,也未写主函数。功能函数已经写出,可能中间会有小问题,你调一下即能使用。