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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#include <iostream>
#include <algorithm>
#include <initializer_list>
class LinkedList
{
private:
struct Node
{
int data = 0;
Node* next = nullptr;
Node* prev = nullptr;
Node( int data ): next{ nullptr } , prev{ nullptr } , data{ data }
{ }
};
public:
// Default Constructor
LinkedList() {}
// Destructor
~LinkedList()
{
while( head != nullptr ) {
Node* temp = head;
head = head->next;
delete temp;
}
}
/**
* Public function that sorts the elements in this list by invoking
* the private function that accepts head pointer.
*/
void sort()
{
sort( head );
// After the merge sort, tail pointer will be pointing to incorrect node
// Update the tail (*** Need a better way to update tail ***)
Node* findTail = head;
while( findTail != nullptr ) {
if( findTail->next == nullptr ) {
tail = findTail;
}
findTail = findTail->next;
}
}
void push_back( int x )
{
if( tail == nullptr ) {
tail = new Node{ x };
head = tail;
theSize++;
}
else {
Node* temp = tail;
tail = new Node{ x };
temp->next = tail;
tail->prev = temp;
theSize++;
}
}
void display() const
{
Node* current = head;
while( current != nullptr ) {
std::cout << current->data << std::endl;
current = current->next;
}
}
private:
Node* head = nullptr;
Node* tail = nullptr;
int theSize = 0;
/**
* Perform merge sort on the this list.
* @param theHead Node* Reference to the head
*/
void sort( Node* &theHead )
{
Node* a = nullptr;
Node* b = nullptr;
// Base case
if( theHead == nullptr || theHead->next == nullptr ) {
return;
}
// Split the list in half
// For odd number of nodes, the extra node will be in the first half.
frontBackSplit( theHead , a , b );
// Recursively break the list down until the sublist contains 1 element (Sorted)
sort( a );
sort( b );
// Merge the two sorted lists
theHead = sortedMerge( a , b );
}
/**
* Take 2 Node Pointers, each pointing at the head of sorted sublist,
* merge them, and return the Node pointer to the head node of the merged list.
* @param a Node* Pointer to the head of the first sorted list
* @param b Node* Pointer to the head of the second sorted list
* @return Node* head of the merged list (nullptr if both given nodes are empty)
*/
Node* sortedMerge( Node* a , Node* b )
{
Node* headOfMerged;
// If one of the node is nullptr, return the other node
// No merging occurs this this case
if( a == nullptr ) {
return b;
}
else if( b == nullptr ) {
return a;
}
// First element in list, a, is less than the first element in b
if( a->data <= b->data ) {
headOfMerged = a;
while( b != nullptr ) {
if( a->data <= b->data ) {
if( a->next == nullptr ) {
a->next = b;
b->prev = a;
break;
}
a = a->next;
}
else {
Node* toAdd = b;
b = b->next;
toAdd->prev = a->prev;
a->prev->next = toAdd;
toAdd->next = a;
a->prev = toAdd;
}
}
}
// First element in list, b, is less than the first element in a
else {
headOfMerged = b;
while( a != nullptr ) {
if( b->data <= a->data ) {
if( b->next == nullptr ) {
b->next = a;
a->prev = b;
break;
}
b = b->next;
}
else {
Node* toAdd = a;
a = a->next;
toAdd->prev = b->prev;
b->prev->next = toAdd;
toAdd->next = b;
b->prev = toAdd;
}
}
}
return headOfMerged;
}
/**
* Utility function for merge sort
* @param frontRef Node* reference pointer that will point to the head of first sorted half
* @param backRef Node* reference pointer that will point to the head of second sorted half
*/
void frontBackSplit( Node* theHead , Node* &frontRef , Node* &backRef )
{
Node* fast;
Node* slow;
// If the list is empty, both front and back points to null
// If there is only one element, front points to it and back points
// to null.
if( theHead == nullptr || theHead->next == nullptr ) {
frontRef = theHead;
backRef = nullptr;
}
else {
slow = theHead;
fast = theHead->next;
// Fast advances 2 nodes while slow advances 1 node
while( fast != nullptr ) {
fast = fast->next;
if( fast != nullptr ) {
slow = slow->next;
fast = fast->next;
}
}
// slow should be pointing right before midpoint. Split at this point
frontRef = theHead;
backRef = slow->next;
// Update the prev and next pointers
backRef->prev = nullptr;
slow->next = nullptr;
}
}
};
// Test Driver to test operations of LinkedList
int main()
{
int n;
std::cin >> n;
LinkedList list1;
for( int i = 0 ; i < n ; i++ ) {
int input;
std::cin >> input;
list1.push_back( input );
}
list1.sort();
list1.display();
}
|