#include<iostream>
#include<cstdlib>
using namespace std;
class NUM
{
public:
int n;
int a[5];
NUM(int x=0)
{
for (int i = 0; i < 5; i++)
a[i] = 0;
n = x;
};
void descrease();
};
void NUM::descrease()
{
for (int i = 0; i < 5; i++)
{
a[i] = n % 10;
n /= 10;
}
}
typedef struct LNode
{
int data;
struct LNode* next;
}LinkNode;
void DestroyList(LinkNode*& L)
{
LinkNode* pre = L, * p = L->next;
while (p != NULL)
{
free(pre);
pre = p;
p = pre->next;
}
free(pre);
}
void Display(LinkNode* L)
{
LinkNode* p = L->next;
while (p != NULL)
{
cout << p->data;
p = p->next;
}
cout << endl;
}
void CreateList(LinkNode*& L,int a[])
{
LinkNode* s, * r;
L = (LinkNode*)malloc(sizeof(LinkNode));
r = L;
for(int i=0;i<5;i++)
{
s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = a[i];
r->next = s;
r = s;
}
r->next = NULL;
}
void sort(LinkNode*& L)
{
LinkNode* p, * pre, * q;
pre = L;
while (pre->next!= NULL)
{
p = pre->next->next;
q = p->next;
while (p != NULL)
{
if (pre->next->data < p->data)
{
p->next = pre->next;
pre->next->next = q;
pre->next=p;
}
else
{
p = q;
q = p->next;
}
}
pre = pre->next;
}
}
int main()
{
LinkNode* L;
int n;
cout << "Input n:" << endl;
cin >> n;
NUM num(n);
num.descrease();
CreateList(L,num.a);
Display(L);
sort(L);
cout << "显示排序后的链表:" << endl;
Display(L);
DestroyList(L);
return 0;
}