请为以下程序中的List类写一个复制构造函数#include<iostream> #include<fstream> #include <string>; using…
- User
请为以下程序中的List类写一个复制构造函数#include #include #include ; using namespace std; class student { int id; string name; int score; public: student(int i = 0, string n = "\0", int s = 0); void show(); bool operator<(student stu); bool operator!=(student stu); void input(int i = 0, string n = "\0", int s = 0); friend ostream& operator<<(ostream& out, student stu); void operator=(const student stu);
}; void student:: operator=(const student stu) { id = stu.id; name = stu.name; score = stu.score; } ostream& operator<<(ostream& out, student stu) { out << stu.id << '\t' << stu.name << '\t' << stu.score << endl; return out; } void student::input(int i, string n, int s) { id = i; name = n; score = s; } student::student(int i, string n, int s) { id = i; name = n; score = s; } void student::show() { cout << "id= " << id << " name= " << name << " score= " << score << endl; } bool student:: operator<(student stu) { return(score < stu.score); } bool student:: operator!=(student stu) { return (score != stu.score); } template class List; template class Node { T info; Node* link; public: Node(); Node(const T& i); friend class List; }; template Node::Node() { link = NULL; } templateNode::Node(const T& i) { info = i; link = NULL; } templateclass List { Node* head, * tail; public: List(); List(List& lis); ~List(); void MakeEmpty(); Node* CreatNode(T data); void InsertFront(Node* p); void PrintList(); void InsertRear(Node* p); }; template void List::PrintList() { Node* temp = head->link; while (temp!= 0) { cout << temp->info; temp = temp->link; } cout << endl; } templatevoid List:: InsertRear(Node* p){ tail->link = p; tail = p; p->link = NULL; } template void List::InsertFront(Node* p) { p->link = head->link; head->link = p; if (head == tail) tail = p; } template Node* List::CreatNode(T data) { Node* temp = new Node(data); return temp; } /template List::List(List& lis) { head = tail = new Node(); T inf; List temp; Node pnode; while (lis.head != NULL) { inf = lis.head->link->info; pnode = lis.CreatNode(inf); temp.InsertFront(pnode); lis.head->link = lis.head->link->link; } this = temp; }/
template List::List() { head = tail = new Node(); } templatevoid List::MakeEmpty() { Node* temp; while (head->link != 0) { temp = head->link; head->link = temp->link; delete temp; } tail = head; } templateList::~List() { MakeEmpty(); delete head; }
void main() { student st; Node* pnode; List list1, list2; //读入文件中数据建立链表list1,list2; ifstream ifile; ifile.open("F:\C++\c++\5 10\studata.txt"); int i, s; char n[10]; while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list1.CreatNode(st); list1.InsertFront(pnode); } ifile.close(); ifile.open("F:\C++\c++\5 10\studata.txt"); while (ifile.eof() == 0) { ifile >> i >> n >> s; st.input(i, n, s); pnode = list2.CreatNode(st); list2.InsertRear(pnode); } ifile.close(); //list1,list2 建立成功,输出查看。 list1.PrintList(); list2.PrintList();
cout << "用list构造list3\n"; List <student> list3(list1); //拷贝构造函数先建立一个头结点,再以原链表的各结点的数据域来构造新链表的各对应结点。 list3.PrintList(); /*cout << "请输入删除结点的学号:\n"; int number; cin >> number; st.putkey(number); pnode = list1.Find(st); if (pnode) { pnode = list1.DeleteNode(pnode); delete pnode; list1.PrintList(); cout << "list1 长度:" << list1.Length() << endl; } else cout << "未找到!\n"; cout << "清空list1,按分数从大到小的顺序重建list1" << endl; list1.MakeEmpty(); ifile.clear(0); ifile.seekg(0); //读入文件数据,重建list1 .................. pnode = list1.CreatNode(st); list1.InsertOrder(pnode); list1.PrintList(); cout << "将链表list1逆序\n"; //要求不删除原结点,也不另建一个链表来取代, // 而是通过改变指针域的链接方向来逆转链表。 list1.Reverse(); list1.PrintList(); cout << "用list3赋值给list1\n"; list1 = list3; list1.PrintList();*/}
- Assistant
复制构造函数的写法和拷贝构造函数有些类似,需要在类中进行定义,在参数列表中传入一个同类型的引用来进行初始化。具体实现可以按照以下步骤:
- 初始化一个空的头结点;
- 通过遍历原链表,创建一个新结点并将原结点的数据复制到新结点中,然后插入到新的链表尾部;
- 最终返回新建链表。
代码实现如下:
template List::List(List& lis) { head = tail = new Node(); Node* iter = lis.head->link; // 原链表中的第一个结点 while(iter != nullptr) { Node* newNode = new Node(iter->info); // 复制原结点数据 InsertRear(newNode); // 插入到新链表尾部 iter = iter->link; // 遍历下一个结点 } }
需要注意的是,在复制结点数据时需要确保类型支持复制构造函数或重载了赋值运算符。如果是自定义类型,需要按照题目给出的方式进行实现。