1,链表的常见操作
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->structNode
{
intvalue;
structNode*next;
}root;
//已知链表的头结点head,写一个函数把这个链表逆序
Node*ReverseList(Node*head)
{//链表逆序
assert(head!=NULL);
if(head->next==NULL)
{//只有头节点
returnhead;
}
Node*pPre=head->next;
Node*pCur=pPre->next,pTmp;
if(pCur==NULL)
{//只有一个节点
returnhead;
}
while(pCur!=NULL)
{
pTmp=pCur->next;//记录下一个
head->next=pCur;
pCur->next=pPre;
pPre=pCur;
pCur=pTmp;
}
returnhead;
}
Node*Merge(Node*head1,Node*head2)
{//已知两个链表head1和head2各自有序升序排列,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
Node*head=NULL;
Node*p1=head1->next;
Node*p2=head2->next;
Node*pCur=head;
while(p1!=NULL&&p2!=NULL)
{
if(p1->value<p2->value)
{
pCur->next=p1;
pCur=p1;
p1=p1->next;
}
else
{
pCur->next=p2;
pCur=p2;
p2=p2->next;
}
}
if(p1!=NULL)
{//第一个有剩余
pCur->next=p1;
}
if(p2!=NULL)
{
pCur->next=p2;
}
returnhead;
}
Node*MergeRecursive(Node*head1,Node*head2)
{//已知两个链表head1和head2各自升序排列,请把它们合并成一个链表依然有序,这次要求用递归方法进行。
if(head1==NULL)
returnhead2;
if(head2==NULL)
returnhead1;
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);
}
returnhead;
}
{
intvalue;
structNode*next;
}root;
//已知链表的头结点head,写一个函数把这个链表逆序
Node*ReverseList(Node*head)
{//链表逆序
assert(head!=NULL);
if(head->next==NULL)
{//只有头节点
returnhead;
}
Node*pPre=head->next;
Node*pCur=pPre->next,pTmp;
if(pCur==NULL)
{//只有一个节点
returnhead;
}
while(pCur!=NULL)
{
pTmp=pCur->next;//记录下一个
head->next=pCur;
pCur->next=pPre;
pPre=pCur;
pCur=pTmp;
}
returnhead;
}
Node*Merge(Node*head1,Node*head2)
{//已知两个链表head1和head2各自有序升序排列,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)
Node*head=NULL;
Node*p1=head1->next;
Node*p2=head2->next;
Node*pCur=head;
while(p1!=NULL&&p2!=NULL)
{
if(p1->value<p2->value)
{
pCur->next=p1;
pCur=p1;
p1=p1->next;
}
else
{
pCur->next=p2;
pCur=p2;
p2=p2->next;
}
}
if(p1!=NULL)
{//第一个有剩余
pCur->next=p1;
}
if(p2!=NULL)
{
pCur->next=p2;
}
returnhead;
}
Node*MergeRecursive(Node*head1,Node*head2)
{//已知两个链表head1和head2各自升序排列,请把它们合并成一个链表依然有序,这次要求用递归方法进行。
if(head1==NULL)
returnhead2;
if(head2==NULL)
returnhead1;
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);
}
returnhead;
}
2,动态分配二维数组
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int**alloArrays(unsignedintnrows,unsignedintncolumns)
{
unsignedinti;
int**array=(int**)malloc(nrows*sizeof(int*));
for(i=0;i<nrows;i++)
array[i]=(int*)malloc(ncolumns*sizeof(int));
returnarray;
}
int**allocArrays2(introws,intcolumns)
{
int**array=newint*[rows];
inti,j;
for(i=0;i<rows;++i)
{
array[i]=newint[columns];
}
for(i=0;i<rows;++i)
{
for(j=0;j<columns;++j)
{
array[i][j]=i*j;
}
}
returnarray;
}
{
unsignedinti;
int**array=(int**)malloc(nrows*sizeof(int*));
for(i=0;i<nrows;i++)
array[i]=(int*)malloc(ncolumns*sizeof(int));
returnarray;
}
int**allocArrays2(introws,intcolumns)
{
int**array=newint*[rows];
inti,j;
for(i=0;i<rows;++i)
{
array[i]=newint[columns];
}
for(i=0;i<rows;++i)
{
for(j=0;j<columns;++j)
{
array[i][j]=i*j;
}
}
returnarray;
}
3.字符串简单操作
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->/************************************************************************/
/*Author:phinecosDate:2009-06-2*/
/************************************************************************/
#include<iostream>
usingnamespacestd;
intstrcmp_p(constchar*s1,constchar*s2)
{
intret;
while((ret=*(unsignedchar*)s1++-*(unsignedchar*)s2++)==0);
returnret;
}
intmemcmp_p(constchar*s1,constchar*s2,size_tn)
{
intret=0;
while(n--&&(ret=*(unsignedchar*)s1++-*(unsignedchar*)s2++)==0);
returnret;
}
void*memcpy_p(void*dest,constvoid*src,size_tcount)
{
char*pDest=static_cast<char*>(dest);
constchar*pSrc=static_cast<constchar*>(src);
if((pDest>pSrc)&&(pDest<(pSrc+count)))
{//源地址和目标地址内存重叠
for(size_ti=count-1;i!=-1;--i)
pDest[i]=pSrc[i];
}
else
{
for(size_ti=0;i<count;++i)
pDest[i]=pSrc[i];
}
returndest;
}
intmain()
{
charstr[]="0123456789";
charstr2[]="0";
cout<<memcmp_p(str,str2,3)<<endl;
memcpy_p(str+1,str+0,9);
cout<<str<<endl;
memcpy_p(str,str+5,5);
cout<<str<<endl;
return0;
}
/*Author:phinecosDate:2009-06-2*/
/************************************************************************/
#include<iostream>
usingnamespacestd;
intstrcmp_p(constchar*s1,constchar*s2)
{
intret;
while((ret=*(unsignedchar*)s1++-*(unsignedchar*)s2++)==0);
returnret;
}
intmemcmp_p(constchar*s1,constchar*s2,size_tn)
{
intret=0;
while(n--&&(ret=*(unsignedchar*)s1++-*(unsignedchar*)s2++)==0);
returnret;
}
void*memcpy_p(void*dest,constvoid*src,size_tcount)
{
char*pDest=static_cast<char*>(dest);
constchar*pSrc=static_cast<constchar*>(src);
if((pDest>pSrc)&&(pDest<(pSrc+count)))
{//源地址和目标地址内存重叠
for(size_ti=count-1;i!=-1;--i)
pDest[i]=pSrc[i];
}
else
{
for(size_ti=0;i<count;++i)
pDest[i]=pSrc[i];
}
returndest;
}
intmain()
{
charstr[]="0123456789";
charstr2[]="0";
cout<<memcmp_p(str,str2,3)<<endl;
memcpy_p(str+1,str+0,9);
cout<<str<<endl;
memcpy_p(str,str+5,5);
cout<<str<<endl;
return0;
}
4,统计一个字符串中所有字符出现的次数
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include<iostream>
#include<map>
usingnamespacestd;
staticmap<char,int>countMap;
staticintcountArray[128];
voiddoCount(constchar*str)
{
while(*str)
{
countMap[*str++]++;
}
}
voiddoCount2(constchar*str)
{
while(*str)
{
countArray[*str++]++;
}
}
intmain()
{
charstr[]="fasdfdsfdferwefaasdf";
//使用map
doCount(str);
map<char,int>::iteratoriter;
for(iter=countMap.begin();iter!=countMap.end();++iter)
{
cout<<iter->first<<":"<<iter->second<<endl;
}
//不用map,直接数组
doCount2(str);
for(inti=0;i<128;++i)
{
if(countArray[i]>0)
{
printf("%c/t%d/n",i,countArray[i]);
}
}
return0;
}
#include<map>
usingnamespacestd;
staticmap<char,int>countMap;
staticintcountArray[128];
voiddoCount(constchar*str)
{
while(*str)
{
countMap[*str++]++;
}
}
voiddoCount2(constchar*str)
{
while(*str)
{
countArray[*str++]++;
}
}
intmain()
{
charstr[]="fasdfdsfdferwefaasdf";
//使用map
doCount(str);
map<char,int>::iteratoriter;
for(iter=countMap.begin();iter!=countMap.end();++iter)
{
cout<<iter->first<<":"<<iter->second<<endl;
}
//不用map,直接数组
doCount2(str);
for(inti=0;i<128;++i)
{
if(countArray[i]>0)
{
printf("%c/t%d/n",i,countArray[i]);
}
}
return0;
}
5. 在字符串中找出连续最长的数字串的长度
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
intFindMaxIntStr(char*outputstr,char*intputstr)
{
char*in=intputstr,*out=outputstr,*temp,*final;
intcount=0,maxlen=0;
while(*in!='/0')
{
if(*in>='0'&&*in<='9')
{
for(temp=in;*in>='0'&&*in<='9';in++)
count++;
if(maxlen<count)
{
maxlen=count;
final=temp;//temp保存了当前连续最长字串的首地址,final是总的最长
*(final+count)='/0';//给字符串赋上结束符
}
count=0;//不管当前累计的最长数字是否大于前面最长的,都应该清除count,以便下次计数
}
//到此处时*in肯定不是数字
in++;
}
//上述比较过程只保存了最长字串在输入数组中对应的地址,避免了反复拷贝到输出数组的过程
for(inti=0;i<maxlen;i++)//将最终的最长字串保存到输出数组
{
*out++=*final++;
}
*out='/0';
returnmaxlen;
}
intFindMaxIntStr(char*outputstr,char*intputstr)
{
char*in=intputstr,*out=outputstr,*temp,*final;
intcount=0,maxlen=0;
while(*in!='/0')
{
if(*in>='0'&&*in<='9')
{
for(temp=in;*in>='0'&&*in<='9';in++)
count++;
if(maxlen<count)
{
maxlen=count;
final=temp;//temp保存了当前连续最长字串的首地址,final是总的最长
*(final+count)='/0';//给字符串赋上结束符
}
count=0;//不管当前累计的最长数字是否大于前面最长的,都应该清除count,以便下次计数
}
//到此处时*in肯定不是数字
in++;
}
//上述比较过程只保存了最长字串在输入数组中对应的地址,避免了反复拷贝到输出数组的过程
for(inti=0;i<maxlen;i++)//将最终的最长字串保存到输出数组
{
*out++=*final++;
}
*out='/0';
returnmaxlen;
}
6,最大公共子串
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->intmaxCommonStr(char*s1,char*s2,char**r1,char**r2)
{//求最大公共子串
intlen1=strlen(s1);
intlen2=strlen(s2);
intmaxlen=0;
inti,j;
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
{
if(s1[i]==s2[j])//找到了第一个相等的
{
intas=i,bs=j,count=1;//保存第一个相等的首地址
while(as+1<len1&&bs+1<len2&&s1[++as]==s2[++bs])//查找最大相等长度
count++;
if(count>maxlen)//如果大于最大长度则更新
{
maxlen=count;
*r1=s1+i;
*r2=s2+j;
}
}
}
}
}
{//求最大公共子串
intlen1=strlen(s1);
intlen2=strlen(s2);
intmaxlen=0;
inti,j;
for(i=0;i<len1;i++)
{
for(j=0;j<len2;j++)
{
if(s1[i]==s2[j])//找到了第一个相等的
{
intas=i,bs=j,count=1;//保存第一个相等的首地址
while(as+1<len1&&bs+1<len2&&s1[++as]==s2[++bs])//查找最大相等长度
count++;
if(count>maxlen)//如果大于最大长度则更新
{
maxlen=count;
*r1=s1+i;
*r2=s2+j;
}
}
}
}
}
本文介绍了链表的基本操作,如逆序和合并有序链表,以及字符串处理技巧,包括统计字符频率、查找最长数字串和寻找最大公共子串。
9万+

被折叠的 条评论
为什么被折叠?



