无序双向链表的优先队列
来源:互联网 发布:云南广电网络营业厅 编辑:程序博客网 时间:2024/04/26 05:13
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef int Item;
typedef struct node
{
Item data;
struct node *next,*prev;
}*PQlink;
typedef struct pq
{
PQlink header,tail;
}*PQ;
PQ createLink()
{
PQ pq = malloc(sizeof(*pq));
if(!pq)
{
return null;
}
PQlink h = malloc(sizeof(*h));
if(!h)
{
return null;
}
PQlink t = malloc(sizeof(*t));
if(!t)
{
return null;
}
h->next = t;
h->prev = t;
t->prev = h;
t->next = h;
pq->header = h;
pq->tail = t;
return pq;
}
void insertPQ(PQ pq,Item aData)
{
PQlink t = malloc(sizeof(*t));
if(!t)
{
return;
}
t->data = aData;
t->next = pq->header->next;
t->next->prev = t;
t->prev = pq->header;
pq->header->next = t;
}
int emptyPQ(PQ pq)
{
return pq->header->next->next == pq->header;
}
Item deleMax(PQ pq)
{//最大优先队列模拟
Item max = 0;
PQlink x = pq->header->next,t;
for (t = x;t->next != pq->header;t = t->next)
{
if (x->data < t->data)
{
x = t;
}
}
max = x->data;
x->next->prev = x->prev;
x->prev->next = x->next;
free(x);
return max;
}
Item deleMin(PQ pq)
{//最小优先队列模拟
Item min = 0;
PQlink x = pq->header->next,t;
for (t = x;t->next != pq->header;t = t->next)
{
if (x->data > t->data)
{
x = t;
}
}
min = x->data;
x->next->prev = x->prev;
x->prev->next = x->next;
free(x);
return min;
}
void changePQ(PQ pq,PQlink b,Item v)
{//改变其中的一个数据
PQlink x = pq->header->next;
for (; x->next != pq->header;x = x->next)
{
if (x->data == b->data)
{
break;
}
}
if (x->next != pq->header)
{
b->data = v;
}
else
{
printf("change is failed");
}
}
void joinPQ(PQ a,PQ b)
{//合并
if (a == b)
{
printf("join failed,a and b is same");
return;
}
if (b->header->next->next == b->header)
{
printf("b is nil");
return;
}
a->tail->prev->next = b->header->next;
b->header->next->prev = a->tail->prev;
b->tail->next = a->header;
a->tail->next = b->tail;
free(a->tail);
free(b->header);
}
void deletePQ(PQ a,PQlink x)
{//删除单个节点,注意没有写查找这个节点是否存在
if (x == a->header||x == a->tail)
{
printf("node is delete failed");
return;
}
x->next->prev = x->prev;
x->prev->next = x->next;
free(x);
}
void copyPQ(PQ a,PQ b)
{//链表拷贝
for (PQlink x = a->header->next; x->next != a->header;x = x->next)
{
insertPQ(b,x->data);
}
}
void print(PQ a)
{
int j = 1;
for(PQlink x = a->header->next; x->next != a->header;x = x->next,++j)
{
printf("%d ",x->data);
if (j %10 ==0)
{
printf("\n");
}
}
}
void deleteAll(PQ pq)
{//释放内存
PQlink t = pq->header->next;
PQlink x;
int i = 1;
for (;t->next != pq->header;t = x,++i)
{
x = t->next;
printf("%d ",t->data);
free(t);
if (i%10 ==0)
{
printf("\n");
}
}
free(pq->header);
free(pq->tail);
free(pq);
}
- 无序双向链表的优先队列
- 无序双向优先队列
- 无序链表实现优先队列
- 优先队列:采用无序顺序表描述
- HDU 6136 Death Podracing(优先队列+双向链表)
- BZOJ2151 种树 贪心+双向链表+优先队列
- Java双向队列,用双向链表实现的
- 队列的双向链表实现
- 双向队列的链表实现
- 双向链表实现的消息队列
- c++双向链表构成的队列
- 优先队列的链表实现
- 双向链表 + 栈 + 队列
- 双向链表实现队列
- HDU5521 双向最短路+优先队列优化
- Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 双向链表+反向做+优先队列
- 双向链表、队列与栈的C/C++实现
- 基于双向链表的双端队列
- Unity 3D + Vuforia制作AR人物互动
- OpenGL学习三十二:二次几何体
- 下面的一小段儿代码,我不知道为什么它的执行逻辑是什么?
- 我的vim在windows下的配置
- Android 自动接听电话和挂断
- 无序双向链表的优先队列
- struts2中的jar文件
- 机器学习是什么
- 织梦通过 phpmyadmin 导出的数据,再次导入的时候报错
- JAVA基础——包精简总结
- MIPS系列笔记-CP0 Hazard
- Bootstrap多级级联菜单
- IOS Sent Events 事件
- java linkedblocking queue