1. 写出程序把一个链表中的接点顺序倒排
typedef struct linknode
{
int data;
struct linknode *next;
}node;
//将一个链表逆置
node *reverse(node *head)
{
node *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
2. 写出程序删除链表中的所有接点
void del_all(node *head)
{
node *p;
while(head!=NULL)
{
p=head->next;
free(head);
head=p;
}
cout<<"释放空间成功!"<<endl;
}
3. 请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。
int search(char *cpSource, int n, char ch)
{
int i;
for(i=0; i<n && *(cpSource+i) != ch; ++i);
return i;
}
4. 写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于
str2返回1,若str1小于str2返回-1
int strcmp ( const char * src,const char * dst)
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
++src;
++dst;
}
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
5. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int maxlen = 0;
for(int i = 0; i < len1; i++)
{
for(int j = 0; j < len2; j++)
{
if(s1[i] == s2[j])
{
int as = 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;
}
}
}
}
}
6. 请写一个函数重载的简单例子
int add(int a,int b)
{
return (a + b);
}
float add(float a,float b)
{
return (a + b);
}
函数重载:// 函数重载是指函数名相同,但参数类型或返回值可能不同的函数。
7.请把下述代码加上异常处理。
int MyWriteFile(CString strFileName, CString strText)
{
int nRet = 0;
CFile myFile;
myFile.Open(strFileName, CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate,
NULL);
int nLen = strText.GetLength();
myFile.Write((char*)(LPCSTR)strText, nLen);
myFile.Close();
return nRet;
}
int MyWriteFile(CString strFileName, CString strText)
{
int nRet = 0;
CFile myFile;
__try
{
nRet = myFile.Open(strFileName,\
CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, NULL);
if(!nRet)
{
// 输出异常信息
__leave;// 结束try块,跳到__finaly代码块
}
int nLen = strText.GetLength();
nRet = myFile.Write((char*)(LPCSTR)strText, nLen);
if(!nRet)
{
//输出异常信息
__leave;// 结束try块,跳到__finaly代码块
}
}
__finaly
{
myFile.Close();
}
return nRet;
}
8、输入一个字符串,将其逆序后输出。(使用C++,不建议用伪码)
#include <iostream>
using namespace std;
void main(){
char a[50];
memset(a,0,sizeof(a));
int i=0,j;
char t;
cin.getline(a,50,'\n');
for(i=0,j=strlen(a)-1;i<strlen(a)/2;i++,j--)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
cout<<a<<endl;
}
拓展:直接用STL里现成的算法:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str;
getline(cin,str);
reverse(str.begin(), str.end());
cout<<"str Reversed:"<<str<<endl;
system("pause");
return 0;
}
9、在不用第三方参数的情况下,交换两个参数的值
#include <stdio.h>
void main()
{
int i=60;
int j=50;
i=i+j;
j=i-j;
i=i-j;
printf("i=%d\n",i);
printf("j=%d\n",j);
}
方法二:
i^=j;
j^=i;
i^=j;
10. 编写my_memcpy函数,实现与库函数memcpy类似的功能,不能使用任何库函数;
void* mymemcpy(void* pvTo, const char* pvFrom, size_t size)
{
assert((dest != NULL) && (src != NULL));
byte* psTo = (byte*)pvTo;
byte* psFrom = (byte*)pvFrom;
while (size– > 0)
{*psTo++ = *psFrom++;}
return pvTo;
}