#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node(int d = int()) :data(d), next(NULL){}
};
class List
{
public:
List()
{
first = NULL;
}
void Insert(int a[], int n)
{
Node *p = first;
for (int i = 0; i < n; i++)
{
Node *s = new Node(a[i]);
if (p == NULL)
{
p = s;
first = p;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
s->next = p->next;
p->next = s;
}
}
}
void Sort()
{
Sort(NULL, first);
}
void Sort(Node *p, Node *t)
{
if (t->next == NULL)
{
first = t;
return;
}
else
{
Node *q = t->next;
Sort(t, q);
if (p == NULL)
{
q->next = t;
t->next = NULL;
return;
}
q->next = t;
}
}
//递归逆序单链表。
void Printf()
{
Node *p = first;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
private:
Node *first;
};
int main()
{
List list;
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Insert(a,sizeof(a)/sizeof(int));
list.Sort();
list.Printf();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/liuhuiyan_2014/article/details/46876879