中北大学算法与数据结构实验报告

上传人:今*** 文档编号:105769252 上传时间:2019-10-13 格式:DOC 页数:72 大小:693.17KB
返回 下载 相关 举报
中北大学算法与数据结构实验报告_第1页
第1页 / 共72页
中北大学算法与数据结构实验报告_第2页
第2页 / 共72页
中北大学算法与数据结构实验报告_第3页
第3页 / 共72页
中北大学算法与数据结构实验报告_第4页
第4页 / 共72页
中北大学算法与数据结构实验报告_第5页
第5页 / 共72页
点击查看更多>>
资源描述

《中北大学算法与数据结构实验报告》由会员分享,可在线阅读,更多相关《中北大学算法与数据结构实验报告(72页珍藏版)》请在金锄头文库上搜索。

1、 实验类别:算法与数据结构 专 业:信息与计算科学 班 级:13080241 学 号:1308024120 姓 名:杨燕 中北大学理学院实验一 链表的应用(一)建立线性表【实验内容】1、 画出详细规范的算法流程图2、 定义链表结点数据类型3、 定义链表数据类型4、 实现单向线性链表的建立5、 实现单向线性链表取元素6、 实现单向线性链表遍历7、 实现单向线性链表插入8、 实现单向线性链表删除【实验方法与步骤】1、 定义链表结点数据类型typedef struct LNodeint data;struct LNode *next;LNode,*LinkList;其中int data;表示节点是整

2、型数据,若定义浮点型的为:float data;其他类似。2、 定义链表数据类型typedef char DateTypetypedef struct LNodeDateType data;struct LNode *next;LNode,*LinkList;3、 实现单向线性链表的建立#include #include #include typedef struct LNodeint data;struct LNode *next;LNode,*LinkList;void CreateList_L(LinkList &L,int n) /逆位序输入n个数据元素的值,建立带头结点的单链表L i

3、nt i; LNode *p; L=(LinkList)malloc(sizeof(LNode); L-next=NULL;/先建立一个带头结点的空链表 cout请输入创建的单链表中的数据: 0;-i) p=(LinkList)malloc(sizeof(LNode);/生成新结点 cinp-data; p-next=L-next;/将新结点插入到单链表的头 L-next=p;/修改单链表头结点的指针域 /for结束 if(n) cout成功创建一个单链表!endl; else cout创建了一个空链表!endl; void main() LinkList L; int InitLNodeNu

4、m; coutCreateList_L.cppendl=endl; coutendlInitLNodeNum; CreateList_L(L,InitLNodeNum); coutOK.!endl; getch();/end of main() function4、 实现单向线性链表取元素#include #include #include #define ElemType int #define LIST_MAX_LENGTH 100 /LIST_MAX_LENGTH是单链表L的最大长度typedef struct LNodeElemType data;struct LNode *next;

5、LNode,*LinkList;void CreateList_L(LinkList &L,int n) /创建一个带头结点的单链表L int i; LNode *p; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; for(i=n;i0;-i) p=(LinkList)malloc(sizeof(LNode); cinp-data; p-next=L-next; L-next=p; int GetElem_L(LinkList L,int i,int &e) /GetElem_L() function/L为带头结点的单链表的头指针,当第i个元素存

6、在时,其值赋给e并返回OK, /否则返回Error LNode *p; int j=1; p=L-next; /初始化,p指向链表第一个结点,j为计数器 while(p&jnext;+j; if(!p|ji) cout这个元素 i 不存在!data; return (e); /结束单链表的取元素void main() /main() function LinkList L; int e; /e can be Every DataType int i,LListNodeNum; /j is a counter for cycle coutGetElem_L.cppendl=endlendl; c

7、outLListNodeNum; cout请输入创建的单链表中的数据: endl; CreateList_L(L,LListNodeNum); coutendl成功创建一个单链表L!endl; cout你想提取哪一个位置上的数据?: i; /输入要提取的数据 if(iLListNodeNum) coutendl输入错误!endl; GetElem_L(L,LListNodeNum-i+1,e); coutendl位置i 在单链表中的数据是: e; coutendl.OK.!endl; getch();5、 实现单向线性链表遍历#include #include #include #includ

8、e #define LIST_INIT_LENGTH 10 /LIST_INIT_LENGTH is the Init_Define_Length of LinkList typedef int ElemType; typedef struct LNodeint data;struct LNode *next;LNode,*LinkList;void CreateList_L(LinkList &L,int n) /CreatList_L() subfunction /To Creatre a LinkList L with HeadNode int i; LNode *p; int arra

9、yLIST_INIT_LENGTH; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; printf(Please input the nodes data: n); for(i=0;in;i+) /input the data to create the LinkList scanf(%d,&arrayi); for(i=0;idata=arrayi; /for example to a CreateList p-next=L-next; L-next=p; /end of for /end of CreateList_L() function v

10、oid Contray(LinkList &head) /Contray() function /Delete the NO.i element of LinkList and return by variable e LNode *p,*q; p=head; head=NULL; while(p) q=p; p=p-next; q-next=head; head=q; /end of while coutendlSuccess to Contray the LinkList !;/end of Contray() functionvoid main() /main() function Li

11、nkList L; LNode *p; int i,LNodeNum; /j is just a counter for cycle coutContray.cppendl=endlendl; coutHow many nodes do you want to create? ; cinLNodeNum; CreateList_L(L,LNodeNum); p=L; coutendlThe next ones Inserted Direction is always in front of this.endl; for(i=0;inext; coutdata ; /output the LinkList before Contray coutendl; cout

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号