1,链表的常见操作

  1. struct Node  
  2. {  
  3.     int value;  
  4.     struct Node* next;  
  5. }root;  
  6.  
  7. //已知链表的头结点head,写一个函数把这个链表逆序  
  8. Node * ReverseList(Node *head)   
  9. {//链表逆序  
  10.     assert(head != NULL);  
  11.     if (head->next == NULL)  
  12.     {//只有头节点  
  13.         return head;  
  14.     }  
  15.     Node* pPre = head->next;  
  16.     Node* pCur = pPre->next,pTmp;  
  17.     if (pCur == NULL)  
  18.     {//只有一个节点  
  19.         return head;  
  20.     }  
  21.     while (pCur != NULL)  
  22.     {  
  23.         pTmp = pCur->next;//记录下一个  
  24.         head->next = pCur;  
  25.         pCur->next = pPre;  
  26.         pPre = pCur;  
  27.         pCur = pTmp;  
  28.     }  
  29.     return head;  
  30. }  
  31.  
  32. Node * Merge(Node *head1 , Node *head2)  
  33. {//已知两个链表head1 和head2 各自有序升序排列,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)  
  34.  
  35.     Node* head = NULL;  
  36.     Node *p1 = head1->next;  
  37.     Node *p2 = head2->next;  
  38.     Node* pCur = head;  
  39.     while (p1 != NULL && p2 != NULL)  
  40.     {  
  41.         if (p1->value < p2->value)  
  42.         {  
  43.             pCur ->next = p1;  
  44.             pCur = p1;  
  45.             p1 = p1->next;  
  46.         }  
  47.         else 
  48.         {  
  49.             pCur->next = p2;  
  50.             pCur = p2;  
  51.             p2 = p2->next;  
  52.         }  
  53.     }  
  54.     if (p1 != NULL)  
  55.     {//第一个有剩余  
  56.         pCur->next = p1;  
  57.     }  
  58.     if (p2 != NULL)  
  59.     {  
  60.         pCur->next = p2;  
  61.     }  
  62.     return head;  
  63. }  
  64. Node * MergeRecursive(Node *head1 , Node *head2)  
  65. {//已知两个链表head1 和head2 各自升序排列,请把它们合并成一个链表依然有序,这次要求用递归方法进行。  
  66.     if ( head1 == NULL )  
  67.         return head2;  
  68.     if ( head2 == NULL)  
  69.         return head1;  
  70.     Node *head = NULL;  
  71.     if ( head1->data < head2->data )  
  72.     {  
  73.         head = head1;  
  74.         head->next = MergeRecursive(head1->next,head2);  
  75.     }  
  76.     else 
  77.     {  
  78.         head = head2;  
  79.         head->next = MergeRecursive(head1,head2->next);  
  80.     }  
  81.     return head;  

2,动态分配二维数组

  1. int **alloArrays(unsigned int nrows,unsigned int ncolumns)  
  2. {      
  3.     unsigned int i;  
  4.     int **array = (int **)malloc(nrows * sizeof(int *));  
  5.     for(i = 0; i < nrows; i++)  
  6.         array[i] = (int *)malloc(ncolumns * sizeof(int));  
  7.     return array;  
  8. }  
  9.  
  10. int** allocArrays2(int rows, int columns)  
  11. {  
  12.     int** array = new int* [rows];  
  13.     int i,j;  
  14.     for (i = 0;i< rows; ++i)  
  15.     {  
  16.         array[i] = new int[columns];  
  17.     }  
  18.     for (i = 0; i < rows; ++i)  
  19.     {  
  20.         for (j =0; j < columns;++j)  
  21.         {  
  22.             array[i][j] = i*j;  
  23.         }  
  24.     }  
  25.     return array;  

3.字符串简单操作

  1. /************************************************************************/ 
  2. /* Author: phinecos Date:2009-06-2                                                                     */ 
  3. /************************************************************************/ 
  4. #include <iostream>  
  5. using namespace std;  
  6.  
  7. int strcmp_p (const char *s1, const char *s2)  
  8. {   
  9.     int ret;  
  10.     while ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);  
  11.     return ret;  
  12. }  
  13.  
  14. int  memcmp_p(const char *s1, const char *s2, size_t n)  
  15. {    
  16.     int ret = 0;  
  17.     while (n-- && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);  
  18.     return ret;  
  19. }  
  20.  
  21. void* memcpy_p( void *dest, const void *src, size_t count )  
  22. {  
  23.     char* pDest = static_cast<char*>(dest);  
  24.     const char* pSrc = static_cast<const char*>(src);  
  25.  
  26.     if ((pDest > pSrc) && (pDest < (pSrc+count)))  
  27.     {//源地址和目标地址内存重叠  
  28.         for (size_t i = count -1 ; i != -1; --i)  
  29.             pDest[i] = pSrc[i];  
  30.     }  
  31.     else 
  32.     {  
  33.         for (size_t i = 0; i < count; ++i)  
  34.             pDest[i] = pSrc[i];  
  35.     }  
  36.     return dest;  
  37. }  
  38.  
  39. int main()  
  40. {  
  41.     char str[] = "0123456789";  
  42.     char str2[] = "0";  
  43.     cout << memcmp_p(str,str2,3) << endl;  
  44.     memcpy_p( str+1, str+0, 9 );  
  45.     cout << str << endl;  
  46.     memcpy_p( str, str+5, 5 );  
  47.     cout << str << endl;  
  48.     return 0;  

 4,统计一个字符串中所有字符出现的次数

  1. #include <iostream>  
  2. #include <map>  
  3. using namespace std;  
  4.  
  5. static map<char,int> countMap;  
  6. static int countArray[128];  
  7.  
  8. void doCount(const char* str)  
  9. {  
  10.     while (*str)  
  11.     {  
  12.         countMap[*str++]++;  
  13.     }  
  14. }  
  15. void doCount2(const char* str)  
  16. {  
  17.     while (*str)  
  18.     {  
  19.         countArray[*str++]++;  
  20.     }  
  21. }  
  22. int main()  
  23. {  
  24.     char str[] = "fasdfdsfdferwefaasdf";  
  25.     //使用map  
  26.     doCount(str);  
  27.     map<char,int>::iterator iter;  
  28.     for (iter = countMap.begin(); iter != countMap.end(); ++iter)  
  29.     {  
  30.         cout << iter->first << " : " << iter->second << endl;  
  31.     }  
  32.     //不用map,直接数组  
  33.     doCount2(str);  
  34.     for (int i = 0; i < 128; ++i)  
  35.     {  
  36.         if (countArray[i]>0)  
  37.         {  
  38.             printf("%c\t%d\n",i,countArray[i]);  
  39.         }  
  40.     }  
  41.     return 0;  

 5. 在字符串中找出连续最长的数字串的长度

  1.  
  2. int FindMaxIntStr(char *outputstr,char *intputstr)  
  3. {  
  4.     char *in = intputstr,*out = outputstr, *temp, *final;  
  5.     int count = 0, maxlen = 0;  
  6.  
  7.     while( *in != '\0' )  
  8.     {  
  9.         if( *in >= '0' && *in <= '9' )  
  10.         {  
  11.             for(temp = in; *in >= '0'&& *in <= '9'; in++ )  
  12.                 count++;  
  13.             if( maxlen < count )  
  14.             {  
  15.                 maxlen = count;  
  16.                 final = temp; // temp保存了当前连续最长字串的首地址,final是总的最长  
  17.                 *(final+count) = '\0'//  给字符串赋上结束符  
  18.             }  
  19.             count = 0; // 不管当前累计的最长数字是否大于前面最长的,都应该清除count,以便下次计数  
  20.         }  
  21.         //到此处时*in肯定不是数字  
  22.         in++;  
  23.     }  
  24.     // 上述比较过程只保存了最长字串在输入数组中对应的地址,避免了反复拷贝到输出数组的过程  
  25.     for(int i = 0; i < maxlen; i++)     // 将最终的最长字串保存到输出数组  
  26.     {  
  27.         *out++ = *final++;  
  28.     }  
  29.     *out = '\0';  
  30.     return maxlen;  

6,最大公共子串

  1. int maxCommonStr(char *s1, char *s2, char **r1, char **r2)  
  2. {//求最大公共子串  
  3.     int len1 = strlen(s1);  
  4.     int len2 = strlen(s2);  
  5.     int maxlen = 0;  
  6.     int i,j;  
  7.  
  8.     for(i = 0; i < len1; i++)  
  9.     {  
  10.         for(j = 0; j < len2; j++)  
  11.         {  
  12.             if(s1[i] == s2[j])        //找到了第一个相等的  
  13.             {  
  14.                 int as = i, bs = j, count = 1; // 保存第一个相等的首地址  
  15.                 while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs])     //查找最大相等长度  
  16.                     count++;  
  17.                 if(count > maxlen)  //如果大于最大长度则更新  
  18.                 {  
  19.                     maxlen = count;  
  20.                     *r1 = s1 + i;  
  21.                     *r2 = s2 + j;  
  22.                 }  
  23.             }  
  24.         }  
  25.     }