ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:49.79KB ,
资源ID:26176366      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/26176366.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编写List类实验报告.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编写List类实验报告.docx

1、编写List类实验报告 2015/2016(1)实验题目 List类编写及其成员函数拓展 学生姓名 韩笑 学生学号 201426811704 学生班级 计算机+自动化1402班 任课教师 * 提交日期 2015-11-13 计算机科学与技术学院(软件学院)实验报告一、 题目的内容 编写一个算法判断链表中的数据项是否按从小到大排序,该链表的第一个节点由first指向。 对于给定的整数n,编写一个算法把新的节点插入到链表中第n个节点之后的位置,该链表的第一个节点由first指向。 编写一个算法来颠倒一个链表,该链表的第一个节点由first指向。不要复制列表元素;而是重载链接和指针,使得first指

2、向原来的最后一个节点,且节点之间的所有链接都反向。二、 做题思路及设计分析题目:作业中共有三题,都是基于链表并对其函数进行扩充,因此首先需要自行编写一个链表类,之后再在链表类中增加所需的函数来达到题目要求。 List 类:先构造一个node内部类,存放各节点的值、尾指针(见第四部分代码)。在List类中,有内部成员变量头节点(first),list大小(Size);内部成员函数swap、erase(删除指定节点)、get_node(返回第n个节点);功能函数除构造析构外有display(输出函数)、erase(删除第n个节点)、insert(插入节点,见“第二问”)、push_front(在链

3、表首位插入节点)、push_back(在链表尾部插入节点)、reverse(见“第三问”)、judge_sorted(见“第一问”)、empty(判空函数)、size(返回list大小)、copy construction(拷贝构造函数)以及对等号的重载。Next Node节点示意图:Key List示意图: 第 一 问:函数名定为judge_sorted,从头节点first开始往后遍历,依次判断前一个数是否小于后一个数(前一个数用中间变量t来存储),如果不满足则直接返回false,然则为true。第 二 问:函数名定为insert,函数中首先判断list中Size是否为0,若为0,则直接新建

4、first,并指向的key赋值,并同时把Size变为1,关键代码如下:first = new node;first-next = NULL;first-key = val;Size = 1;如果节点在队尾,则只需将队尾节点(x)的next指向新建节点(a),关键代码如下:x-next = a;对于中间节点,先将原节点(x)的next值赋予新建节点(a)的next,原节点next值改为新建节点,关键代码如下:a-next = x-next;x-next = a;第 三 问:函数名定为reverse,从前往后遍历链表,定义中间变量结点temp和r,依次改变每个结点的next值,最后将first指向

5、最后一个节点即可,关键代码如下:node *q = first, *r, *temp = NULL;while(1) /exchange all nodes head pointer and tail pointer r = q-next; q-next = temp; if(r = NULL) /let the last node to be the first one first = q; break; temp = q; q = r;三、 程序调试、测试、运行记录 主要的测试经过如下: 四、 源代码代码实现工程名为:List具体函数声明/定义如下: List.h#pragma once#

6、ifndef _List_H_#define _List_H_#include using namespace std;class Listprivate: class node public: node *next; /tail pointer int key; /number node(); /constructed function node(int x); /Function overloading node(); /destructor ; node *first; /first pointer int Size; /size of the list inline void swap

7、(node* a, node* b); /exchange the number between node a and b void erase(node* x); /remove the node x from the list inline node* get_node(int pos); /get the posth nodes pointerpublic: List(); /constructed function List(const List& l);/copy constructor List(); /destructor List& operator= (const List&

8、 l); /operator = overloading void erase(int pos); /erase the node at the position of pos void display(ostream & out); /display function void insert(int pos, int val); /insert x to the pos location in the list void push_front(int val); /insert item to the begin of the list void push_back(int val); /i

9、nsert item to the end of the list void reverse(); /reverse the list bool judge_sorted(); /judge whether the list is in order bool empty(); /judge whether the list is empty int size(); /show size of the list friend ostream& operator(ostream & out, List& l); /operator key; a-key = b-key; b-key = temp;

10、void List:erase(node* x) /remove the node at the right of node x from the list if(Size = 1); else if(x-next = NULL); else if(first = x) first = x-next; else node *temp = first; while(temp != x) temp = temp-next; temp-next = x-next; delete x; x = NULL; Size-;inline List:node* List:get_node(int pos) /

11、get the posth nodes pointer if(pos Size | pos 0) /exceptional handling cerr 0) x = x-next; pos-; return x;List:List(const List& l) /copy constructor first = new node(); Size = 0; node* p = l.first; while(p != NULL) push_back(p-key); p = p-next; List:List() /constructed function first = new node(); f

12、irst-next = NULL; Size = 0;List:List() /destructor node* p; while(first != NULL) p = first; first = first-next; delete p; void List:erase(int pos) /erase the node at the position of pos erase(get_node(pos-1);void List:display(ostream & out) /display function node *p = first; int cnt = Size; while(cn

13、t-) out key; if(cnt != 0) out next; void List:insert(int pos, int val)/insert x to the pos location in the list if(Size = 0) first = new node; first-next = NULL; first-key = val; Size = 1; return; node *x = get_node(pos-1); node *a = new node(val); if(x-next = NULL) x-next = a; a-next = NULL; else a

14、-next = x-next; x-next = a; Size+;void List:push_front(int val) /insert item to the begin of the list insert( 1, val); swap(first, first-next);void List:push_back(int val) /insert item to the end of the list insert( Size, val);void List:reverse() /reverse the list node *q = first, *r, *temp = NULL;

15、while(1) /exchange all nodes head pointer and tail pointer r = q-next; q-next = temp; if(r = NULL) /let the last node to be the first one first = q; break; temp = q; q = r; bool List:judge_sorted() /judge whether the list is in order int t = first-key; node* p = first-next; if(p-key key t) return fa

16、lse; p = p-next; else while(p != NULL) if(t key) return false; p = p-next; return true;bool List:empty() /judge whether the list is empty return Size = 0;int List:size() /show size of the list return Size;ostream& operator (ostream & out, List & l) /operator key); p = p-next; Size = l.Size; return *

17、this; main.cpp#include List.hint main() List l; cout Test push_back(1):; l.push_back(1); cout l endl; /- cout Test push_front(2):; l.push_front(2); cout l endl; /- cout Test push_back(2):; l.push_back(2); cout l endl; /- cout Test insert(1,3):; l.insert(1,3); cout l endl; /- cout Test erase(1):; l.e

18、rase(1); cout l endl; /- cout Test reverse():; l.reverse(); cout l endl; /- cout Test empty(1):; if(l.empty() cout The list is empty. endl; else cout The list isnt empty. endl; /- cout Test size():; cout l.size() endl; /- cout Test judge_sorted():; if(l.judge_sorted() cout This list has already sorted. endl; else cout This list hasnt sorted yet. endl; /- cout Test copy constructor:; List temp(l); cout temp endl; /- cout Test operator = overloading:; List cnt; cnt = l; cout cnt endl; /- cout Exceptional: ; temp.erase(100);五、 总结从本次实验我基本掌握list的内部实现,对于自己的编写代码的能力又有所提高。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1