锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久综合狠狠综合久久综合88,久久综合九色综合精品,精品久久久久久国产牛牛apphttp://www.shnenglu.com/kingoal/Algorithm Study using C++zh-cnWed, 07 May 2025 01:40:06 GMTWed, 07 May 2025 01:40:06 GMT60鍗氬縐昏嚦http://libiao.appspot.comhttp://www.shnenglu.com/kingoal/archive/2009/07/26/91279.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusSun, 26 Jul 2009 14:12:00 GMThttp://www.shnenglu.com/kingoal/archive/2009/07/26/91279.htmlhttp://www.shnenglu.com/kingoal/comments/91279.htmlhttp://www.shnenglu.com/kingoal/archive/2009/07/26/91279.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/91279.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/91279.html鍗氬縐昏嚦http://libiao.appspot.com,涓撴敞浜?span>涓撴敞浜嶧reeBSD,C/C++, Python,綆楁硶鍜屾湇鍔″櫒寮鍙?/span>

Kingoal Lee's Alogrithm Study using cplusplus 2009-07-26 22:12 鍙戣〃璇勮
]]>
鍗曢摼琛ㄧ殑緲昏漿http://www.shnenglu.com/kingoal/archive/2007/09/05/31623.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusWed, 05 Sep 2007 06:41:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/09/05/31623.htmlhttp://www.shnenglu.com/kingoal/comments/31623.htmlhttp://www.shnenglu.com/kingoal/archive/2007/09/05/31623.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/31623.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/31623.html鍏蜂綋浠g爜涓猴細
#include <iostream>

using namespace std;

#ifndef 
null
#define 
null (void*)0
#endif

typedef struct node
{
    struct node
* next;
    
int data;
}node;

node
* head=(node*)null;

void reverse(node* root)
{
    node 
*cur,*pre,*next;
    
    pre
=(node*)null;
    cur
=root;
    next
=cur->next;
    
    
while(next)
    {
        cur
->next=pre;
        pre
=cur;
        cur
=next;
        next
=cur->next;
    }
    
    head
=cur;
    cur
->next=pre;
}

void insert(node* p)
{
    p
->next=head;
    head
=p;
}

void del(node* p)
{
    node 
*cur,*next;
    cur
=p;
    next
=p->next;
    
    
while(next)
    {
        delete cur;
        cur
=next;
        next
=cur->next;
    }
    
    delete cur;
}

void print(node* p)
{
    
while(p)
    {
        cout
<<p->data<<" ";
        p
=p->next;
    }
    
    cout
<<endl;
}

int main(int argc,char** argv)
{
    
for(int i=0;i<10;i++)
    {
        node
* p=new node;
        p
->next=(node*)null;
        p
->data=i;
        
        insert(p);
    }
    print(head);
    cout
<<"reverse order:"<<endl;
    
    reverse(head);
    print(head);
    
    del(head);
    
    
    system(
"pause");
    
return 0;
}




Kingoal Lee's Alogrithm Study using cplusplus 2007-09-05 14:41 鍙戣〃璇勮
]]>
姹備袱鎺掑簭鏁扮粍鐨勪腑浣嶆暟http://www.shnenglu.com/kingoal/archive/2007/08/31/31274.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusFri, 31 Aug 2007 02:32:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/31/31274.htmlhttp://www.shnenglu.com/kingoal/comments/31274.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/31/31274.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/31274.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/31274.html#include <iostream>
#include 
<string>

using namespace std;

