笔试题练习(七)

本文介绍了链表的基本操作,如逆序和合并有序链表,以及字符串处理技巧,包括统计字符频率、查找最长数字串和寻找最大公共子串。

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;
}

2,动态分配二维数组

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->int**alloArrays(unsignedintnrows,unsignedintncolumns)
{
unsigned
inti;
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;
}

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;
}

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;
}

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;
}
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值