-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort0s1s2s.cpp
189 lines (162 loc) · 4.18 KB
/
sort0s1s2s.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <unordered_map>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data = 0){
this->data = data;
this->next = NULL;
}
~Node(){
cout<<"Deleted Node: "<<this->data<<endl;
this->next = NULL;
delete next;
}
};
void print(Node* &head){
Node* temp = head;
while(temp){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
// Counting Approach ( using data replacement )
void sort0s1s2s(Node* &head){
// If LL is empty
if(head == NULL)
return;
// If single node is present
if(head->next == NULL)
return ;
int zeros = 0;
int ones = 0;
int twos = 0;
Node* temp = head;
// 1] Find count of zeros , ones & twos
while(temp){
if(temp->data == 0)
zeros++;
else if(temp->data == 1)
ones++;
else if(temp->data == 2)
twos++;
temp = temp->next;
}
// 2] fill LL with count of zeros , ones & twos
temp = head;
// fill zeros
while(zeros--){
temp->data = 0;
temp = temp->next;
}
// fill ones
while(ones--){
temp->data = 1;
temp = temp->next;
}
// fill twos
while(twos--){
temp->data = 2;
temp = temp->next;
}
}
// sorting without data replacement ( links are replaced here )
// It's more preferable & advisable
Node* sort0s1s2s_01(Node* &head){
// If LL is empty
if(head == NULL)
return NULL;
// If single node is present
if(head->next == NULL)
return head;
// 1] Create Dummy Nodes
Node* zeroHead = new Node(-1);
Node* zeroTail = zeroHead;
Node* oneHead = new Node(-1);
Node* oneTail = oneHead;
Node* twoHead = new Node(-1);
Node* twoTail = twoHead;
// 2] Traverse the LL
Node* curr = head;
while(curr){
if(curr->data == 0){
// Take out zero wali node
Node* temp = curr;
curr = curr->next;
temp->next = NULL;
// append that node In zeroHead LL
zeroTail->next = temp;
zeroTail = temp;
}
else if(curr->data == 1){
// Take out one wali node
Node* temp = curr;
curr = curr->next;
temp->next = NULL;
// append that node In oneHead LL
oneTail->next = temp;
oneTail = temp;
}
else if(curr->data == 2){
// Take out two wali node
Node* temp = curr;
curr = curr->next;
temp->next = NULL;
// append that node In twoHead LL
twoTail->next = temp;
twoTail = temp;
}
}
// Now we are ready with LLs of zeros , ones & twos
// 3] Modifiy one wali & two wali dummy nodes
Node* temp = oneHead;
oneHead = oneHead->next;
delete temp;
temp = twoHead;
twoHead = twoHead->next;
delete temp;
// 4] Join all three LLs
if(oneHead != NULL){
// one wali LL is not empty
zeroTail->next = oneHead;
if(twoHead != NULL){
oneTail->next = twoHead;
}
}
else{
// one wali LL is empty
if(twoHead != NULL)
zeroTail->next = twoHead;
}
// 4] Remove zero wali dummy Nodes
temp = zeroHead;
zeroHead = zeroHead->next;
delete temp;
// 5] bring head at it's right pos & return head
head = zeroHead;
return head;
}
int main()
{
Node* head = new Node(2);
Node* first = new Node(2);
Node* second = new Node(2);
Node* third = new Node(2);
Node* forth = new Node(2);
Node* fifth = new Node(2);
Node* sixth = new Node(2);
head->next = first;
first->next = second;
second->next = third;
third->next = forth;
forth->next = fifth;
fifth->next = sixth;
sixth->next = NULL;
print(head);
head = sort0s1s2s_01(head);
print(head);
return 0;
}