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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#include <iostream>
using namespace std;
struct Node
{
char name[20];
int age;
double height;
Node *next;
};
Node *start_ptr = NULL;
// list: the list to be sorted.
// less_than: a pointer to a function which compares two nodes.
// returns: a pointer to the sorted list.
Node* mergesort(Node* list, bool(*less_than)(Node*, Node*)) ;
// the function we're using to compare two nodes.
bool age_is_less( Node* a, Node* b )
{
return a->age < b->age ;
}
// list: The list to be appended to.
// add_me: the list or item to be appended.
void append(Node*& list, Node*add_me)
{
if ( list == 0 )
list = add_me ;
else
{
Node * current = list ;
while ( current->next )
current = current->next ;
current->next = add_me ;
}
}
// list: The list
// returns: number of items in list.
unsigned length(Node* list)
{
if ( !list )
return 0 ;
unsigned i=1 ;
while ( (list = list->next) )
++i ;
return i ;
}
// list: The list.
// nth: the number of node we're looking for.
// returns: a pointer to the nth node in list.
Node* nth_node( Node* list, unsigned nth )
{
Node* current = list ;
unsigned i=0;
while ( i++ < nth )
current = current->next;
return current ;
}
// list_a: one of the (sorted) lists to be merged
// list_b: one of the (sorted) lists to be merged.
// less_than: the comparison function for determing the order of Nodes.
// returns: a list consisting of the elements of both list_a and list_b in
// the order determined by less_than.
// This function is really an implementation detail of mergesort.
Node* merge(Node* list_a, Node* list_b, bool(*less_than)(Node*,Node*)) ;
std::ostream& operator<<(std::ostream& os, const Node* list)
{
os << "{ " ;
const Node* current = list ;
while ( current )
{
os << current->age << ' ' ;
current = current->next ;
}
return os << "}" ;
}
Node* mergesort(Node* list, bool(*less_than)(Node*, Node*))
{
std::cout << "mergesort entered: " << list << '\n' ;
if ( !list || !list->next )
return list ;
unsigned len = length(list) ;
Node* left_list = list ;
Node* nth = nth_node(left_list, (len/2)-1) ;
Node* right_list = nth->next ;
nth->next = 0 ;
std::cout << "\tsplit: " << left_list << " + " << right_list << '\n' ;
left_list = mergesort(left_list, less_than) ;
std::cout << '\n' ;
right_list = mergesort(right_list, less_than) ;
std::cout << '\n' ;
std::cout << "\tsorted: " << left_list << " + " << right_list << '\n' ;
return merge(left_list, right_list, less_than) ;
}
Node* merge( Node* list_a, Node* list_b, bool (*less_than)(Node*,Node*) )
{
Node* result=0 ;
while ( list_a || list_b )
{
if ( list_a && list_b )
{
if ( less_than(list_a, list_b) )
{
Node * next = list_a->next ;
list_a->next = 0 ;
append(result, list_a) ;
list_a = next ;
}
else
{
Node* next = list_b->next ;
list_b->next = 0 ;
append(result, list_b) ;
list_b = next ;
}
}
else if ( list_a )
{
Node* next = list_a->next ;
append(result,list_a) ;
list_a = 0 ;
}
else
{
Node* next = list_b->next ;
append(result, list_b) ;
list_b = 0 ;
}
}
return result ;
}
void add_node_at_end ()
{
Node *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new Node;
cout << "Please enter the name of the person: ";
cin >> temp->name;
cout << "Please enter the age of the person : ";
cin >> temp->age;
cout << "Please enter the height of the person : ";
cin >> temp->height;
cout << endl;
temp->next = NULL;
// Set up link to this node
if (start_ptr == NULL)
start_ptr = temp;
else
{
temp2 = start_ptr; // We know this is not NULL - list not empty!
while (temp2->next != NULL)
temp2 = temp2->next; // Move to next link in chain
temp2->next = temp;
}
}
void delete_start_node()
{
Node *temp;
temp = start_ptr;
start_ptr = start_ptr->next;
delete temp;
}
void delete_end_node()
{
Node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{
temp1 = start_ptr;
if (temp1->next == NULL)
{
delete temp1;
start_ptr = NULL;
}
else
{
while (temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
}
}
void sort_age()
{
start_ptr = mergesort(start_ptr, age_is_less) ;
}
void print_node()
{
Node *temp;
temp = start_ptr;
if(temp == NULL) cout << "Empty List!" << endl;
while(temp != NULL)
{
if(temp == NULL) cout << "Empty List!" << endl;
cout << "Name : " << temp->name << endl;
cout << "Age : " << temp->age << endl;
cout << "Height : " << temp->height << endl;
cout << endl;
temp = temp->next;
}
}
int main()
{
int n;
cout << "n= ";
cin >> n;
for(int i = 0; i <= n-1; i++)
{
add_node_at_end();
}
print_node();
cout << "---------------------------------------------" << endl;
sort_age();
print_node();
system("pause");
return 0;
}
|