char *addr = dest; int val_len = strlen(src); dest[val_len] = '\\0'; int i;
for (i=0; inext; while(q!=NULL) {
temp=q->next; q->next=p; p=q; q=temp; }
这样增加个辅助的指针就行乐。 ok 通过编译的代码: #include #include #include
typedef struct List{ int data;
struct List *next; }List;
List *list_create(void) {
struct List *head,*tail,*p; int e;
head=(List *)malloc(sizeof(List)); 第 21 页 共 55 页
tail=head;
printf(\ scanf(\ while(e){
p=(List *)malloc(sizeof(List)); p->data=e; tail->next=p; tail=p;
scanf(\ tail->next=NULL; return head; }
List *list_reverse(List *head) {
List *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;
第 22 页 共 55 页
return head; }
void main(void) {
struct List *head,*p; int d;
head=list_create(); printf(\
for(p=head->next;p;p=p->next) printf(\ head=list_reverse(head); printf(\
for(p=head;p->next;p=p->next) printf(\ }
编写函数数N个BYTE的数据中有多少位是1。
解:此题按步骤解:先定位到某一个BYTE数据;再计算其中有多少个1。叠加得解。 #incluede #define N 10 //定义BYTE类型别名 #ifndef BYTE
typedef unsigned char BYTE; #endif
int comb(BYTE b[],int n) {
int count=0;
第 23 页 共 55 页
int bi,bj; BYTE cc=1,tt;
//历遍到第bi个BYTE数据 for(bi=0;bi>1; tt=tt/2; } }
return count; } //测试 int main() {
BYTE b[10]={3,3,3,11,1,1,1,1,1,1}; cout
1。编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。
char * search(char *cpSource, char ch) {
char *cpTemp=NULL, *cpDest=NULL; int iTemp, iCount=0; while(*cpSource) {
if(*cpSource == ch) {
iTemp = 0;
cpTemp = cpSource;
第 24 页 共 55 页
while(*cpSource == ch) ++iTemp, ++cpSource; if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp; if(!*cpSource) break; }
++cpSource; }
return cpDest; }
#include #include //
// 自定义函数MyAtoI
// 实现整数字符串转换为证书输出
// 程序不检查字符串的正确性,请用户在调用前检查 //
int MyAtoI(char str[]) { int i;
int weight = 1; // 权重 int rtn = 0; // 用作返回
for(i = strlen(str) - 1; i >= 0; i--) {
rtn += (str - '0')* weight; //
第 25 页 共 55 页