-
Notifications
You must be signed in to change notification settings - Fork 0
/
Odd&Even.cpp
119 lines (88 loc) · 2.17 KB
/
Odd&Even.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
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
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace std;
class List
{
class Node
{
public:
int num;
Node *address=NULL;
public:
Node(int pnum)
{
num = pnum;
}
};
public:
//Real definition of List
short Nodeamount=0;
private: Node *head = NULL;
public:
Node* getHead()
{
return head;
}
void setHead(Node* nod)
{
head = nod;
}
void addElement(int n)
{
Node *nod = new Node(n);
Nodeamount++;
if(head==NULL)
{
setHead(nod);
}else
{
Node *reader;
for(reader=head; reader->address!=NULL; reader=reader->address);
reader->address = nod;
}
}
void oddeven()
{
List odd;
List even;
for(Node *reader=head; reader!=NULL; reader=reader->address)
{
if(reader->num%2!=0)
{
odd.addElement(reader->num);
}else
{
even.addElement(reader->num);
}
}
if(odd.Nodeamount>0)
{
cout<<"Odd numbers: "<<odd.Nodeamount<<endl;
odd.printList();
}
if(even.Nodeamount>0)
{
cout<<"Even numbers: "<<even.Nodeamount<<endl;
even.printList();
}
}
void printList()
{
Node *reader = head;
for(int i=0; i<Nodeamount; i++)
{
cout<<reader->num<<endl;
reader = reader->address;
}
}
};
int main() {
List a;
for(int i=0; i<50; i++)
{
a.addElement((rand()%500)+1);
}
a.oddeven();
return 0;
}