-
Notifications
You must be signed in to change notification settings - Fork 1
/
7_6_even_odd.cpp
127 lines (121 loc) · 2.78 KB
/
7_6_even_odd.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
120
121
122
123
124
125
126
127
#include <iostream>
using namespace std;
template <class T>
class LinkedList{
struct node{
T x;
node *next;
};
public:
LinkedList(){
head = NULL;
curr = NULL;
}
node* getHead(){
return head;
}
node* getCurrent(){
return curr;
}
void addNode(struct node* b){
b->next = head;
head = b;
if(curr == NULL)
curr = head;
}
void addValue(T a){
node *n = new node;
n->x = a;
n->next = head;
head = n;
if(curr == NULL)
curr = head;
}
void rewind(){
curr = head;
}
void next(){
if( curr != NULL )
curr = curr->next;
}
int getValue(){
if( curr != NULL )
return curr->x;
return 0;
}
int hasValue(){
return ( curr != NULL ? true : false );
}
void evenOddSort(){
rewind();
node *temp1, *temp2 = head->next;
int i = 1;
while(temp2->next!=NULL){
int j = i, k = i-1;
temp1 = temp2 = curr->next;
while(j && curr->next->next){
curr->next = curr->next->next;
j--;
}
while(k && temp2->next)
{
temp2 = temp2->next;
k--;
}
if(!j){
if(curr->next->next)
temp2->next = curr->next->next;
else
temp2->next = NULL;
curr->next->next = temp1;
next();
i++;
}
else
{
curr->next = temp1;
temp2->next = NULL;
}
}
rewind();
}
private:
node *head;
node *curr;
};
int main(){
LinkedList<int> L;
L.addValue(12);
L.addValue(11);
L.addValue(10);
L.addValue(9);
L.addValue(8);
L.addValue(7);
L.addValue(6);
L.addValue(5);
L.addValue(4);
L.addValue(3);
L.addValue(2);
L.addValue(1);
L.addValue(0);
L.rewind();
while(L.hasValue())
{
cout << "BEFORE----Value: " << L.getValue();
if(L.getValue()/10 < 1)
cout << " ";
cout << " Pointer: " << L.getCurrent() << endl;
L.next();
}
cout << "\n----------------------------------------------------\n" << endl;
L.evenOddSort();
while(L.hasValue())
{
cout << "AFTER-----Value: " << L.getValue();
if(L.getValue()/10 < 1)
cout << " ";
cout << " Pointer: " << L.getCurrent() << endl;
L.next();
}
return 0;
}