-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLSort0s1s2s.cpp
More file actions
190 lines (166 loc) · 3.45 KB
/
LLSort0s1s2s.cpp
File metadata and controls
190 lines (166 loc) · 3.45 KB
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
190
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
~Node()
{
int value = this->data;
// memory free
if (this->next != NULL)
{
delete next;
this->next = NULL;
}
cout << "Memory is free for node with data " << value << endl;
}
};
void insertAtHead(Node *&head, int val)
{
Node *temp = new Node(val);
temp->next = head;
head = temp;
}
void insertAtTail(Node* &tail, int val)
{
Node *temp = new Node(val);
tail->next = temp;
tail = temp;
}
void insertAtPosition(Node *&head, Node* &tail, int pos, int value)
{
if (pos == 1)
{
insertAtHead(head, value);
return;
}
Node *node = new Node(value);
Node *temp = head;
int cnt = 1;
while (cnt < pos - 1)
{
temp = temp->next;
cnt++;
}
if (temp->next == NULL)
{
insertAtTail(tail, value);
return;
}
node->next = temp->next;
temp->next = node;
}
void sort012(Node* &head){
if(head == NULL){
return;
}
int zeroCount = 0;
int oneCount = 0;
int twoCount = 0;
Node* temp = head;
while(temp != NULL){
if(temp->data == 0){
zeroCount++;
}
else if(temp->data == 1){
oneCount++;
}
else if(temp->data == 2){
twoCount++;
}
temp = temp->next;
}
temp = head;
while(temp != NULL){
if(zeroCount != 0){
temp->data = 0;
zeroCount--;
}
else if(oneCount != 0){
temp->data = 1;
oneCount--;
}
else if(twoCount != 0){
temp->data = 2;
twoCount--;
}
temp = temp->next;
}
}
void sort2(Node *&head){
Node* zeroHead = new Node(-1);
Node* zeroTail = zeroHead;
Node* oneHead = new Node(-1);
Node* oneTail = oneHead;
Node* twoHead = new Node(-1);
Node* twoTail = twoHead;
Node* temp = head;
while(temp != NULL){
if(temp->data == 0){
insertAtTail(zeroTail,0);
}
else if(temp->data == 1){
insertAtTail(oneTail,1);
}
else if(temp->data == 2){
insertAtTail(twoTail,2);
}
temp = temp->next;
}
if(oneHead->next != NULL){
zeroTail->next = oneHead->next;
}
else{
zeroTail->next = twoHead->next;
}
oneTail->next = twoHead->next;
twoTail->next = NULL;
head = zeroHead->next;
oneHead->next = NULL;
zeroHead->next = NULL;
twoHead->next = NULL;
delete oneHead;
delete zeroHead;
delete twoHead;
}
void print(Node *&head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int main(int argc, char const *argv[])
{
Node *node1 = new Node(1);
// cout << node1->data << endl;
// cout << node1->next << endl;
Node *head = node1;
print(head);
insertAtHead(head, 0);
print(head);
insertAtHead(head, 1);
print(head);
Node *tail = node1;
insertAtTail(tail, 2);
print(head);
insertAtTail(tail, 2);
print(head);
insertAtPosition(head, tail, 6, 0);
insertAtTail(tail, 2);
print(head);
// sort012(head);
sort2(head);
print(head);
return 0;
}