iPivot]=a[j];
iPivot=j;
}
}
a[iPivot]=nPivot;
if(iPivot-ileft>1)
quick_sort(a,ileft,iPivot-1);
if(iright-iPivot>1)
quick_sort(a,iPivot+1,iright);
}
时间复杂度O(NlogN)
链表
单链表
双链表
循环链表
单链表逆置
void reverse(link *head)
{
link *p, *s, *t;
p = head;
s = p->next;
while(s->next!=NULL)
{
t = s->next;
s->next = p;
p = s;
s = t;
}
s->next = p;
head->next->next = NULL; //尾指针置为空
head->next = s; //赋值到头指针后一位
}
链表合并
Node * Merge(Node *head1 , Node *head2)
{
if ( head1 == NULL)
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}
递归方式: Node * MergeRecursive(Node *head1 , Node *head2)
{
if ( head1 == NULL )
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
head->next = MergeRecursive(head1->next,head2);
}
else
{
head = head2 ;
head->next = MergeRecursive(head1,head2->next);
}
return head ;
}
写一个Singleton模式
#include<iostream>
using namespace std;
class Singleton
{
private:
static Singleton* _instance;
protected:
Singleton()
{
cout<<”Singleton”<<endl;
}
&nb |