int main(int argc,char* argv[])
{
    
int A[]={1,3,5,7,8,9};
    
int B[]={2,4,6,10,11,12,13,14};

    
int sizeA = sizeof(A)/sizeof(int);
    
int sizeB = sizeof(B)/sizeof(int);

    
int ma=0,na=sizeA-1;
    
int mb=0,nb=sizeB-1;

    
while(1)
    {
        
int ka=(na+ma+1)/2;
        
int kb=(nb+mb+1)/2;

        
if(na<ma)
        {
            cout
<<B[kb]<<endl;
            
break;
        }
        
if(nb<mb)
        {
            cout
<<A[ka]<<endl;
            
break;
        }
        
if(A[ka]==B[kb])//find the value
        {
            cout
<<A[ka]<<endl;
            
break;
        }

        
if((ma==na)&&((nb-mb+1)%2==0))//there is only one element at A[]
        {
            
if((A[na]<B[kb])&&(A[na]>=B[kb-1]))
            {
                cout
<<A[na]<<endl;
                
break;
            }
        }
        
if((ma==na)&&((nb-mb+1)%2))
        {
            
if((A[na]>B[kb])&&(A[na]<=B[kb+1]))
            {
                cout
<<A[na]<<endl;
                
break;
            }
        }

        
if((mb==nb)&&((na-ma+1)%2==0))//there is only one element at B[]
        {
            
if((B[nb]<A[ka])&&(B[nb]>=A[ka-1]))
            {
                cout
<<B[nb]<<endl;
                
break;
            }
        }
        
if((mb==nb)&&((na-ma+1)%2))
        {
            
if((B[nb]>A[ka])&&(B[nb]<=A[ka+1]))
            {
                cout
<<B[nb]<<endl;
                
break;
            }
        }

        
int offset=ka-ma;
        
if(offset>(kb-mb))
            offset
=kb-mb;
        
if(offset==0)
            offset
++;
        
//int offset=((ka>=kb)?ka:kb);

        
if(A[ka]<B[kb])
        {
            ma
+=offset;
            nb
-=offset;
        }

        
if(A[ka]>B[kb])
        {
            na
-=offset;
            mb
+=offset;
        }
    }

    system(
"pause");
    
return 0;
}



Kingoal Lee's Alogrithm Study using cplusplus 2007-08-31 10:32 鍙戣〃璇勮
]]>
甯哥敤瀛楃涓瞫tring鎿嶄綔--findhttp://www.shnenglu.com/kingoal/archive/2007/08/29/31131.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusWed, 29 Aug 2007 03:12:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/29/31131.htmlhttp://www.shnenglu.com/kingoal/comments/31131.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/29/31131.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/31131.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/31131.html#include <iostream>
#include 
<string>
#include 
<cctype>
#include 
<vector>
#include 
<algorithm>
#include 
<iterator>

using namespace std;

int main(int argc,char** argv[])
{
    string line1
="We were her pride of 10 she named us:";
    string line2
="Benjamin, Phoenix, the Prodigal";
    string line3
="and perspicacious pacific Suzanne";
    string sentence 
= line1+' '+line2+' '+line3;
    
    string separator(
" \n\t:,\r\v\f");
    vector
<string> longest,shortest;
    
int num = 0;
    string::size_type startpos
=0,endpos=0;
    string word;
    
int longLen=0,shortLen=-1,wordlen;
    
    
while((startpos=sentence.find_first_not_of(separator,endpos))!=string::npos)
    {
        
++num;
        
        endpos
=sentence.find_first_of(separator,startpos);
        
if(endpos==string::npos)
        {
            wordlen 
= sentence.size()-startpos;
        }
        
else
        {
            wordlen 
= endpos-startpos;
        }
        
        word.assign(sentence.begin()
+startpos,sentence.begin()+wordlen+startpos);
        
        startpos 
= sentence.find_first_not_of(separator,endpos);
        
        
if(shortLen==-1)
        {
            shortLen
=longLen=wordlen;
            shortest.push_back(word);
            longest.push_back(word);
            
            
continue;
        }
        
if(shortLen==wordlen)
        {
            shortest.push_back(word);
        }
        
if(shortLen>wordlen)
        {
            shortest.clear();
            shortest.push_back(word);
            shortLen 
= wordlen;
        }
        
if(wordlen==longLen)
        {
            longest.push_back(word);
        }
        
if(wordlen>longLen)
        {
            longest.clear();
            longest.push_back(word);
            longLen
=wordlen;
        }    
    }
    
    cout
<<"Words:"<<num<<endl;
    cout
<<"Shortest:"<<shortLen<<endl;
    copy(shortest.begin(),shortest.end(),ostream_iterator
<string>(cout," "));
    cout
<<endl;
    cout
<<"Longest:"<<longLen<<endl;
    copy(longest.begin(),longest.end(),ostream_iterator
<string>(cout," "));
    cout
<<endl;
    
    system(
"pause");
    
return 0;
}
#include <iostream>
#include 
<string>
#include 
<cctype>
#include 
<vector>
#include 
<algorithm>
#include 
<iterator>

using namespace std;

void str_replace(string& str,const string& src,const string& dst)
{
    string::size_type pos 
= 0;
    
int srclen = src.size();
    
int dstlen = dst.size();
    
    
while((pos = str.find(src,pos))!=string::npos)
    {
        
//str.replace(pos,srclen,dst);
        str.erase(pos,srclen);
        str.insert(pos,dst);
        pos
+=dstlen;
    }
}

