-
Notifications
You must be signed in to change notification settings - Fork 0
/
test21.cpp
86 lines (75 loc) · 1.44 KB
/
test21.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
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<ctype.h>
#include<stdio.h>
using namespace std;
struct Node {
char val;
Node* next;
Node(int n, Node* next_ = nullptr) :
val(n), next(next_) {}
};
void addNode(Node* head, int n)
{
Node* p = head;
Node* newNode = new Node{ n, nullptr };
while (p->next)
{
p = p->next;
}
newNode->val = n;
newNode->next = nullptr;
p->next = newNode;
}
Node* crateList()
{
Node* list = new Node{ -1, nullptr };
for (int i = 1; i < 10; i++)
{
addNode(list, rand() % 10 + '0');
addNode(list, rand() % 26 + 'a');
}
return list;
}
void printNode(Node* head)
{
Node* p = head->next;
while (p)
{
std::cout << p->val << ' ';
p = p->next;
}
std::cout << endl;
}
void splitList(Node* head, Node* head1, Node* head2) {
Node* p = head->next;
Node* p1 = head1;
Node* p2 = head2;
while (p != nullptr) {
if (isalpha(p->val)) {
p1->next = p;
p1 = p;
}
else if (isdigit(p->val)) {
p2->next = p;
p2 = p;
}
p = p->next;
}
p1->next = NULL;
p2->next = NULL;
}
int main()
{
Node l1{' '},
l2{' '},
* l3{};
l3 = crateList();
printNode(l3);
splitList(l3, &l1, &l2);
printNode(&l1);
printNode(&l2);
return 0;
}