other algorithm
111posted @ 2006-11-21 22:11 ailab 閱讀(230) | 評論 (0) | 編輯 收藏
用心去做好這件事情
2006年11月21日 #
posted @ 2006-11-21 22:11 ailab 閱讀(230) | 評論 (0) | 編輯 收藏
posted @ 2006-11-21 22:11 ailab 閱讀(271) | 評論 (0) | 編輯 收藏
posted @ 2006-11-21 22:11 ailab 閱讀(226) | 評論 (0) | 編輯 收藏
?
posted @ 2006-11-21 22:09 ailab 閱讀(242) | 評論 (0) | 編輯 收藏
posted @ 2006-11-21 21:58 ailab 閱讀(289) | 評論 (0) | 編輯 收藏
2006年11月20日 #
兩個鏈表是否相交,如果相交,找出第一個開始相交的節(jié)點
node?
*
list(node?
*
list1,node?
*
list2)
{
?
if
(list1?
==
?
null
?
||
?list2?
==
?
null
)
???
return
?
null
;
?
int
?len1?
=
?
0
,len2?
=
?
0
;
?node?
*
p?
=
?list1,
*
q?
=
?list2;
?
while
(p
->
next?
!=
?
null
)
???
{
????????len1?
++
;
????????p?
=
?
->
next;
????????}
??
while
(q
->
next?
!=
?
null
)
??
{
??????len2?
++
;
??????q?
=
?q
->
next;
????}
??
if
(p?
!=
?q)
????
return
?
null
;
??len1?
++
;
??len2?
++
;
??p?
=
?list1;q?
=
?list2;
??
if
(len1?
>
?len2?)
??
{
???????
int
?diff?
=
?len1?
-
?len2;
???????p?
=
?list1;
???????
while
(diff?
>
?
0
?
&&
?p
!=
?
null
;)
????????
{
?????????p?
=
?p
->
next;
?????????diff?
--
;
??????????}
????}
??
else
?
?
{
???????
int
?diff?
=
?len2?
-
?len1;
???????q?
=
?list2;
???????
while
(diff?
>
?
0
?
&&
?q?
!=
?
null
)
????????
{diff?
--
;?q
=
?q
->
next;}
??}
?
while
(p?
!=
?q)
?
{
???p?
=
?p
->
next;q
=
?q
->
next;
?}
??
return
?p
????
}
posted @ 2006-11-20 23:32 ailab 閱讀(218) | 評論 (0) | 編輯 收藏
node?*reverse_list(node?*head)

{
??if(head?==?null?||?head->next?==?null)
??????return?head;
??node?*cur?=?head->next;
??node?*pre?=?head;
??node?*next?=?null;
??while(cur?!=?null)
??
{
?????next?=?cur->next;
?????cur->next?=?pre;
?????pre?=?cur;??
?????cur?=?next;
??}
??head->next?=?null;
??head?=?pre;
??reurn?head;
}
node?*reverse_list(node?*head)

{
??if(head?==?null?||?head->next?==?null)
????return?head;
??node?*cur?=?head->next;
??node?*next?=?null;
??head->next?=?null;
??while(cur?!=?null)
?
{
????next?=?cur->next;
????cur->next?=?head;
????head?=?cur;
????cur?=?next;
?}
??return?head;
??
}posted @ 2006-11-20 22:11 ailab 閱讀(213) | 評論 (0) | 編輯 收藏
node?
*
merge(node?
*
head1,node?
*
head2)
{
??
if
(head1?
==
?
null
)
?????
return
?head2;
??
if
(head2?
==
?
null
)
?????
return
?head1;
??reverse_list(
&
head2);
??node?
*
head3?
=
?
null
,
*
cur?
=
?
null
;
??node?
*
p?
=
?head1,
*
q?
=
?head2;
??
while
(p?
!=
?
null
?
&&
?q?
!=
?
null
)
??
{
????
if
(p
->
value?
<
?q
->
value)
????
{
???????
if
(head3?
==
?
null
)
????????
{
???????????head3?
=
?p;
???????????cur?
=
?p;
???????????p?
=
?p
->
next;
?????????}
????????
else
????????
{
??????????cur
->
next?
=
?p;
??????????cur?
=
?p;
??????????p?
=
?p
->
next;
?????????}
??????}
?????
else
??????
{
???????????
if
(head3?
==
?
null
)
???????????
{
?????????????head3?
=
?q;
?????????????cur?
=
?q;
?????????????q?
=
?q
->
next;
????????????}
???????????
else
?????????????
{
???????????????cur
->
next?
=
?q;
???????????????cur?
=
?q;
???????????????q
=
q
->
next;
???????????????}
????????}
???}
???
if
(p?
==
?
null
)
??????cur
->
next?
=
?q;
???
if
(q?
==
?
null
)
??????cur
->
next?
=
?p;
???
return
?head3;
}
posted @ 2006-11-20 22:04 ailab 閱讀(220) | 評論 (0) | 編輯 收藏
char?*strstr(const?char?*str,const?char?*sub)

{
??if(str?==?null?||?sub?==?null)
????return?null;
??const?char?*p?=?str;
??const?char?*q?=?sub;
??while(*str?!=?'\0'?&&?*sub?!=?'\0')
??
{
?????if(*str++?!=?*sub++)
??????
{
??????????str?=?++p;
??????????sub?=?q;
????????}
???}
??if(*sub?==?'\0')
????return?p;
??else
????return?null;
}
char?*strstr(const?char?*str,const?char?*sub)

{
??if(str?==?null?||?sub?==?null)
????return?null;
??const?char?*p?=?str;
??const?char?*q?=?sub;
??for(;*str?!=?'\0'?;str++)
??
{
????if(*str?!=?*sub)
??????continue;
????p?=?str;
????while(1)
????
{
????????if(*sub?==?'\0')
???????????return?str;
????????if(*p++?!=?*sub++)
???????????break;
?????????
??????}?
?????sub?=?q;
}posted @ 2006-11-20 21:50 ailab 閱讀(215) | 評論 (0) | 編輯 收藏
void?reverse(int?*a,int?n,int?k)

{
??if(a?==?null?||?n?<?0?||?k?>?n)
????return;
??revers_array(a,0,n-1);
??revers_array(a,0,k);
??revers_array(a,k+1,n-1);
??
}posted @ 2006-11-20 21:20 ailab 閱讀(253) | 評論 (0) | 編輯 收藏