当前位置:网站首页>有序双向链表的实现。
有序双向链表的实现。
2022-08-01 19:22:00 【CaspianSea】
使用头节点和尾节点来管理链表,遍历,删除,修改都比较容易。
struct room
{
int price;
int mID;
int type;
int region;
int listIdx;
};
struct roomList
{
int idx;
int next;
int prev;
int tc_id;
};
room g_room[1000];
int g_roomIdx;
int g_tcId;
roomList g_roomListBuf[1000 + 200 + 1];
int g_roomListBufIdx;
int g_roomHeadList[10][10];
int g_roomTailList[10][10];
void init()
{
g_roomListBufIdx = 1;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
int p = g_roomHeadList[i][j] = g_roomListBufIdx++;
int q = g_roomTailList[i][j] = g_roomListBufIdx++;
g_roomListBuf[p].next = q;
g_roomListBuf[q].prev = p;
g_roomListBuf[p].prev = g_roomListBuf[q].next = 0;
}
}
++g_tcId;
}
void addRoom(int mID, int type, int region, int price)
{
//int i = g_roomIdx++;
g_room[mID].mID = mID;
g_room[mID].price = price;
g_room[mID].type = type;
g_room[mID].region = region;
int j = g_roomListBufIdx++;
g_room[mID].listIdx = j;
g_roomListBuf[j].idx = mID;
g_roomListBuf[j].next = g_roomListBuf[j].prev = 0;
//g_roomListBuf[j].tc_id = g_tcId;
int k = g_roomHeadList[type][region];
int iter = g_roomListBuf[k].next;
int tail = g_roomTailList[type][region];
while ( iter!= tail && g_room[g_roomListBuf[iter].idx].price < price)
{
iter = g_roomListBuf[iter].next;
}
int prev = g_roomListBuf[iter].prev;
g_roomListBuf[prev].next = j;
g_roomListBuf[j].prev = prev;
g_roomListBuf[j].next = iter;
g_roomListBuf[iter].prev = j;
}
void modifyPrice(int mID, int price)
{
int oldPrice = g_room[mID].price;
g_room[mID].price = price;
int listIdx = g_room[mID].listIdx;
int prev = g_roomListBuf[listIdx].prev;
int next = g_roomListBuf[listIdx].next;
g_roomListBuf[prev].next = next;
g_roomListBuf[next].prev = prev;
int type = g_room[mID].type;
int region = g_room[mID].region;
if (oldPrice < price)
{
int iter = next;
int tail = g_roomTailList[type][region];
while (iter != tail && g_room[g_roomListBuf[iter].idx].price < price)
{
iter = g_roomListBuf[iter].next;
}
prev = g_roomListBuf[iter].prev;
g_roomListBuf[prev].next = listIdx;
g_roomListBuf[listIdx].prev = prev;
g_roomListBuf[iter].prev = listIdx;
g_roomListBuf[listIdx].next = iter;
}
else
{
int iter = prev;
int head = g_roomHeadList[type][region];
while (iter != head && g_room[g_roomListBuf[iter].idx].price > price)
{
iter = g_roomListBuf[iter].prev;
}
next = g_roomListBuf[iter].next;
g_roomListBuf[next].prev = listIdx;
g_roomListBuf[listIdx].next = next;
g_roomListBuf[iter].next = listIdx;
g_roomListBuf[listIdx].prev = iter;
}
}
测试代码:
void printRoom(int type, int region)
{
int idx = g_roomHeadList[type][region];
int iter = g_roomListBuf[idx].next;
int head = g_roomTailList[type][region];
while (iter != head)
{
int roomIdx = g_roomListBuf[iter].idx;
std::cout << "room " << g_room[roomIdx].mID << " price " << g_room[roomIdx].price << std::endl;
iter = g_roomListBuf[iter].next;
}
}
int main(void)
{
init();
addRoom(100, 1, 0, 1560);
addRoom(90, 1, 0, 980);
addRoom(10, 1, 0, 8700);
addRoom(9, 1, 0, 3290);
addRoom(1, 1, 0, 2200);
addRoom(2, 1, 0, 4400);
printRoom(1, 0);
modifyPrice(9, 7500);
std::cout << "*********************************************" << std::endl;
printRoom(1, 0);
return 0;
}
运行结果:
room 90 price 980
room 100 price 1560
room 1 price 2200
room 9 price 3290
room 2 price 4400
room 10 price 8700
*********************************************
room 90 price 980
room 100 price 1560
room 1 price 2200
room 2 price 4400
room 9 price 7500
room 10 price 8700
边栏推荐
- MySQL开发技巧——存储过程
- GBase 8c中怎么查询数据库配置参数,例如datestyle。使用什么函数或者语法呢?
- AcWing 797. 差分
- Every calculation, & say what mean
- Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
- ClassID的计算中,&表示啥意思
- Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
- 数据库系统原理与应用教程(072)—— MySQL 练习题:操作题 121-130(十六):综合练习
- 10 个 PHP 代码安全漏洞扫描程序
- TestNG多个xml进行自动化测试
猜你喜欢
Try compiling QT test on Allwinner V853 development board
手撸代码,Redis发布订阅机制实现
kubernetes-部署nfs存储类
Become a Contributor in 30 minutes | How to participate in OpenHarmony's open source contributions in multiple ways?
MySQL database - stored procedures and functions
有点奇怪!访问目的网址,主机能容器却不行
Flowable-based upp (unified process platform) running performance optimization
首篇 NLP 领域图神经网络综述:127 页,从图构建到实际应用面面观
【综述专栏】IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
从普通进阶成优秀的测试/开发程序员,一路过关斩将
随机推荐
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes?
Risc-v Process Attack
网站建设流程
odoo coding conventions (programming conventions, coding guidelines)
首篇 NLP 领域图神经网络综述:127 页,从图构建到实际应用面面观
shell脚本专题(07):文件由cfs到bos
手撸代码,Redis发布订阅机制实现
Database Plus 的云上之旅:SphereEx 正式开源 ShardingSphere on Cloud 解决方案
10 个 PHP 代码安全漏洞扫描程序
Source code analysis of GZIPOutputStream class
【服务器数据恢复】服务器Raid5阵列mdisk组中多块磁盘离线的数据恢复案例
Combining two ordered arrays
力扣刷题之求两数之和
GEE(8):使用MODIS填补由去云后的Landsat影像计算得到的NDVI数据
Redis的内存淘汰策略和过期删除策略的区别是什么
升哲科技携全域数字化方案亮相2022全球数字经济大会
面试必问的HashCode技术内幕
把 Oracle 数据库从 RAC 集群迁移到单机环境
app直播源码,点击搜索栏自动弹出下拉框
哈哈!一个 print 函数,还挺会玩啊!