编写main函数测试template情况下线性表的基本功能的正确性

时间: 2023-05-20 14:06:48 浏览: 60
好的,可以的。以下是一个示例的main函数,用于测试template情况下线性表的基本功能的正确性: ``` #include <iostream> #include "List.h" using namespace std; int main() { List<int> myList; // 测试插入元素 myList.insert(0, 1); myList.insert(1, 2); myList.insert(2, 3); // 测试获取元素 cout << "第一个元素是:" << myList.get(0) << endl; cout << "第二个元素是:" << myList.get(1) << endl; cout << "第三个元素是:" << myList.get(2) << endl; // 测试删除元素 myList.remove(1); // 再次测试获取元素 cout << "第一个元素是:" << myList.get(0) << endl; cout << "第二个元素是:" << myList.get(1) << endl; return 0; } ``` 这个main函数创建了一个List对象,然后测试了插入、获取和删除元素的功能。你可以根据自己的需要修改这个main函数,测试其他的功能。
相关问题

用C++,设有两条有序链表(即 data 域元素的关键字由前往后不断增大),试设计算法,将这两 条链表合并为一条新的有序链表,原链表不变。两条链表中 data 域关键字相同的元素只选 取一个存储到新的有序链表中,不同的元素都存储到新的有序链表中。 要求: ➢ 直接编写链表的友元函数完成该功能。 ➢ 链表的 data 域可存储用户自定义类对象。 ➢ 编写测试 main()函数测试线性表的正确性

``` #include <iostream> using namespace std; template<class T> class Node{ public: T data; //数据域 Node<T> *next; //指针域 Node(const T &x, Node<T> *n = NULL){ //构造函数 data = x; next = n; } Node(){next = NULL;} //构造函数 ~Node(){} //析构函数 }; template<class T> class List{ public: List(){head = new Node<T>; head->next = NULL;} //构造函数 ~List(){makeEmpty();} //析构函数 void makeEmpty(); //置空函数 Node<T> *getHead() const{return head;} int Length() const; //求长度函数 Node<T> *Search(const T &x) const; //查找函数 Node<T> *Locate(int i) const; //定位函数 bool getData(int i, T &x) const; //取数据函数 void setData(int i, const T &x); //设置数据函数 bool Insert(int i, const T &x); //插入函数 bool Remove(int i, T &x); //删除函数 bool IsEmpty() const{return head->next == NULL;} //判断是否为空 bool IsFull() const{return false;} //判断是否为满 void Sort(); //排序函数 void Reverse(); //逆置函数 void Merge(List<T> &list); //合并函数 void Print() const; //输出函数 friend void TestList(); //测试函数 private: Node<T> *head; //头指针 }; template<class T> void List<T>::makeEmpty(){ //置空函数 Node<T> *p = head->next, *q; head->next = NULL; while(p != NULL){ q = p->next; delete p; p = q; } } template<class T> int List<T>::Length() const{ //求长度函数 Node<T> *p = head->next; int len = 0; while(p != NULL){ len++; p = p->next; } return len; } template<class T> Node<T> *List<T>::Search(const T &x) const{ //查找函数 Node<T> *p = head->next; while(p != NULL && p->data != x) p = p->next; return p; } template<class T> Node<T> *List<T>::Locate(int i) const{ //定位函数 if(i < 0) return NULL; Node<T> *p = head; int j = -1; while(p != NULL && j < i){ j++; p = p->next; } return p; } template<class T> bool List<T>::getData(int i, T &x) const{ //取数据函数 if(i < 0) return false; Node<T> *p = Locate(i); if(p == NULL) return false; else{ x = p->data; return true; } } template<class T> void List<T>::setData(int i, const T &x){ //设置数据函数 if(i < 0) return; Node<T> *p = Locate(i); if(p == NULL) return; else p->data = x; } template<class T> bool List<T>::Insert(int i, const T &x){ //插入函数 Node<T> *p = Locate(i - 1); if(p == NULL) return false; else{ Node<T> *q = new Node<T>(x, p->next); p->next = q; return true; } } template<class T> bool List<T>::Remove(int i, T &x){ //删除函数 Node<T> *p = Locate(i - 1); if(p == NULL || p->next == NULL) return false; else{ Node<T> *q = p->next; x = q->data; p->next = q->next; delete q; return true; } } template<class T> void List<T>::Sort(){ //排序函数(冒泡排序) Node<T> *p = head->next, *q; T temp; while(p != NULL){ q = p->next; while(q != NULL){ if(p->data > q->data){ temp = p->data; p->data = q->data; q->data = temp; } q = q->next; } p = p->next; } } template<class T> void List<T>::Reverse(){ //逆置函数 Node<T> *p = head->next, *q; head->next = NULL; while(p != NULL){ q = p->next; p->next = head->next; head->next = p; p = q; } } template<class T> void List<T>::Merge(List<T> &list){ //合并函数 Node<T> *a = head->next, *b = list.getHead()->next, *c = head; while(a != NULL && b != NULL){ if(a->data < b->data){ c->next = a; a = a->next; c = c->next; } else if(a->data > b->data){ c->next = b; b = b->next; c = c->next; } else{ c->next = a; a = a->next; b = b->next; c = c->next; } } if(a != NULL) c->next = a; else if(b != NULL) c->next = b; } template<class T> void List<T>::Print() const{ //输出函数 Node<T> *p = head->next; while(p != NULL){ cout << p->data << " "; p = p->next; } cout << endl; } void TestList(){ //测试函数 List<int> L1, L2; int a[5] = {1, 3, 5, 7, 9}, b[5] = {2, 4, 6, 8, 10}; for(int i = 0; i < 5; i++) L1.Insert(i, a[i]); for(int i = 0; i < 5; i++) L2.Insert(i, b[i]); cout << "L1: "; L1.Print(); cout << "L2: "; L2.Print(); L1.Merge(L2); cout << "L1 after merge: "; L1.Print(); } int main(){ TestList(); return 0; } ```