int main(int argc,char** argv[])
{
    
//replace/erase/insert
    string str("I like apple,what about you? apple tastes great!");
    cout
<<str<<endl;
    str_replace(str,
"apple","banana");
    cout
<<str<<endl;
    
    
//assign/append
    string q1("When lilacs last in the dooryard bloom'd");
    string q2(
"The child is father of the man");
    string sentence;
    
    sentence.assign(q2.begin(),q2.begin()
+13);
    sentence.append(q1.substr(q1.find(
"in"),15));
    cout
<<sentence<<endl;
    
    system(
"pause");
    
return 0;
}



Kingoal Lee's Alogrithm Study using cplusplus 2007-08-29 11:12 鍙戣〃璇勮
]]>
STL甯歌鎿嶄綔錛?錛?-鎺掑簭http://www.shnenglu.com/kingoal/archive/2007/08/28/30998.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusTue, 28 Aug 2007 02:54:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/28/30998.htmlhttp://www.shnenglu.com/kingoal/comments/30998.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/28/30998.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/30998.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/30998.htmlgenerate_n(back_inserter(vec),100,rand);

sort(vec.begin(),vec.end());//or sort(vec.begin(),vec.end(),greater<int>());

copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," "));

#include <iostream>
#include 
<functional>
#include 
<algorithm>
#include 
<iterator>
#include 
<vector>
#include 
<cstdlib>

using namespace std;

int main(int argc,char** argv)
{
    vector
<int> vec;
    generate_n(back_inserter(vec),
100,rand);
    
    copy(vec.begin(),vec.end(),ostream_iterator
<int>(cout," "));
    cout
<<endl;
    
    sort(vec.begin(),vec.end());
    
    copy(vec.begin(),vec.end(),ostream_iterator
<int>(cout," "));
    cout
<<endl;
    
    system(
"pause");
    
return 0;
}




