-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-1
137 lines (125 loc) · 2.52 KB
/
7-1
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
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <math.h>
using namespace std;
class Sorted_prime
{
/*
*Default constructor and a user defined constructor.
*/
public:
Sorted_prime (){val=0;next=NULL;}
Sorted_prime(int n):val(n),next(NULL){}
Sorted_prime(int n,Sorted_prime* _next):val(n),next(_next){}
/*a static method to check if a value is a prime number.*/
static bool Is_prime(int);
/*insert a new node and the linked list must maintained as sorted order.The head is the smallest*/
bool insert(int);
/*return the pointer to it's next node*/
Sorted_prime *Get_next();
/*output the list elements */
void Print_out();
/*clear memory after the prime linked list is no longer being used.*/
~Sorted_prime();
private:
int val;// element value
Sorted_prime *next;//sorted as i linked list ,each contains one element and one pointer to it's next node.
};
Sorted_prime* head=NULL;//the global pointer as the first pointer .
/*--------Sorted_prime complement---------------*/
bool Sorted_prime::Is_prime(int n)
{
if(n<-10000||n>10000)//check if the numder locates in the right range.
return false;
if(n<2) // the smallest prime numer is 2.
return false;
int tmp=sqrt(n);
int i;
for(i=2;i<=tmp;i++)
{
if(n%i==0)
return false;
}
return true;
}
Sorted_prime* Sorted_prime::Get_next()
{
return next;
}
bool Sorted_prime::insert(int n)
{
Sorted_prime * prime=new Sorted_prime(n);
if(head==NULL)
{
head=prime;
return true;
}
if(prime->val<head->val)
{
prime->next=head;
head=prime;
return true;
}
if(prime->val==head->val)
{
return true;
}
else
{
Sorted_prime* pcur;
Sorted_prime * pre;
pre=head;
for(pcur=head->next;pcur!=NULL;pcur=pcur->next,pre=pre->next)
{
if(prime->val==pcur->val)
return true;
if(prime->val<pcur->val)
{
pre->next=prime;
prime->next=pcur;
return true;
}
}
//insert as the last one
pre->next=prime;
}
return true;
}
void Sorted_prime::Print_out()
{
if(head==NULL)
return;
Sorted_prime* q;
for(q=head;q->next!=NULL;q=q->next)
cout<<q->val<<' ';
//output the last one
cout<<q->val;
}
Sorted_prime::~Sorted_prime()
{
Sorted_prime* q;
Sorted_prime* qnext;
for(q=head;q->next!=NULL;q=q->next)
{
qnext=q->next;
delete q;
q=qnext->next;
}
head=NULL;
}
/*test program*/
int main()
{
int num;
Sorted_prime *qprime=new Sorted_prime();
Begin:
if( cin>>num);
{
if(Sorted_prime::Is_prime(num))
{
qprime->insert(num);
goto Begin;
}
}
qprime->Print_out();
return 0;
}