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