Kingoal Lee's Alogrithm Study using cplusplus 2007-08-28 10:54 鍙戣〃璇勮
]]>
partial_sort--鍩轟簬鍫嗘帓搴?/title><link>http://www.shnenglu.com/kingoal/archive/2007/08/27/30979.html</link><dc:creator>Kingoal Lee's Alogrithm Study using cplusplus</dc:creator><author>Kingoal Lee's Alogrithm Study using cplusplus</author><pubDate>Mon, 27 Aug 2007 14:49:00 GMT</pubDate><guid>http://www.shnenglu.com/kingoal/archive/2007/08/27/30979.html</guid><wfw:comment>http://www.shnenglu.com/kingoal/comments/30979.html</wfw:comment><comments>http://www.shnenglu.com/kingoal/archive/2007/08/27/30979.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/kingoal/comments/commentRss/30979.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/kingoal/services/trackbacks/30979.html</trackback:ping><description><![CDATA[緇忓父浼氬嚭鐜版眰n涓暟涓殑鍓峩涓渶灝忓?鏈澶у鹼級錛屽彲浠ラ噰鍙栫殑絳栫暐涓猴細<br><br> <div style="direction: ltr;">鐪媙鍜宬鐨勫ぇ灝忥紝鏁扮粍鐨勮寰嬨傚嵆渚跨湅鎯呭喌涔熶笉涓瀹氭湁緇濆鐨?#8220;鏈浼?<br>濡傛灉鏄熀鏈湁搴忕殑錛岄偅涔堝氨鐢℉oare鐨勭畻娉曪紝姣忔閫変腑闂翠綅緗殑<wbr>鏁板綋杞淬?br>濡傛灉k鎴?n-k)鏄皬甯告暟錛岄偅涔堥儴鍒嗘帓搴忕畻娉曪紝姣斿鐢ㄥ爢銆?br>濡傛灉瑕佹姷鎶楃畻娉曞鏉傚害鏀誨嚮錛岄偅涔堝彲浠ヨ冭檻闅忔満Hoare鎴栬卨i<wbr>near time selection.</div> 鍦╧涓哄皬鍊肩殑鏃跺欙紝鍙互閲囩敤鍩轟簬鍫嗘帓搴忕殑閮ㄥ垎鎺掑簭鏂規硶錛?br>浠g爜濡備笅錛?br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">iostream</span><span style="color: #000000;">></span><span style="color: #000000;"><br>#include </span><span style="color: #000000;"><</span><span style="color: #000000;">cstdlib</span><span style="color: #000000;">></span><span style="color: #000000;"><br>#include </span><span style="color: #000000;"><</span><span style="color: #000000;">algorithm</span><span style="color: #000000;">></span><span style="color: #000000;"><br>#include </span><span style="color: #000000;"><</span><span style="color: #000000;">iterator</span><span style="color: #000000;">></span><span style="color: #000000;"><br><br>using namespace std;<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> min_heapify(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> arr[],</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i,</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> size)<br>{<br>    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> left </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">2</span><span style="color: #000000;">*</span><span style="color: #000000;">i</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> right </span><span style="color: #000000;">=</span><span style="color: #000000;"> left</span><span style="color: #000000;">+</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br><br>    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> largest;<br><br>    </span><span style="color: #0000ff;">if</span><span style="color: #000000;">((left</span><span style="color: #000000;"><=</span><span style="color: #000000;">size)</span><span style="color: #000000;">&&</span><span style="color: #000000;">(arr[left]</span><span style="color: #000000;">></span><span style="color: #000000;">arr[i]))<br>    {<br>        largest </span><span style="color: #000000;">=</span><span style="color: #000000;"> left;<br>    }<br>    </span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br>    {<br>        largest </span><span style="color: #000000;">=</span><span style="color: #000000;"> i;<br>    }<br><br>    </span><span style="color: #0000ff;">if</span><span style="color: #000000;">((right</span><span style="color: #000000;"><=</span><span style="color: #000000;">size)</span><span style="color: #000000;">&&</span><span style="color: #000000;">(arr[right]</span><span style="color: #000000;">></span><span style="color: #000000;">arr[largest]))<br>    {<br>        largest </span><span style="color: #000000;">=</span><span style="color: #000000;"> right;<br>    }<br><br>    </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(largest</span><span style="color: #000000;">!=</span><span style="color: #000000;">i)<br>    {<br>        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> arr[i];<br>        arr[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">arr[largest];<br>        arr[largest]</span><span style="color: #000000;">=</span><span style="color: #000000;">temp;<br><br>        min_heapify(arr,largest,size);<br>    }<br>}<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> fillarray(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> arr[],</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> size)<br>{<br>    </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;"><</span><span style="color: #000000;">size;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>    {<br>        arr[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">rand()</span><span style="color: #000000;">%</span><span style="color: #000000;">size;<br>    }<br>}<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> print(</span><span style="color: #0000ff;">const</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> arr[],</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> size)<br>{<br>    copy(arr,arr</span><span style="color: #000000;">+</span><span style="color: #000000;">size,ostream_iterator</span><span style="color: #000000;"><</span><span style="color: #0000ff;">int</span><span style="color: #000000;">></span><span style="color: #000000;">(cout,</span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">));<br>    cout</span><span style="color: #000000;"><<</span><span style="color: #000000;">endl;<br>}<br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> argc,</span><span style="color: #0000ff;">char</span><span style="color: #000000;">*</span><span style="color: #000000;"> argv[])<br>{<br>    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> size,k;<br><br>    </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(cin</span><span style="color: #000000;">>></span><span style="color: #000000;">size)<br>    {<br>        </span><span style="color: #0000ff;">int</span><span style="color: #000000;">*</span><span style="color: #000000;"> buff</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;">[size];<br><br>        fillarray(buff,size);<br>        print(buff,size);<br><br>        cout</span><span style="color: #000000;"><<</span><span style="color: #000000;">"</span><span style="color: #000000;">Please input the top k value:</span><span style="color: #000000;">"</span><span style="color: #000000;"><<</span><span style="color: #000000;">endl;<br>        cin</span><span style="color: #000000;">>></span><span style="color: #000000;">k;<br><br>        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">(k</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">)</span><span style="color: #000000;">/</span><span style="color: #000000;">2</span><span style="color: #000000;">;i</span><span style="color: #000000;">>=</span><span style="color: #000000;">0</span><span style="color: #000000;">;i</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br>            min_heapify(buff,i,k</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br><br>        print(buff,size);<br>        cout</span><span style="color: #000000;"><<</span><span style="color: #000000;">endl</span><span style="color: #000000;"><<</span><span style="color: #000000;">endl;<br>        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;"> i</span><span style="color: #000000;">=</span><span style="color: #000000;">size</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;i</span><span style="color: #000000;">>=</span><span style="color: #000000;">k;i</span><span style="color: #000000;">--</span><span style="color: #000000;">)<br>        {<br>            </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(buff[i]</span><span style="color: #000000;"><</span><span style="color: #000000;">buff[</span><span style="color: #000000;">0</span><span style="color: #000000;">])<br>            {<br>                </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> buff[</span><span style="color: #000000;">0</span><span style="color: #000000;">];<br>                buff[</span><span style="color: #000000;">0</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">buff[i];<br>                buff[i]</span><span style="color: #000000;">=</span><span style="color: #000000;">temp;<br>    <br>                min_heapify(buff,</span><span style="color: #000000;">0</span><span style="color: #000000;">,k</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br>            }<br>        }<br><br>        print(buff,size);<br><br>        delete [] buff;<br>    }<br>}<br></span></div> <br><br><img src ="http://www.shnenglu.com/kingoal/aggbug/30979.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/kingoal/" target="_blank">Kingoal Lee's Alogrithm Study using cplusplus</a> 2007-08-27 22:49 <a href="http://www.shnenglu.com/kingoal/archive/2007/08/27/30979.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浜屽弶鎼滅儲鏍戞搷浣?3)----闈為掑綊閬嶅巻http://www.shnenglu.com/kingoal/archive/2007/08/24/30767.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusFri, 24 Aug 2007 08:16:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/24/30767.htmlhttp://www.shnenglu.com/kingoal/comments/30767.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/24/30767.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/30767.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/30767.html搴旂敤闃熷垪queue鐨勬槸BFS,鏍坰tack鐨勬槸DFS
浠g爜涓?
#include <iostream>
#include 
<cstdlib>
#include 
<queue>
#include 
<stack>
#include 
<algorithm>

