-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1203.cpp
More file actions
executable file
·125 lines (121 loc) · 2.6 KB
/
Copy path1203.cpp
File metadata and controls
executable file
·125 lines (121 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* 链表合并
**/
#include <iostream>
using namespace std;
template <typename T>
struct node {
node *next;
T data;
};
template <class T>
class list {
private:
node<T> *head, *rear;
void init() {
head = rear = new node<T>;
}
public:
list() {init();}
list(const list &obj) {
node<T> *p = obj.head->next;
while(p != NULL) {
node<T> *q = new node<T>;
q->data = p->data;
rear->next = q;
rear = q;
p = p->next;
}
}
~list() {
node<T> *p = head->next;
while(p != NULL) {
node<T> *q = p;
p = p->next;
delete q;
}
delete head;
}
void input(int n) {
T tmp;
for(int i = 0;i < n;++ i) {
cin >> tmp;
node<T> *p;
p = new node<T>;
p->data = tmp;
rear->next = p;
rear = p;
}
rear->next = NULL;
return;
}
void output() {
node<T> *p = head->next;
while(p != NULL) {
cout << p->data << " ";
node<T> *q = p;
p = p->next;
}
return;
}
void copy(const list &obj) {
node<T> *p = obj.head->next;
while(p != NULL) {
node<T> *q = new node<T>;
q->data = p->data;
rear->next = q;
rear = q;
p = p->next;
}
rear->next = NULL;
}
list<T> operator+ (const list<T> &obj) {
list<T> L;
node<T> *p1 = head->next;
while(p1 != NULL) {
node<T> *q1 = new node<T>;
q1->data = p1->data;
L.rear->next = q1;
L.rear = q1;
p1 = p1->next;
}
node<T> *p2 = obj.head->next;
while(p2 != NULL) {
node<T> *q2 = new node<T>;
q2->data = p2->data;
L.rear->next = q2;
L.rear = q2;
p2 = p2->next;
}
L.rear->next = NULL;
return L;
}
};
int main() {
char s[10];
cin >> s;
int n, m;
if(s[0] == 'i') {
list<int> L1, L2, L;
cin >> n >> m;
L1.input(n); L2.input(m);
L.copy(L1 + L2);
L.output();
}
if(s[0] == 'c') {
list<char> L1, L2, L;
cin >> n >> m;
L1.input(n); L2.input(m);
L.copy(L1 + L2);
L.output();
}
if(s[0] == 'd') {
list<double> L1, L2, L;
cin >> n >> m;
L1.input(n); L2.input(m);
L.copy(L1 + L2);
L.output();
}
cout << endl;
return 0;
}