设有两条有序链表(即 data 域元素的关键字由前往后不断增大),试设计算法,用C++将这两 条链表合并为一条新的有序链表,原链表不变。两条链表中 data 域关键字相同的元素只选 取一个存储到新的有序链表中,不同的元素都存储到新的有序链表中。 要求: ➢ 直接编写链表的友元函数完成该功能。 ➢ 链表的 data 域可存储用户自定义类对象。 ➢ 编写测试 main()函数测试线性表的正确性

下面是一个示例代码: #include <iostream> using namespace std; template<typename T> class LinkedList { friend LinkedList<T> merge(LinkedList<T>& list1, LinkedList<T>& list2) { LinkedList<T> newList; Node<T>* p1 = list1.head->next; Node<T>* p2 = list2.head->next; while(p1 != nullptr && p2 != nullptr) { if(p1->data < p2->data) { newList.insertTail(p1->data); p1 = p1->next; } else if(p1->data > p2->data) { newList.insertTail(p2->data); p2 = p2->next; } else { newList.insertTail(p1->data); p1 = p1->next; p2 = p2->next; } } while(p1 != nullptr) { newList.insertTail(p1->data); p1 = p1->next; } while(p2 != nullptr) { newList.insertTail(p2->data); p2 = p2->next; } return newList; } public: LinkedList():head(new Node<T>()) {} ~LinkedList() { Node<T>* p = head; while(p != nullptr) { Node<T>* q = p->next; delete p; p = q; } } void insertTail(T data) { Node<T>* p = head; while(p->next != nullptr) { p = p->next; } p->next = new Node<T>(data); } void print() { Node<T>* p = head->next; while(p != nullptr) { cout << p->data << " "; p = p->next; } cout << endl; } private: template<typename U> class Node { public: Node():next(nullptr) {} Node(U data):data(data), next(nullptr) {} U data; Node* next; }; Node<T>* head; }; int main() { LinkedList<int> list1, list2; list1.insertTail(1); list1.insertTail(2); list1.insertTail(4); list1.insertTail(6); list2.insertTail(2); list2.insertTail(3); list2.insertTail(5); list2.insertTail(6); LinkedList<int> newList = merge(list1, list2); newList.print(); // 1 2 3 4 5 6 return 0; }

相关推荐

最新推荐

recommend-type

线性表 实验报告.docx

选题3:(易)编写算法实现二个有序的线性表的合并问题(存储结构可选:顺序表/单链表)。 参考课件“chap002线性表.ppt”相关例题。 选题4:(难)运用单向循环链表实现约瑟夫环的问题。 参考实验指导书“实验题 4...
recommend-type

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx
recommend-type

华为OD机试D卷 - 机场航班调度程序 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

基于FPGA读取设计的心电图代码源码+全部资料齐全.zip

【资源说明】 基于FPGA读取设计的心电图代码源码+全部资料齐全.zip基于FPGA读取设计的心电图代码源码+全部资料齐全.zip 【备注】 1、该项目是高分课程设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过mac/window10/11/linux测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip

【资源说明】 基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。