您的位置:首页 > 编程语言 > C语言/C++

单链表的归并、快速排序 C++

2009-12-16 00:11 344 查看
#include <iostream>
#include <time.h>
using namespace std;

template <typename T>
struct Node
{
T data;
Node<T> *next;
};

template <typename T>
class MyLinkList
{
public:
MyLinkList();
~MyLinkList();
void insertTail(T d);
void insertHead(T d);
void reverseAll();
void mergeSort();
void quickSort();
void printAllNodes();
private:
Node<T>* mergeSort_Re(Node<T>* l,Node<T>* t);
void quickSort_Re(Node<T>* l,Node<T>* t);
private:
Node<T> *head;
Node<T> *tail;
};

template <typename T>
MyLinkList<T>::MyLinkList()
{
head=new Node<T>;
tail=head;
head->data=0;
head->next=0;
}

template <typename T>
MyLinkList<T>::~MyLinkList()
{
Node<T> *p=head;
while(p)
{
Node<T> *del=p;
p=p->next;
delete del;
}
}

template <typename T>
void MyLinkList<T>::insertTail(T d)
{
tail->next=new Node<T>;
tail=tail->next;
tail->data=d;
tail->next=0;
}

template <typename T>
void MyLinkList<T>::insertHead(T d)
{
Node<T> *p=new Node<T>;
p->data=d;
p->next=head->next;
if(head==tail)
tail=p;
head->next=p;
}

template <typename T>
void MyLinkList<T>::printAllNodes()
{
Node<T> *p=head->next;
while(p)
{
cout << p->data << ' ';
p=p->next;
}
}

template <typename T>
void MyLinkList<T>::reverseAll()
{
Node<T> *p=head->next;
tail=p;
if(p)
{
while(p->next)
{
Node<T> *temp=p->next;
p->next=temp->next;
temp->next=head->next;
head->next=temp;
}
}
}

template <typename T>
void MyLinkList<T>::mergeSort()
{
//如果要对中间的几个节点排序,则必须先记下tail->next,然后排好序后接上
head->next=mergeSort_Re(head->next,tail);
}

template <typename T>
Node<T>* MyLinkList<T>::mergeSort_Re(Node<T>* h,Node<T>* t)
{
if(h==t)
{
//一定要置零,否则归并时会出问题
h->next=0;
return h;
}
//找到中间的那个节点
Node<T>* p=h;
int nodeCount=1;
while(p!=t)
{
++nodeCount;
p=p->next;
}
p=h;
for(int i=1;i<nodeCount/2;i++)
p=p->next;

//由于在左半部的归并会使p->next改变,所以要暂时保存下
Node<T>* temp=p->next;
//左右2半,递归排序
Node<T>* h1=mergeSort_Re(h,p);
Node<T>* h2=mergeSort_Re(temp,t);

//这里开始对2个有序链表归并,返回第一个节点的指针
Node<T>* he;
if(h1->data < h2->data)
{
he=h1;
h1=h1->next;
}
else
{
he=h2;
h2=h2->next;
}
Node<T>* c=he;
while(h1&&h2)
{
if(h1->data < h2->data)
{
c->next=h1;
h1=h1->next;
}
else
{
c->next=h2;
h2=h2->next;
}
c=c->next;
}
if(h1)
c->next=h1;
if(h2)
c->next=h2;
return he;
}

template <typename T>
void MyLinkList<T>::quickSort()
{
quickSort_Re(head->next,tail);
}

template <typename T>
void MyLinkList<T>::quickSort_Re(Node<T>* h,Node<T>* t)
{
//这里都用了值交换,基本和在数组里类似,如果交换节点则复杂的多

if(h==t)
return;

//partition
Node<T> *i,*j;
i=j=h;
while(j!=t->next)
{
if(j->data < h->data)
{
i=i->next;
swap(i->data,j->data);
}
j=j->next;
}
swap(h->data,i->data);
//递归 排序
quickSort_Re(h,i);
//防止越界
if(i->next!=t->next)
quickSort_Re(i->next,t);
}

int main()
{
srand((unsigned)time(0));
MyLinkList<double> link;
for(int i=0;i<50;i++)
{
int temp=rand()%100;
cout << temp << ' ';
if(i%2)
link.insertTail(temp);
else
link.insertHead(temp);
}
cout << endl;
link.printAllNodes();
cout << endl;
link.quickSort();
link.printAllNodes();
cout << endl;
link.reverseAll();
link.printAllNodes();
cout << endl;
link.mergeSort();
link.printAllNodes();
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: