已阅读5页,还剩12页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
双向链表排序练习(C+和Delphi代码)双向链表排序练习(C+和Delphi代码)2006-12-15 23:28/VC+版本#include #include #include #include typedef struct tagNode tagNode* forward; int data; tagNode* next; Node, *pNode;typedef struct tagHeadLast pNode begin; pNode end; HeadLast;HeadLast CreateList(int n);void InsertListAtLast(HeadLast& list, pNode node);void InsertList(HeadLast& list, pNode node);void DeleteList(HeadLast& list);bool IsBlankHeadLast(HeadLast& list);void DispList(HeadLast& list);void SortList(HeadLast& list);int main(int argc, char *argv) srand(unsigned)time(NULL); HeadLast List = CreateList(108); DispList(List); coutendlforward = NULL; p-next = NULL; p-data = rand() % 100; InsertListAtLast(hl,p); return hl;void InsertList(HeadLast& list, pNode node) if(IsBlankHeadLast(list) node-forward = NULL; node-next = NULL; list.begin = node; list.end = node; return; pNode p = list.begin; while(p != NULL) if(node-data data) if(p = list.begin) node-next = p; p-forward = node; node-forward = NULL; list.begin = node; return; node-next = p; node-forward = p-forward; p-forward = node; node-forward-next = node; return; p = p-next; InsertListAtLast(list, node);void InsertListAtLast(HeadLast& list, pNode node) if(IsBlankHeadLast(list) list.begin = node; list.end = node; node-forward = NULL; node-next = NULL; else list.end-next = node; node-forward = list.end; node-next = NULL; list.end = node; void DeleteList(HeadLast& list) if(IsBlankHeadLast(list) return; pNode p; p = list.begin-next; if(p = NULL) delete list.begin; list.begin = NULL; list.end = NULL; return; while(p != list.end) delete p-forward; p = p-next; delete p-forward; delete p;bool IsBlankHeadLast(HeadLast& list) if(list.begin = NULL) return true; return false;void DispList(HeadLast& list) int index = 1; pNode p; p = list.begin; while(p != NULL) coutsetw(5)setiosflags(ios:left)index+datanext; void SortList(HeadLast& list) if(list.begin = list.end) return; pNode p, oldp; p = list.begin; list.begin = NULL; list.end = NULL; while(p != NULL) oldp = p; p = p-next; InsertList(list, oldp); /DevC+版本#include #include #include using namespace std;typedef struct tagNode tagNode* forward; int data; tagNode* next; Node, *pNode;typedef struct tagHeadLast pNode begin; pNode end; HeadLast;HeadLast CreateList(int n);void InsertListAtLast(HeadLast& list, pNode node);void InsertList(HeadLast& list, pNode node);void DeleteList(HeadLast& list);bool IsBlankHeadLast(HeadLast& list);void DispList(HeadLast& list);void SortList(HeadLast& list);int main(int argc, char *argv) srand(unsigned)time(NULL); HeadLast List = CreateList(108); DispList(List); coutendlforward = NULL; p-next = NULL; p-data = rand() % 100; InsertListAtLast(hl,p); return hl;void InsertList(HeadLast& list, pNode node) if(IsBlankHeadLast(list) node-forward = NULL; node-next = NULL; list.begin = node; list.end = node; return; pNode p = list.begin; while(p != NULL) if(node-data data) if(p = list.begin) /插在最前 node-next = p; p-forward = node; node-forward = NULL; list.begin = node; return; node-next = p; /插在中间 node-forward = p-forward; p-forward = node; node-forward-next = node; return; p = p-next; InsertListAtLast(list, node);void InsertListAtLast(HeadLast& list, pNode node) if(IsBlankHeadLast(list) list.begin = node; list.end = node; node-forward = NULL; node-next = NULL; else list.end-next = node; node-forward = list.end; node-next = NULL; list.end = node; void DeleteList(HeadLast& list) if(IsBlankHeadLast(list) return; pNode p; p = list.begin-next; if(p = NULL) delete list.begin; list.begin = NULL; list.end = NULL; return; while(p != list.end) delete p-forward; p = p-next; delete p-forward; delete p;bool IsBlankHeadLast(HeadLast& list) if(list.begin = NULL) return true; return false;void DispList(HeadLast& list) int index = 1; pNode p; p = list.begin; while(p != NULL) coutsetw(5)setiosflags(ios:left)index+datanext; void SortList(HeadLast& list) if(list.begin = list.end) return; pNode p, oldp; p = list.begin; list.begin = NULL; list.end = NULL; while(p != NULL) oldp = p; p = p-next; InsertList(list, oldp); /Delphi版本program Doub;$APPTYPE CONSOLEuses SysUtils;type pNode = Node; Node = record forw: pNode; data: integer; next: pNode; end; HeadLast = record head: pNode; last: pNode; end;function IsBlankHeadLast(list: HeadLast): boolean;begin result := false; if list.head = nil then result := true;end;procedure InsertListAtLast(var list: HeadLast; node: pNode);begin if IsBlankHeadLast(list) then begin list.head := node; list.last := node; node.forw := nil; node.next := nil; end else begin list.last.next := node; node.forw := list.last; node.next := nil; list.last := node; end;end;function CreateList(n: integer): HeadLast;var p: pNode;begin result.head := nil; result.last := nil; while true do begin if n = 0 then break; new(p); p.forw := nil; p.next := nil; p.data := Random(100); InsertListAtLast(result, p); dec(n); end;end;procedure InsertList(var list: HeadLast; node: pNode);var p: pNode;begin if IsBlankHeadLast(list) then begin node.forw := nil; node.next := nil; list.head := node; list.last := node; exit; end; p := list.head; while p nil do begin if node.data p.data then begin if p = list.head then begin node.next := p; p.forw := node; node.forw := nil; list.head := node; exit; end; node.next := p; node.forw := p.forw; p.forw := node; node.forw.next := node; exit; end; p := p.next; end; InsertListAtLast(list, node);end;procedure DeleteList(var list: HeadLast);var p: pNode;begin if IsBlankHeadLast(list) then exit; p := list.head.next; if p = nil then begin Dispose(list.head); list.head := nil; list.last := nil; exit; end; while p list.last do begin Dispose(p.forw); p := p.next; end; Dispose(p.forw); Dispose(p);end;function setw(str: string; width: integer): string;begin result := str; while
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年湖北省中考物理真题及答案解析
- 2026中国新能源汽车电池行业竞争格局及市场分析报告
- 2026中国智能睡眠监测设备技术演进与临床应用前景展望
- 2026中国轨道交通车辆轴箱轴承状态监测与智能诊断系统
- 2026乔木行业市场深度调研及发展趋势与投资战略研究报告
- 2026葡萄牙清洁能源行业市场需求与发展方向分析报告
- 2026Fast芯片组市场需求变化与客户偏好调查报告
- 2026叶黄素酯行业应急管理体系与危机公关策略报告
- 2026中国医疗器械行业市场供需分析及投资评估规划发展研究报告
- 2026软件开发行业市场现状调研及投资风险评估规划报告
- 军训教官教案完整版
- 企业知识产权宣传培训课件
- 中职等比数列课件
- 太原理工大学《大学物理A》2025 - 2026学年第一学期期末试卷(A卷)
- DBJT 15-20-2016 建筑基坑工程技术规程
- 近年国内电解铝行业重大事故案例
- ICU进修汇报医学知识讲解讲义
- 洗手间6s管理制度
- 养老院章程范本2016
- DB52T 1283-2018 精准扶贫 农村“组组通”硬化路建设与管理养护规范
- 车厢维修合同范本
评论
0/150
提交评论