typedef struct pq
{
PNode header,tail;
}*PQ;
PQ PQinit()
{
PQ pq = (PQ)malloc(sizeof(&pq));
if (!pq)
{
cout<<"PQ create failed!!!"<<endl;
return NULL;
}
PNode h = (PNode)malloc(sizeof(&h));
if (!h)
{
cout<<"PNode create failed!!!"<<endl;
return NULL;
}
PNode t = (PNode)malloc(sizeof(&t));
if (!t)
{
cout<<"PNode create failed!!!"<<endl;
return NULL;
}
h->prev = t;
h->next = t;
t->prev = h;
t->next = h;
pq->header = h;
pq->tail = t;
return pq;
}
int PQempty(PQ pq)
{
return pq->header->next->next == pq->header;
}
void PQinsert(PQ pq,int v)
{
PNode t = (PNode)malloc(sizeof(&t));
if (!t)
{
cout<<"PNode create failed!!!"<<endl;
return;
}
t->data = v;
t->next = pq->header->next;
t->next->prev = t;
t->prev = pq->header;
pq->header->next = t;
}
int PQdeleteMAX(PQ pq)
{
int max = 0;
PNode t;
PNode x = pq->header->next;
for (t = x;t->next !=pq->tail;t = t->next)
{
if (t->data > x->data)
{
x = t;
}
}
max = x->data;
x->next->prev = x->prev;
x->prev->next = x->next;
free(x);
return max;
}
void PQdelete(PNode x)
{
x->next->prev = x->prev;
x->prev->next = x->next;
}