using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    cout
<<"Original Input:"<<endl;
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

void BST_Preorder_Walk(BST* p)
{
    
    
if(p)
    {
        cout
<<p->key<<" ";
        BST_Preorder_Walk(p
->left);
        BST_Preorder_Walk(p
->right);
    }
}

void BST_Postorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Postorder_Walk(p
->left);
        BST_Postorder_Walk(p
->right);
        cout
<<p->key<<" ";
    }
}

void BST_Layer_Walk()
{
    queue
<BST*> queue;
    BST
* p;
    queue.push(root);
    
    
while(!queue.empty())
    {
        p
=queue.front();
        
        queue.pop();
        
if(p->left)
            queue.push(p
->left);
        
if(p->right)
            queue.push(p
->right);
        
        cout
<<p->key<<" ";
    }
    
    cout
<<endl;
}

void Inorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    
    
while(!stk.empty()||node)
    {
        
if(node)
        {
            stk.push(node);
            node
=node->left;
        }
        
else
        {
            node
=stk.top();
            cout
<<node->key<<" ";
            stk.pop();
            node
=node->right;
        }
    }
}

void Preorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    
    
while(!stk.empty()||node)
    {
        
if(node)
        {
            cout
<<node->key<<" ";
            stk.push(node);
            node
=node->left;
        }
        
else
        {
            node
=stk.top();
            stk.pop();
            node
=node->right;
        }
    }
}

void Postorder_Walk(BST* node=root)
{
    stack
<BST*> stk;
    BST
* p;
    
int flag = 0;//0 stands for left, 1 stands for right

    
do
    {
        
while(node)
        {
            stk.push(node);
            node
=node->left;
        }
        
        p
=NULL;
        flag
=0;
        
        
while(!stk.empty()&&(flag==0))
        {
            node
=stk.top();
            
if(node->right==p)
            {
                stk.pop();
                p
=node;
                cout
<<node->key<<" ";
            }
            
else
            {
                node
= node->right;
                flag
=1;
            }
        }
    }
while(!stk.empty());
}

int main(int argc,char* argv[])
{
    
int input;
    
    BST_Build();
    
    cout
<<"Inorder Walk:"<<endl;
    
//BST_Inorder_Walk(root);
    Inorder_Walk();
    cout
<<endl;
    
    cout
<<"Preorder Walk:"<<endl;
    BST_Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Stack Preorder Walk:"<<endl;
    Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Postorder Walk:"<<endl;
    BST_Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Stack Postorder Walk:"<<endl;
    Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Layer Walk:"<<endl;
    BST_Layer_Walk();
    
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}




Kingoal Lee's Alogrithm Study using cplusplus 2007-08-24 16:16 鍙戣〃璇勮
]]>
浜屽弶鎼滅儲鏍戞搷浣?2)--鍏朵粬http://www.shnenglu.com/kingoal/archive/2007/08/24/30756.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusFri, 24 Aug 2007 04:35:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/24/30756.htmlhttp://www.shnenglu.com/kingoal/comments/30756.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/24/30756.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/30756.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/30756.html#include <iostream>
#include 
<cstdlib>
using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    cout
<<"Original Input:"<<endl;
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

