#include <iostream>
using
namespace
std;
struct
Node {
int
data;
Node* next;
} Node;
struct
Node* swap(
struct
Node* ptr1,
struct
Node* ptr2)
{
struct
Node* tmp = ptr2->next;
ptr2->next = ptr1;
ptr1->next = tmp;
return
ptr2;
}
void
bubbleSort(
struct
Node** head,
int
count)
{
struct
Node** h;
int
i, j, swapped;
for
(i = 0; i <= count; i++) {
h = head;
swapped = 0;
for
(j = 0; j < count - i - 1; j++) {
struct
Node* p1 = *h;
struct
Node* p2 = p1->next;
if
(p1->data > p2->data) {
*h = swap(p1, p2);
swapped = 1;
}
h = &(*h)->next;
}
if
(swapped == 0)
break
;
}
}
void
printList(
struct
Node* n)
{
while
(n != NULL) {
cout <<
char
(n->data) <<
" -> "
;
n = n->next;
}
cout <<
"NULL"
<< endl;
}
void
insert(
struct
Node** head,
int
data)
{
struct
Node* ptr =
new
struct
Node();
ptr->data = data;
ptr->next = NULL;
if
(*head == NULL) {
*head = ptr;
}
else
{
struct
Node* ptr1 = *head;
while
(ptr1->next != NULL) {
ptr1 = ptr1->next;
}
ptr1->next = ptr;
}
}
int
main()
{
int
arr[] = {
'b'
,
'b'
,
'c'
,
'c'
,
'd'
,
'e'
,
'f'
,
'b'
,
'b'
,
'a'
,
'a'
};
int
list_size, i;
struct
Node* start = NULL;
list_size =
sizeof
(arr) /
sizeof
(arr[0]);
for
(i = 0; i < list_size; i++)
insert(&start, arr[i]);
bubbleSort(&start, list_size);
printList(start);
return
0;
}