线性表:删除,分离,移动
文件名:
del.cpp
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
#include <iostream>
#include <sstream>
using namespace std;
typedef struct Num {
int data;
struct Num *next;
} Num,*Nums;
typedef struct Node {
Nums head;
Nums tail;
int length=0;
} Node,*Nodes;
int init(Node &node) {
node.head=new Num;
node.head->next=nullptr;
node.tail=node.head;
return 0;
}
int TailInsert(Node &node,int num) {
node.tail->next=new Num;
node.tail=node.tail->next;
node.tail->data=num;
node.tail->next=nullptr;
node.length++;
return 0;
}
int del(Node &node) {
Nums current=node.head->next;
while(current&¤t->next) {
if(current->data==current->next->data) {
if(current->next==node.tail) {
node.tail=current;
}
Nums t=current->next;
current->next=t->next;
delete t;
node.length--;
}
else current=current->next;
}
return 0;
}
/*int print(Nodes &node) {
Nums current=node->head->next;
while(current) {
cout<<current->data<<" ";
current=current->next;
}
return 0;
}*/
int des(Node &node) {
Nums current=node.head;
while(current) {
Nums t=current;
current=current->next;
delete t;
}
return 0;
}
int main() {
Node node;
init(node);
int num;
string line;
if(getline(cin,line)) {
istringstream iss(line);
while(iss>>num) {
TailInsert(node,num);
}
}
del(node);
//print(node);
cout<<node.length;
des(node);
return 0;
}
文件名:
split.cpp
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
#include <iostream>
using namespace std;
typedef struct Num {
int data;
Num* next;
} Num,*Nums;
typedef struct Node {
Nums head;
Nums tail;
} Node,*Nodes;
void init(Node &node) {
node.head=new Num;
node.head->next=nullptr;
node.tail=node.head;
}
void TailInsert(Node &node,int num) {
node.tail->next=new Num;
node.tail=node.tail->next;
node.tail->data=num;
node.tail->next=nullptr;
}
void split(Node &node, int n, Node &less, Node &greater) {
Nums current = node.head->next;
while (current != NULL) {
if (current->data < n) {
TailInsert(less, current->data);
} else {
TailInsert(greater, current->data);
}
current = current->next;
}
}
void printList(Node &node) {
Nums current = node.head->next;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
}
void des(Node &node) {
Nums current=node.head;
while(current) {
Nums t=current;
current=current->next;
delete t;
}
}
int main() {
Node node, less, greater;
init(node);
init(less);
init(greater);
int length;
cin>>length;
for (int i = 0; i < length; i++) {
int num;
cin>>num;
TailInsert(node, num);
}
int n;
cin>>n;
split(node, n, less, greater);
printList(less);
printList(greater);
des(node);
des(less);
des(greater);
return 0;
}
文件名:
move.cpp
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
#include <iostream>
using namespace std;
typedef struct Num {
int data;
struct Num* next;
} Num,*Nums;
typedef struct Node {
Nums head;
Nums tail;
int length;
} Node,*Nodes;
void init(Node &node) {
node.head=new Num;
node.head->next=node.head;
node.tail=node.head;
node.length=0;
}
void TailInsert(Node &node,int num) {
Nums newNode=new Num;
newNode->data=num;
newNode->next=node.head;
node.tail->next=newNode;
node.tail=newNode;
node.length++;
}
void move(Node &node,int k) {
k=k%node.length;
Nums newTail=node.head->next;
for(int i=0;i<node.length-k-1;++i) {
newTail=newTail->next;
}
node.tail->next=node.head->next;
node.head->next=newTail->next;
newTail->next=node.head;
node.tail=newTail;
}
void print(Node &node) {
Nums current=node.head->next;
for(int i=0;i<node.length;i++) {
cout<<current->data<<" ";
current=current->next;
}
}
void des(Node &node) {
Nums current=node.head;
for(int i=0;i<node.length;i++) {
Nums t=current;
current=current->next;
delete t;
}
}
int main() {
Node node;
init(node);
int n,num,k;
cin>>n;
for(int i=0;i<n;i++) {
cin>>num;
TailInsert(node,num);
}
cin>>k;
move(node,k);
print(node);
des(node);
return 0;
}