void BST_Preorder_Walk(BST* p)
{
    
    
if(p)
    {
        cout
<<p->key<<" ";
        BST_Preorder_Walk(p
->left);
        BST_Preorder_Walk(p
->right);
    }
}

void BST_Postorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Postorder_Walk(p
->left);
        BST_Postorder_Walk(p
->right);
        cout
<<p->key<<" ";
    }
}

BST
* BST_Search(int key)
{
    BST
* x=root;
    
    
while(x)
    {
        
if(x->key==key)
            
return x;
        
else
            
if(x->key>key)
                x
=x->left;
            
else
                x
=x->right;
    }
    
    
return NULL;    
}

BST
* BST_Min(BST* p=root)
{
    
//BST* p = root;
    
    
while(p->left)
    {
        p
=p->left;
    }
    
    
return p;
}

BST
* BST_Max(BST* p=root)
{
    
//BST* p = root;
    
    
while(p->right)
    {
        p
=p->right;
    }
    
    
return p;
}

BST
* BST_Successor(int key)
{
    BST
* p = BST_Search(key);
    BST
* y;
    
    
if(p)
    {
        
if(p->right)
        {
            
return BST_Min(p->right);
        }
        
        y
=p->parent;
        
while(y&&(y->right==p))
        {
            p
=y;
            y
=y->parent;
        }
        
        
return y;
    }
    
    
return NULL;
}

BST
* BST_Predecessor(int key)
{
    BST
* p = BST_Search(key);
    BST
* y;
    
    
if(p)
    {
        
if(p->left)
            
return BST_Max(p->left);
        
        y
=p->parent;
        
while(y&&(y->left==p))
        {
            p
=y;
            y
=y->parent;
        }
        
        
return y;
    }
    
    
return p;
}

BST
* BST_Delete(int key)
{
    BST
* p = BST_Search(key);
    BST
* y,*x;
    
    
if(p)
    {
        
if(!p->left||!p->right)
        {
            y
=p;
        }
        
else
            y
=BST_Successor(key);
            
        
if(y->left)
            x
=y->left;
        
else
            x
=y->right;
        
        
if(x!=NULL)
            x
->parent=y->parent;
            
        
if(!y->parent)
            root
=x;
        
else
        {
            
if(y==y->parent->left)
                y
->parent->left=x;
            
else
                y
->parent->right=x;
        }
        
        
if(y!=p)
            p
->key=y->key;
        
        
return y;
    }
    
    
return p;
}

int main(int argc,char* argv[])
{
    
int input;
    
    BST_Build();
    
    BST_Delete(
7);
    
    cout
<<"Inorder Walk:"<<endl;
    BST_Inorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Preorder Walk:"<<endl;
    BST_Preorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Postorder Walk:"<<endl;
    BST_Postorder_Walk(root);
    cout
<<endl;
    
    cout
<<"Min: "<<BST_Min()->key<<endl;
    cout
<<"Max: "<<BST_Max()->key<<endl;
    
    
while(1)
    {
        cin
>>input;
        
        
if(input==-1)
            
break;
        
        BST
* p;
        
if(p=BST_Search(input))
        {
            cout
<<"Parent="<<(p->parent)->key<<endl;
            
if(p->left)
                cout
<<"Left:"<<p->left->key<<endl;
            
if(p->right)
                cout
<<"Right:"<<p->right->key<<endl;
        }
        
else
        {
            cout
<<"Not found!"<<endl;
            
break;
        }
        
        
if(p=BST_Predecessor(input))
        {
            cout
<<"Predecessor:"<<p->key<<endl;
        }
        
        
if(p=BST_Successor(input))
        {
            cout
<<"Successor:"<<p->key<<endl;
        }
    }
    
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}



