#include <iostream>
using
namespace
std;
class
Node {
public
:
int
data;
Node* prev;
Node* next;
Node(
int
data)
{
this
->data = data;
this
->prev = NULL;
this
->next = NULL;
}
};
void
print(Node* head)
{
Node* temp = head;
while
(temp != NULL) {
cout << temp->data <<
" "
;
temp = temp->next;
}
cout << endl;
}
void
push(Node*& head, Node*& tail,
int
item)
{
if
(tail == NULL) {
Node* temp =
new
Node(item);
tail = temp;
head = temp;
}
else
{
Node* temp =
new
Node(item);
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void
swap(Node*& Node1, Node*& Node2)
{
Node* temp;
temp = Node1->next;
Node1->next = Node2->next;
Node2->next = temp;
if
(Node1->next != NULL)
Node1->next->prev = Node1;
if
(Node2->next != NULL)
Node2->next->prev = Node2;
temp = Node1->prev;
Node1->prev = Node2->prev;
Node2->prev = temp;
if
(Node1->prev != NULL)
Node1->prev->next = Node1;
if
(Node2->prev != NULL)
Node2->prev->next = Node2;
}
void
sort(Node*& head, Node*& tail)
{
if
(head == tail || head == NULL)
return
;
Node* first = head;
Node* last = tail;
while
((first != last)
&& (first->prev != last)) {
if
(first->data == 0)
first = first->next;
if
(last->data == 1)
last = last->prev;
if
(first->data == 1 && last->data == 0
&& first->prev != last) {
swap(first, last);
if
(head == first)
head = last;
if
(tail == last)
tail = first;
Node* Temp = first;
first = last->next;
last = Temp->prev;
}
}
}
int
main()
{
Node* head = NULL;
Node* tail = NULL;
push(head, tail, 1);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 0);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 1);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 0);
sort(head, tail);
print(head);
return
0;
}