拷贝构造函数和赋值运算符函数怎么实现?
1 #include <iostream>2 #include <stdexcept>
3 #include <exception>
4 using namespace std;
5
6 typedef int T;
7
8 class List{
9 private:
10 struct Node{ //内部类
11 T data;
12 Node* next;
13 Node(const T& d):data(d),next(){}
14 };
15 Node *head; //头指针
16 int sz; //元素个数
17 public:
18 List(void):head(NULL),sz(0){}
19 ~List(void){
20 clear();
21 }
22 //拷贝构造函数
23 List(const List& l){
24
25 }
26 //赋值运算符函数
27 List& operator= (const List& l){
28
29 }
30 //向链表中插入一个元素
31 void insert(const T& d){
32 Node* pn = new Node(d);
33 if(head == NULL){
34 head = pn;
35 sz++;
36 return;
37 }
38 Node* p = head;
39 while(p->next){
40 p = p->next;
41 }
42 p->next = pn;
43 sz++;
44 }
45 //打印链表
46 void travel(void){
47 Node* p = head;
48 while(p){
49 cout << p->data << ' ';
50 p = p->next;
51 }
52 cout << endl;
53 }
54 //在指定位置增加元素
55 bool insert(int pos,const T& d){
56 if(pos < 0 || pos > sz){
57 return false;
58 }
59 Node* p = head;
60 Node* pn = new Node(d);
61 if(pos == 0){
62 pn->next = head;
63 head = pn;
64 sz++;
65 return true;
66 }
67 for(int i=0;i<pos-1;++i){
68 p = p->next;
69 }
70 pn->next = p->next;
71 p->next = pn;
72 sz++;
73 return true;
74 }
75 //清空链表
76 void clear(void){
77 Node* p = head;
78 while(head){
79 head = head->next;
80 delete p;
81 p = head;
82 }
83 sz = 0;
84 }
85 //删除节点
86 bool erase(int pos){
87 if(pos < 0 || pos >= sz){
88 return false;
89 }
90 Node* p = head;
91 if(pos == 0){
92 head = head->next;
93 delete p;
94 sz--;
95 return true;
96 }
97 for(int i=0;i<pos-1;++i){
98 p = p->next;
99 }
100 Node* q = p->next;
101 p->next = q->next;
102 delete q;
103 sz--;
104 return true;
105 }
106 T& at(int pos)throw(out_of_range){
107 if(pos < 0 || pos >= sz){
108 throw out_of_range("out");
109 }
110 Node* p = head;
111 for(int i=0;i<pos;++i){
112 p = p->next;
113 }
114 return p->data;
115 }
116 int size(void){
117 return sz;
118 }
119 };