单链表练习实验报告 - --李兴福
更新时间:2023-11-28 04:37:01 阅读量: 教育文库 文档下载
单链表练习实验
——————电信14--1班 20141303225 李兴福
实验目的:熟练掌握单的基本操作及简单应用。 实验内容:
1、 设计一个算法,将单链表中的N个元素倒置。
2、 设单链表中的数据递增有序,写算法,将元素X插入到单链表中适当位置,且保持该
表的有序性。
3、 用单链表实现两个集合的合并。
4、 将两个用单链表表示的有序表合并成一个有序表。 以下代码均在C--free5.0环境下编译。
首先第一个题目:设计一个算法,将单链表中的N个元素倒置 程序代码:
#include
typedef int ELEMTYPE; //定义一个结构体 struct node { ELEMTYPE element; struct node *next;
}; //创建一个头结点和一个尾节点 struct node *head; struct node *tail;
void init() //实现单链表的初始化 { head=(struct node *)malloc (sizeof (struct node )); // 算法复杂度为o(1) if(!head) exit(0); head->next= NULL; tail=head; }
void append(ELEMTYPE a) //将元素一个个的接在单链表后面 { struct node *p; // 算法复杂度为o(1) p=(struct node *)malloc(sizeof(struct node)); if(!p) exit(0); p->element=a; p->next=tail->next; tail->next=p; tail=p; }
void inverse()//实现单链表的逆置 { struct node *p,*r; // While语句影响算法执行时间 算法复杂度为o(n) p=head->next;
if(!p) exit(0); head->next=NULL; while(p) { r=p->next; p->next=head->next; head->next=p; p=r; } }
void traversal() //遍历单链表,将其元素输出 { struct node *p; // for语句影响算法执行时间 算法复杂度为o(n) printf(\ for(p=head->next;p!=NULL;p=p->next) printf(\ printf(\ }
int main() // traversal()函数调用语句影响算法执行时间 { struct node *p; // 算法复杂度为o(n) init(); append(1); append(2); append(3); append(4); append(5); append(6); append(7); append(8); append(9); append(10); traversal(); inverse(); traversal(); }
运行结果如下图:
第二个题目:设单链表中的数据递增有序,写算法,将元素X插入到单链表中适当位置,且保持该表的有序性。 程序代码:
#include
typedef int ELEMTYPE; //定义一个结构体 struct node { ELEMTYPE element; struct node *next;
}; //创建一个头结点和一个尾节点 struct node *head; struct node *tail;
void init() //实现单链表的初始化 { head=(struct node *)malloc (sizeof (struct node )); // 算法复杂度为o(1) if(!head) exit(0); head->next= NULL; tail=head; }
void append(ELEMTYPE a) //将元素一个个的接在单链表后面 { struct node *p; // 算法复杂度为o(1) p=(struct node *)malloc(sizeof(struct node)); if(!p) exit(0);
p->element=a; p->next=tail->next; tail->next=p; tail=p; }
int insert(ELEMTYPE a)//插入一个元素后其单链表仍有序 { struct node *q,*ltemp,*p;// While语句影响算法执行时间 算法复杂度为o(n) int j=1; ltemp=(struct node *)malloc(sizeof (struct node)); if(!ltemp) exit(0); ltemp->element=a; q=head->next; p=q->next; while((p->element
void traversal() //遍历单链表,将其元素输出 { struct node *p; // for语句影响算法执行时间 算法复杂度为o(n) printf(\ for(p=head->next;p!=NULL;p=p->next) printf(\ printf(\ }
int main() // insert()调用函数语句影响算法执行时间 算法复杂度为o(n) { struct node *p; init(); append(10); append(30); append(34); append(36); append(40); append(45); append(48);
append(50); append(55); append(60); traversal(); insert(37); insert(42); traversal(); }
其运行结果如下图:
第三个题目:用单链表实现两个集合的合并。 程序代码:
#include
typedef int ELEMTYPE; //定义一个结构体 struct node { ELEMTYPE element; struct node *next; };
struct node *head;
struct node *tail; //创建三个头结点和三个尾节点 struct node *head1; struct node *tail1; struct node *head2;
正在阅读:
单链表练习实验报告 - --李兴福11-28
阻碍企业登陆创业板的34个细节问题12-10
PDP性格测试:你是老虎、孔雀、无尾熊、猫头鹰,还是变色龙型?10-27
教师资格证考试试题汇总(真题和模拟题)04-25
最新仁爱英语八年级(下) Unit 8 Topic 1 Section B 说 课 稿04-12
高三抒情散文4篇02-06
文化市场调查报告(精选多篇)09-28
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 兴福
- 单链
- 练习
- 实验
- 报告
- 2018高中政治每日一题(第7周)影响文化发展的重要因素3
- 网络路由试题(附答案)
- 2013版安全质量标准化考试卷doc
- pdms实用基本操作大全
- 劳动法练习题
- 水城县鱼塘煤矿 二采回风 - 图文
- 西北工业大学博士研究生学位论文编写规则(试用版)
- 与2010年上海会考有关第四分册知识点默写答案(第四册全部)1
- 长治学院详细介绍 - 图文
- 初中语文修改病句练习(及答案)24题分析
- 新教科版四年级科学上册单元复习资料-全册(打印版)
- 主要单元工程、重要隐蔽工程、工程关键部位的概念及验收签证
- 钢筋和混凝土材料的力学性能
- LC—2控制器使用说明书
- 方正春元-总账V2.2操作手册
- 难点探究专题:平面直角坐标系中点的坐标的变化规律(选做)
- 计算机网络技术基础试题库(包括答案)
- 《卖火柴的小女孩》教学设计
- 通信原理实验报告(8份) - 图文
- 廉政风险防控方案