1、将一个链表逆序
LinkList *reverse(LinkList *head) { LinkList *p1,*p2 = NULL,*p3 = NULL; if(head == NULL || head->next == NULL) return head; p1 = head->next; while(p1!=NULL) { p3 = p1->next; p1->next = p2; p2 = p1; p1 = p3; } head->next = p2; // head = p2; return head; }
2,计算一个字节里(byte)里面有多少bit被置1
#include <stdio.h> int comb(const int c) { int count = 0; int i = 0; int cc = c; while(i++<8) { if((cc&1)==1) { count++; } cc = cc>>1; } return count; } int main() { const int c = 0xcf; printf("%d ",comb(c)); return 1; }
3、在一个字符串中找到可能的最长的子字符串
#include <stdio.h> #include <stdlib.h> #include <string.h> char *commanstring(char shortstring[],char longstring[]) { int i,j; char *substring = malloc(256); if(strstr(longstring,shortstring)!=NULL) return shortstring; for(i=strlen(shortstring)-1;i>0;i--) { for(j=0;j<=strlen(shortstring)-i;j++) { memcpy(substring,&shortstring[j],i); substring[i]='