#include <iostream>
using namespace std;
typedef struct Node
{
int data;
struct Node *next;
} node;
int lenList(node *head)
{
if (head == NULL)
return 0;
node *p = head;
int len = 0;
while(p != NULL)
{
len++;
p = p->next;
}
return len;
}
void sortList(node *head)
{
int len = lenList(head);
cout << "len:" << len << endl;
if (len == 0)
return;
node *p = head;
int i, j, tmp;
for (i = 0; i < len - 1; i++)
{
p = head;
for (j = 0; j < len -i -1; j++)
{
if (p->data > p->next->data)
{
tmp = p->data;
p->data = p->next->data;
p->next->data = tmp;
}
p = p->next;
}
}
}
void printList(node *head)
{
node *p = head;
cout << "List: " << endl;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main(void)
{
int arr[10] = {
3, 4, 9, 1, 5, 2, 8, 0, 6, 7
};
node *head = (node *)malloc(sizeof(node));
head->data = arr[0];
head->next = NULL;
node *p = NULL;
for (int i = 0; i < 10; i++)
{
p = (node *)malloc(sizeof(node));
p->data = arr[i];
p->next = head->next;
head->next = p;
}
printList(head);
sortList(head);
printList(head);
return 0;
}