线性表的应用 链式有序表的合并旋转链表分隔链表翻转链表#includeiostream#includecstdlibusing namespace std;typedef int ElemType;typedef int Status;typedef struct LNode{ElemType data;struct LNode *next;int val;}LNode,*LinkList;//创建链表void CreateList_H(LinkList L,int n){Lnew LNode;L-nextNULL;cout请输入nendl;for(int i0;in;i){LNode*p new LNode;cinp-data;p-nextL-next;L-nextp;}}//输出链表void PrintList (LinkList L) {LNode *p;pL-next;while(p!NULL) {coutp-data ;pp-next;}coutendl;}//链式有序表的合并void MergeList_L(LinkList La,LinkList Lb,LinkList Lc){LNode *paLa-next;LNode *pbLb-next;LcLa;LNode *pcLc;while(papb){if(pa-datapb-data){pc-nextpa;pcpa;papa-next;}else{pc-nextpb;pcpb;pbpb-next;}}pc-nextpa?pa:pb;delete Lb;}//旋转链表struct LNode* rotateRight(struct LNode* head,int k){if(k0||headNULL||head-nextNULL)return head;int n1;struct LNode* tailhead;while(tail-next !NULL){tailtail-next;n;}int movenumk%n;if(movenum0)return head;tail-nexthead;int addnumn-movenum;while(addnum--)tailtail-next;struct LNode* newheadtail-next;tail-nextNULL;return newhead;}//分隔链表struct LNode*partition(struct LNode*head,int x){struct LNode*small(struct LNode*)malloc(sizeof(struct LNode ));struct LNode*large(struct LNode*)malloc(sizeof(struct LNode ));struct LNode*pahead;struct LNode*pbsmall;struct LNode*pclarge;while(pa!NULL){if(pa-val x){pb-nextpa;pbpb-next;}else{pc-nextpa;pcpc-next;}papa-next;}pc-nextNULL;pb-nextlarge-next;struct LNode* newhead small-next;return newhead;}//翻转链表struct LNode* reverseKGroup(struct LNode* head,int k){int xk;int n0;struct LNode* s (struct LNode*)malloc(sizeof(struct LNode));struct LNode* curs;struct LNode* slowhead;struct LNode* fastNULL;struct LNode* prevNULL;while(slow){n;slowslow-next;}slowhead;n/k;for(int i0;in;i){while(x){fastslow-next;slow-nextprev;prevslow;slowfast;x--;}cur-nextprev;while(cur-next)curcur-next;prevNULL;xk;}cur-nextslow;struct LNode* newheads-next;return newhead;}int main(){cout --- 测试 1: 合并有序链表 --- endl;LinkList La, Lb, Lc;cout 创建链表 A (需有序): ;CreateList_H(La, 3);cout 创建链表 B (需有序): ;CreateList_H(Lb, 3);MergeList_L(La, Lb, Lc);cout 合并结果: ;PrintList(Lc);cout endl;// --- 测试 2: 旋转链表 ---cout --- 测试 2: 旋转链表 --- endl;LinkList L_rotate;cout 创建用于旋转的链表: ;CreateList_H(L_rotate, 5);int k_rot 2;cout 向右旋转 k_rot 位后的结果: ;LNode* res_rot rotateRight(L_rotate-next, k_rot);// 临时打印LNode* temp res_rot;while(temp) { cout temp-data ; temp temp-next; }cout endl;cout endl;// --- 测试 3: 分隔链表 ---cout --- 测试 3: 分隔链表 (以 3 为界) --- endl;LinkList L_part;cout 创建用于分隔的链表: ;CreateList_H(L_part, 5); // 例如输入 3 1 4 1 5LNode* res_part partition(L_part-next, 3);temp res_part;while(temp) { cout temp-data ; temp temp-next; }cout endl;cout endl;// --- 测试 4: K 个一组翻转 ---cout --- 测试 4: K 个一组翻转 (k2) --- endl;LinkList L_rev;cout 创建用于翻转的链表: ;CreateList_H(L_rev, 5); // 例如输入 1 2 3 4 5LNode* res_rev reverseKGroup(L_rev-next, 2);temp res_rev;while(temp) { cout temp-data ; temp temp-next; }cout endl;return 0;}

相关新闻

最新新闻

江苏省工程技术研究中心认定对企业有什么好处?如何申报

江苏省工程技术研究中心认定对企业有什么好处?如何申报

一、江苏省工程技术研究中心认定好处获得该资质意味着企业打通了“政策资金税收优惠项目申报”的绿色通道:1.直接资金奖励省级奖励:根据2026年江苏省最新政策,认定为省级工程技术研究中心,省级财政给予最高100万元的直接奖励。地方…

2026/7/3 6:02:45
AI智能体技能开发实战:从原理到企业级应用

AI智能体技能开发实战:从原理到企业级应用

1. Agent Skills:AI时代的效率革命手册 作为一名在AI领域深耕多年的技术从业者,我见证了从早期规则引擎到如今智能体技术的演进历程。Agent Skills的出现,彻底改变了我们与AI协作的方式——它不再是简单的问答工具,而成为了真正能…

2026/7/3 6:02:45
如何快速解决网盘限速问题:九大网盘直链下载助手完整指南

如何快速解决网盘限速问题:九大网盘直链下载助手完整指南

如何快速解决网盘限速问题:九大网盘直链下载助手完整指南 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移动云盘 / 天…

2026/7/3 6:02:45
终极指南:3分钟学会用AutoRaise实现macOS悬停自动激活窗口

终极指南:3分钟学会用AutoRaise实现macOS悬停自动激活窗口

终极指南:3分钟学会用AutoRaise实现macOS悬停自动激活窗口 【免费下载链接】AutoRaise AutoRaise (and focus) a window when hovering over it with the mouse 项目地址: https://gitcode.com/gh_mirrors/au/AutoRaise 你是否厌倦了在macOS上频繁点击窗口来…

2026/7/3 6:02:45
采购类标书靠谱服务商

采购类标书靠谱服务商

在众多的招投标服务供应商中,湖南光德信息科技有限公司(简称“光德”)凭借其卓越的专业能力和优质的服务体系脱颖而出,成为采购类标书制作领域中的佼佼者。以下将从多个维度详细解析为何选择光德作为您的首选合作伙伴,…

2026/7/3 6:02:45
【转网安避坑指南】2026 最新网安行业深度复盘,完整还原真实职场全貌,零基础转行参考好文

【转网安避坑指南】2026 最新网安行业深度复盘,完整还原真实职场全貌,零基础转行参考好文

2026想转行黑客/网络安全?一篇带你了解真实的白帽黑客/网络安全职场! 最近你是不是也总刷到网络安全的内容?看别人做渗透测试、打 CTF 比赛,觉得又酷又高薪,心里蠢蠢欲动,也想转行试试?先别急&a…

2026/7/3 5:57:45

周新闻

月新闻