Kingoal Lee's Alogrithm Study using cplusplus 2007-08-24 12:35 鍙戣〃璇勮
]]>
浜屽弶鎼滅儲鏍戞搷浣?1)----鐢熸垚http://www.shnenglu.com/kingoal/archive/2007/08/24/30738.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusFri, 24 Aug 2007 01:19:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/24/30738.htmlhttp://www.shnenglu.com/kingoal/comments/30738.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/24/30738.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/30738.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/30738.html鐢熸垚涓媯典簩鍙夋悳绱㈡爲鍏抽敭鏄笉鏂湴鎻掑叆鏁版嵁鍒拌鏍戜腑錛岃嫢褰撳墠鐨剅oot涓篘ULL錛屽垯褰撳墠鎻掑叆鐨勮妭鐐逛負root錛屽惁鍒欐瘮杈冨乏鍙蟲爲鎻掑叆錛岀洿鍒頒負NULL涓轟箣銆?br>浜屽弶鎼滅儲鏍戠殑鎻掑叆浠g爜涓?
void BST_Insert(int key)
{
    鏋勫緩褰撳墠鑺傜偣current,鍒濆鍖朿urrent,璁劇疆鍏秜alue=key,宸﹀彸鐖惰妭鐐歸兘涓篘ULL;
   //鐢▁,y涓や釜鎸囬拡鏉ヨ窡韙猚urrent鏀劇疆鐨勪綅緗?br>   y=NULL;
  x=root;

  while(x)//x鏈変笅闈㈢殑鑺傜偣鏃?br>    {  
         y=x;
         x鐨刱ey鍜屽綋鍓嶅弬鏁伴噷闈㈢殑key鍋氬姣旓紝鑻ュぇ浜庡綋鍓峩ey錛屽垯x=left[x],鍚﹀垯x=right[x];
    }

  姣旇緝y鍜孨ULL鐨勫叧緋伙紝鑻==NULL,鍒檙oot=NULL錛屾湁root=current;
  璁劇疆parent[current]=y;
  姣旇緝current鐨刱ey鍜寉鐨刱ey鐨勫ぇ灝忥紝鑻ュぇ錛屽垯left[y]=current,鍚﹀垯right[y]=current;
//鎻掑叆瀹屾瘯
}
浣跨敤浠g爜琛ㄧず涓?
#include <iostream>
#include 
<cstdlib>
using namespace std;

#ifndef NULL
#define NULL 
0
#endif

#ifndef MAXSIZE
#define MAXSIZE    
10
#endif

typedef struct BST
//Binary Search Tree
{
    
int key;
    
//maybe there are some other satellites
    
    struct BST
* left;
    struct BST
* right;
    struct BST
* parent;
} BST;

BST
* root=NULL;

void BST_Insert(int key)//add value key to the Binary Search Tree
{
    BST
* y=NULL;//y records the parent node
    BST* x=root;//x records the current node
    
    BST
* node = new BST();
    node
->key = key;
    node
->left = NULL;
    node
->right = NULL;
    node
->parent = NULL;
    
    
while(x!=NULL)
    {
        y
=x;
        
        
if(key<x->key)
            x
=x->left;
        
else
            x
=x->right;
    }
    
    node
->parent=y;
    
    
if(y==NULL)
        root 
= node;
    
else
    {
        
if(key<y->key)
            y
->left=node;
        
else
            y
->right=node;
    }
}

void BST_Delete(BST* p)
{
    
if(p)
    {
        BST_Delete(p
->left);
        BST_Delete(p
->right);
        delete p;
    }
}

void BST_Build()
{
    
int temp;
    
    
for(int i=0;i<MAXSIZE;i++)
    {
        temp
=rand()%MAXSIZE;
        cout
<<temp<<" ";
        BST_Insert(temp);
    }
    cout
<<endl;
}

void BST_Inorder_Walk(BST* p)
{
    
if(p)
    {
        BST_Inorder_Walk(p
->left);
        cout
<<p->key<<" ";
        BST_Inorder_Walk(p
->right);
    }
}

int main(int argc,char* argv[])
{
    BST_Build();
    BST_Inorder_Walk(root);
    BST_Delete(root);
    
    cout
<<endl;
    system(
"pause");
    
return 0;
}




Kingoal Lee's Alogrithm Study using cplusplus 2007-08-24 09:19 鍙戣〃璇勮
]]>
閫夋嫨綆楁硶--Random Selecthttp://www.shnenglu.com/kingoal/archive/2007/08/23/30653.htmlKingoal Lee's Alogrithm Study using cplusplusKingoal Lee's Alogrithm Study using cplusplusThu, 23 Aug 2007 02:35:00 GMThttp://www.shnenglu.com/kingoal/archive/2007/08/23/30653.htmlhttp://www.shnenglu.com/kingoal/comments/30653.htmlhttp://www.shnenglu.com/kingoal/archive/2007/08/23/30653.html#Feedback0http://www.shnenglu.com/kingoal/comments/commentRss/30653.htmlhttp://www.shnenglu.com/kingoal/services/trackbacks/30653.html鏈Naive鐨勫姙娉曞氨鏄厛瀵規暣涓暟緇勮繘琛屾帓搴忥紙鍙互浣跨敤quicksort/merge sort/heap sort錛変絾鏄叾綆楁硶澶嶆潅搴︿負O(nlogn)錛屽鏋滀嬌鐢╭uicksort鐨刾artition鐨勫彉縐嶇殑璇濓紝鍏舵渶宸鏉傚害涓篛(n^2)錛屼絾鏄鉤鍧囧鏉傚害涓篛(n).鍏舵牳蹇冪畻娉曞涓嬶細
int randselect(int p,int r,int i)
{
    if(p==r)
          return arr[p];
   int q = partition(p,r);//璋冪敤quicksort閲岄潰鐨刾artition
   int k = q-p+1;
   if(i==k)
           return arr[q];
   if(i<k)
           return randselect(p,q-1,i);
   if(i>k)
           return randselect(q+1,r,i-k);
}

浠g爜濡備笅錛?br>
#include <iostream>
#include 
<iterator>
#include 
<algorithm>
#include 
<cstdlib>

using namespace std;

#define MAXSIZE    
100
int arr[MAXSIZE];

void fillarray()
{
    
for(int i=0;i<MAXSIZE;i++)
    {
        arr[i]
=rand()%MAXSIZE;
    }
}

void print(bool flag=true)
{
    
if(flag)
    {
        copy(arr,arr
+MAXSIZE,ostream_iterator<int>(cout," "));
        cout
<<endl;
    }
}

int partition(int p,int r)
{
    
int pivot = arr[r];
   
    
int i=p-1;
    
for(int j=p;j<r;j++)
    {
        
if(arr[j]<pivot)
        {
            i
++;
           
            
int temp = arr[j];
            arr[j]
=arr[i];
            arr[i]
=temp;
        }
    }
   
    arr[r]
=arr[i+1];
    arr[i
+1]=pivot;
   
    
return i+1;
}

void quicksort(int p,int r)
{
    
if(p<r)
    {
        
int q=partition(p,r);
        quicksort(p,q
-1);
        quicksort(q
+1,r);
    }
}

int randselect(int p,int r,int i)
{
    
if(p==r)
        
return arr[p];
       
    
int q = partition(p,r);
    
int k = q-p+1;
    
if(i==k)
        
return arr[q];
    
if(i<k)
        
return randselect(p,q-1,i);
    
if(i>k)
        
return randselect(q+1,r,i-k);
}

int main(int argc,char* argv[])
{
    fillarray();
    print();
   
    
//quicksort(0,MAXSIZE-1);
    
//print();
    for(int i=0;i<MAXSIZE;i++)
        cout
<<randselect(0,MAXSIZE-1,i+1)<<" ";
   
    cout
<<endl;
   
    system(
"pause");
    
return 0;
}



Kingoal Lee's Alogrithm Study using cplusplus 2007-08-23 10:35 鍙戣〃璇勮
]]>
精品久久久久久无码不卡| 久久夜色tv网站| 久久av高潮av无码av喷吹| 国内精品久久久久影院免费| 亚洲中文字幕伊人久久无码| 99久久99这里只有免费费精品 | 久久天天躁狠狠躁夜夜不卡| 亚洲中文字幕伊人久久无码| 伊人久久大香线蕉AV一区二区 | 久久伊人色| 久久无码AV一区二区三区| 国内精品伊人久久久久777| 久久精品国产亚洲av麻豆蜜芽| 久久久久人妻一区二区三区 | 国产亚洲欧美精品久久久| 久久精品中文字幕久久| 国产精品天天影视久久综合网| 久久亚洲国产欧洲精品一| 久久精品国产亚洲AV香蕉| 色综合久久久久网| 中文字幕人妻色偷偷久久| 久久久久国产精品麻豆AR影院 | 伊人色综合九久久天天蜜桃| 国内精品久久久久影院免费| 亚洲伊人久久成综合人影院| 久久97精品久久久久久久不卡| 偷窥少妇久久久久久久久| 狠狠久久综合伊人不卡| 99久久久精品| 久久久精品2019免费观看| 亚洲?V乱码久久精品蜜桃 | 久久精品国产99久久久香蕉| 国产精品一久久香蕉产线看| 色播久久人人爽人人爽人人片AV| 国产成人精品久久| 国产无套内射久久久国产| 色综合久久综合网观看| 久久九九全国免费| 久久激情亚洲精品无码?V| 久久久中文字幕日本| 久久性生大片免费观看性|