15个使用频率极高的基础算法题(附完整代码)

  1. 合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素

    一般来说,合并两个已经有序的数组,首先是开一个能存的下两个数组的第三个数组,但是题目中已经说了,其中一个数组能全部存的下,显然就不应该浪费空间了。
    从前往后扫的话,数据要存在大数组的前头,这样每次要把大数组的元素一次后移一位,显然不是什么好主意,所以我们从后往前存。

    1. #include<iostream>  
    2. #include<cstdlib>  
    3. using namespace std;  
    4.   
    5. int cc[10];  
    6. int tt[5];  
    7.   
    8. void init()  
    9. {  
    10.     srand((unsigned)time(NULL));  
    11.     int num = rand() % 5;  
    12.     for(int i = 0; i < 5; i++)  
    13.     {  
    14.         cc[i] = num;  
    15.         num += rand() % 10 + 1;  
    16.     }  
    17.     num = rand() % 5;  
    18.     for(int i = 0; i < 5; i++)  
    19.     {  
    20.         tt[i] = num;  
    21.         num += rand() % 10 + 1;  
    22.     }  
    23. }  
    24.   
    25. void merge()  
    26. {  
    27.     int i = 4, j = 4, idx = 9;  
    28.     while(i>=0 && j>=0)  
    29.     {  
    30.         if(cc[i] > tt[j])  
    31.             cc[idx--] = cc[i--];  
    32.         else  
    33.             cc[idx--] = tt[j--];  
    34.     }  
    35.     while(i>=0)  
    36.         cc[idx--] = cc[i--];  
    37.     while(j>=0)  
    38.         cc[idx--] = tt[j--];  
    39. }  
    40.   
    41. int main()  
    42. {  
    43.     init();  
    44.     for(int i = 0; i < 5; i++)  
    45.         printf("%d ", cc[i]);  
    46.     putchar(10);  
    47.     for(int i = 0; i < 5; i++)  
    48.         printf("%d ", tt[i]);  
    49.     putchar(10);  
    50.   
    51.     merge();  
    52.   
    53.     for(int i = 0; i < 10; i++)  
    54.         printf("%d ", cc[i]);  
    55.     putchar(10);  
    56.   
    57.     getchar();  
    58.     return 0;  
    59. }  
  2. 合并两个已经排序的单链表

    不能创建第三条链表,用上两种办法进行合并

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. struct node{  
    5.     int data;  
    6.     node *next;  
    7. };  
    8.   
    9. void CreateNode(node *head) // 创建带有头结点的有序单链表  
    10. {  
    11.     node *s = head;  
    12.     int val = rand() % 5 + 1;  
    13.     for (int i = 0; i < 5; i++)  
    14.     {  
    15.         val += rand() % 5 + 1;  
    16.         node *t = new node;  
    17.         t->data = val;  
    18.         t->next = NULL;  
    19.         s = s->next = t;  
    20.     }  
    21. }  
    22.   
    23. void PrintNode(node *head)  
    24. {  
    25.     node *s = head;  
    26.     while (s = s->next)  
    27.         printf("%d ", s->data);  
    28.     putchar(10);  
    29. }  
    30.   
    31. node* Merge_1(node *head1, node *head2) // 顺序扫描  
    32. {  
    33.     node *i = head1->next;  
    34.     node *j = head2->next;  
    35.     node *cc = new node;  
    36.     node *s = cc;  
    37.     while(i && j)  
    38.     {  
    39.         if(i->data < j->data)  
    40.         {  
    41.             s = s->next = i;  
    42.             i = i->next;  
    43.         }  
    44.         else  
    45.         {  
    46.             s = s->next = j;  
    47.             j = j->next;  
    48.         }  
    49.     }  
    50.     if(i)  
    51.         s->next = i;  
    52.     else  
    53.         s->next = j;  
    54.     return cc;  
    55. }  
    56.   
    57. node* Merge_2(node *head1, node *head2) // 递归法  
    58. {  
    59.     if(head1 == NULL)  
    60.         return head2;  
    61.     if(head2 == NULL)  
    62.         return head1;  
    63.     node *cc = NULL;  
    64.     if(head1->data < head2->data)  
    65.     {  
    66.         cc = head1;  
    67.         cc->next = Merge_2(head1->next, head2);  
    68.     }  
    69.     else  
    70.     {  
    71.         cc = head2;  
    72.         cc->next = Merge_2(head1, head2->next);  
    73.     }  
    74.     return cc;  
    75. }  
    76.   
    77. int main()  
    78. {  
    79.     srand((unsigned)time(NULL));  
    80.       
    81.     node *LinkList1 = new node, *LinkList2 = new node;  
    82.     CreateNode(LinkList1);  
    83.     CreateNode(LinkList2);  
    84.     PrintNode(LinkList1);  
    85.     PrintNode(LinkList2);  
    86.   
    87.     // LinkList1 = Merge_1(LinkList1, LinkList2);  
    88.     LinkList1->next = Merge_2(LinkList1->next, LinkList2->next);  
    89.     PrintNode(LinkList1);   // 合并后的链表存在1中  
    90.   
    91.     getchar();  
    92.     return 0;  
    93. }  
  3. 倒序打印一个单链表

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. struct node{  
    5.     int data;  
    6.     node *next;  
    7. };  
    8.   
    9. void CreateNode(node *&head) // 创建不带头结点的有序单链表  
    10. {  
    11.     head = new node;  
    12.     head->data = rand() % 5 + 1;  
    13.     node *s = head, *t = NULL;  
    14.     for(int i = 0; i < 5; i++)  
    15.     {  
    16.         t = new node;  
    17.         t->data = s->data + rand() % 5 + 1;  
    18.         s = s->next = t;  
    19.     }  
    20.     s->next = NULL;  
    21. }  
    22.   
    23. void PrintNode(node *head)  
    24. {  
    25.     node *s = head;  
    26.     while (s != NULL)  
    27.     {  
    28.         printf("%d ", s->data);  
    29.         s = s->next;  
    30.     }  
    31.     putchar(10);  
    32. }  
    33.   
    34. void PrintNode_Reverse(node *head)  
    35. {  
    36.     if(head->next != NULL)  
    37.         PrintNode_Reverse(head->next);  
    38.     printf("%d ", head->data);  
    39. }  
    40.   
    41. int main()  
    42. {  
    43.     srand((unsigned)time(NULL));  
    44.       
    45.     node *LinkList;  
    46.   
    47.     CreateNode(LinkList);  
    48.     PrintNode(LinkList);  
    49.     PrintNode_Reverse(LinkList);  
    50.   
    51.     putchar(10);  
    52.     getchar();  
    53.     return 0;  
    54. }  
  4. 给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点

    参见博客:如何在O(1)的时间里删除单链表的结点

  5. 找到链表倒数第K个节点

    设两个指针,一个先向前走K步,然后两个指针同步移动,等先走的指针走到了末尾,后走的指针就在倒数第K个位置了。

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. struct node{  
    5.     int data;  
    6.     node *next;  
    7. };  
    8.   
    9. void CreateNode(node *&head) // 创建不带头结点的有序单链表  
    10. {  
    11.     head = new node;  
    12.     head->data = rand() % 5 + 1;  
    13.     node *s = head, *t = NULL;  
    14.     for(int i = 0; i < 10; i++)  
    15.     {  
    16.         t = new node;  
    17.         t->data = s->data + rand() % 5 + 1;  
    18.         s = s->next = t;  
    19.     }  
    20.     s->next = NULL;  
    21. }  
    22.   
    23. void PrintNode(node *head)  
    24. {  
    25.     node *s = head;  
    26.     while (s != NULL)  
    27.     {  
    28.         printf("%d ", s->data);  
    29.         s = s->next;  
    30.     }  
    31.     putchar(10);  
    32. }  
    33.   
    34. void find(node *head, int num)  
    35. {  
    36.     node *s = head, *t = head;  
    37.     for(int i = 0; i < num; i++)  
    38.         t = t->next;  
    39.     while(t)  
    40.     {  
    41.         s = s->next;  
    42.         t = t->next;  
    43.     }  
    44.     printf("place: %dth value:%d\n", num, s->data);  
    45. }  
    46.   
    47. int main()  
    48. {  
    49.     srand((unsigned)time(NULL));  
    50.       
    51.     node *LinkList;  
    52.   
    53.     CreateNode(LinkList);  
    54.     PrintNode(LinkList);  
    55.     find(LinkList, 5);  
    56.     find(LinkList, 3);  
    57.   
    58.     putchar(10);  
    59.     getchar();  
    60.     return 0;  
    61. }  
  6. 反转单链表

    用递归与非递归两种方法,注意检查链表只有一个结点的时候反转函数不会出错

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. struct node{  
    5.     int data;  
    6.     node *next;  
    7. };  
    8.   
    9. void CreateNode(node *&head) // 创建不带头结点的有序单链表  
    10. {  
    11.     head = new node;  
    12.     head->data = rand() % 5 + 1;  
    13.     node *s = head, *t = NULL;  
    14.     for(int i = 0; i < 10; i++)  
    15.     {  
    16.         t = new node;  
    17.         t->data = s->data + rand() % 5 + 1;  
    18.         s = s->next = t;  
    19.     }  
    20.     s->next = NULL;  
    21. }  
    22.   
    23. void PrintNode(node *head)  
    24. {  
    25.     node *s = head;  
    26.     while (s != NULL)  
    27.     {  
    28.         printf("%d ", s->data);  
    29.         s = s->next;  
    30.     }  
    31.     putchar(10);  
    32. }  
    33.   
    34. node* Reverse_1(node *head)  // 非递归  
    35. {  
    36.     node *s = head, *t = head->next;  
    37.     s->next = NULL;  
    38.     while(t != NULL)  
    39.     {  
    40.         node *r = t->next;  
    41.         t->next = s;  
    42.         s = t;  
    43.         t = r;  
    44.     }  
    45.     return s;  
    46. }  
    47.   
    48. void Reverse_2(node *front, node *n, node *&head)  // 递归  
    49. {  
    50.     front->next = NULL;  
    51.     if(n != NULL)  
    52.         head = n;  
    53.     if(n != NULL && n->next != NULL)  
    54.         Reverse_2(n, n->next, head);  
    55.     if(n != NULL)  
    56.         n->next = front;  
    57. }  
    58.   
    59. int main()  
    60. {  
    61.     srand((unsigned)time(NULL));  
    62.       
    63.     node *LinkList;  
    64.   
    65.     CreateNode(LinkList);  
    66.     PrintNode(LinkList);  
    67.     LinkList = Reverse_1(LinkList);  
    68.     // Reverse_2(LinkList, LinkList->next, *&LinkList);  
    69.     PrintNode(LinkList);  
    70.   
    71.     putchar(10);  
    72.     getchar();  
    73.     return 0;  
    74. }  

  7. 通过两个栈实现一个队列

    参见博客:用两个栈实现队列

  8. 二分查找

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. void BinarySearch(int cc[], int num, int key)// 数组, 数组个数, 待查数字  
    5. {  
    6.     int i = 0, j = num;  
    7.     int cnt = 0, mid = 0;  
    8.     while(i < j)  
    9.     {  
    10.         cnt++;  
    11.         mid = (i + j) >> 1;  
    12.         if(cc[mid] == key)  
    13.         {  
    14.             printf("%d %s, index: %d\n", cnt, cnt==1?"time":"times", mid);  
    15.             return;  
    16.         }  
    17.         else if(cc[mid] < key)  
    18.             i = mid;  
    19.         else  
    20.             j = mid;  
    21.     }  
    22. }  
    23.   
    24. int main()  
    25. {  
    26.     int cc[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
    27.     BinarySearch(cc, 10, 3);  
    28.   
    29.     getchar();  
    30.     return 0;  
    31. }  
  9. 快速排序

    两种略微有些不一样的快排(传入参数不同)

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. int cc[10];  
    5.   
    6. void init()  
    7. {  
    8.     srand((unsigned)time(NULL));  
    9.     for(int i = 0; i < 10; i++)  
    10.     {  
    11.         int idx = rand()%10;  
    12.         while(cc[idx])  
    13.             idx = rand()%10;  
    14.         cc[idx] = i+1;  
    15.     }  
    16. }  
    17.   
    18. void show()  
    19. {  
    20.     for(int i = 0; i < 10; i++)  
    21.         printf("%d ", cc[i]);  
    22.     putchar(10);  
    23. }  
    24.   
    25. void QuickSort_1(int cc[], int num)  
    26. {  
    27.     int s = 0, t = num-1;  
    28.     int base = cc[0];  
    29.     if(num <= 1)  
    30.         return;  
    31.     while(s < t)  
    32.     {  
    33.         while(t > s && cc[t] >= base)  
    34.             t--;  
    35.         cc[s] = cc[t];  
    36.         while(s < t && cc[s] <= base)  
    37.             s++;  
    38.         cc[t] = cc[s];  
    39.     }  
    40.     cc[s] = base;  
    41.     QuickSort(cc, s);  
    42.     QuickSort(cc+s+1, num-s-1);  
    43. }  
    44.   
    45. void QuickSort_2(int cc[], int l, int r)  
    46. {  
    47.     if(l >= r)  
    48.         return;  
    49.     int s = l, t = r;  
    50.     int base = cc[l];  
    51.     while(s < t)  
    52.     {  
    53.         while(t > s && cc[t] >= base)  
    54.             t--;  
    55.         cc[s] = cc[t];  
    56.         while(s < t && cc[s] <= base)  
    57.             s++;  
    58.         cc[t] = cc[s];  
    59.     }  
    60.     cc[s] = base;  
    61.     QuickSort_2(cc, l, s-1);  
    62.     QuickSort_2(cc, s+1, r);  
    63. }  
    64.   
    65. int main()  
    66. {  
    67.     init();  
    68.     show();  
    69.     //QuickSort_1(cc, sizeof(cc)/sizeof(int));  
    70.     QuickSort_2(cc, 0, sizeof(cc)/sizeof(int)-1);  
    71.     show();  
    72.   
    73.     getchar();  
    74.     return 0;  
    75. }  

  10. 获得一个int型的数中二进制中的个数

    没明白这个题目的意思,我们就理解成两个小题好了:
    (1)求一个int型数字的二进制位数,如6->110->3位数
    (2)求这个int型数字的二进制中'1'的个数

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. int main()  
    5. {  
    6.     int num;  
    7.     while(~scanf("%d", &num))  
    8.     {  
    9.         int bits = 0, cnt = 0;  
    10.         int val = num;  
    11.         while(val)  
    12.         {  
    13.             if(val & 1)  
    14.                 cnt++;  
    15.             bits++;  
    16.             val>>=1;  
    17.         }  
    18.         printf("%d has %d bit and %d '1' in binary\n", num, bits, cnt);  
    19.     }  
    20.     return 0;  
    21. }  
  11. 输入一个数组,实现一个函数,让所有奇数都在偶数前面

    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. int cc[10];  
    5.   
    6. void init()  
    7. {  
    8.     srand((unsigned)time(NULL));  
    9.     for(int i = 0; i < 10; i++)  
    10.     {  
    11.         int idx = rand()%10;  
    12.         while(cc[idx])  
    13.             idx = rand()%10;  
    14.         cc[idx] = i+1;  
    15.     }  
    16. }  
    17.   
    18. void show()  
    19. {  
    20.     for(int i = 0; i < 10; i++)  
    21.         printf("%d ", cc[i]);  
    22.     putchar(10);  
    23. }  
    24.   
    25. void Sort(int cc[], int size)  
    26. {  
    27.     int s = 0, t = size-1;  
    28.     while(s < t)  
    29.     {  
    30.         while(s < t && (cc[s]&1))  
    31.             s++;  
    32.         while(t > s && !(cc[t]&1))  
    33.             t--;  
    34.         cc[s] ^= cc[t];  
    35.         cc[t] ^= cc[s];  
    36.         cc[s] ^= cc[t];  
    37.     }  
    38. }  
    39.   
    40. int main()  
    41. {  
    42.     init();  
    43.     show();  
    44.     Sort(cc, sizeof(cc)/sizeof(int));  
    45.     show();  
    46.   
    47.     getchar();  
    48.     return 0;  
    49. }  
  12. 判断一个字符串是否是另一个字符串的子串

    经典问题,用KMP算法。
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. char t[1000];  
    5. char p[100];  
    6. int f[100];  
    7.   
    8. int cnt = 0;  
    9.   
    10. void KMP()  //需要使用的参数是char t[]长串, char p[]模式串.  
    11. {  
    12.     int n = strlen(t);  
    13.     int m = strlen(p);  
    14.     /计算模式串的失配边  
    15.     f[0] = f[1] = 0;  
    16.     for(int i = 1; i < m; i ++)  
    17.     {  
    18.         int j = f[i];  
    19.         while(j && p[i] != p[j])j = f[j];  
    20.         f[i+1] = p[i] == p[j]?j+1:0;  
    21.     }  
    22.   
    23.     int j = 0;  
    24.     for(int i = 0; i < n; i ++)  
    25.     {  
    26.         while(j && t[i] != p[j]) j = f[j];  
    27.         if(p[j] == t[i])j ++;  
    28.         if(j == m)    //匹配成功  
    29.             cnt ++;  
    30.     }  
    31. }  
    32.   
    33.   
    34. int main()  
    35. {  
    36.     cnt = 0;  
    37.     gets(t);  
    38.     gets(p);  
    39.     KMP();  
    40.     if(cnt >= 1)  
    41.         printf("match!\n");  
    42.     else  
    43.         printf("not match!\n");  
    44.     getchar();  
    45.     return 0;  
    46. }  
  13. 把一个int型数组中的数字拼成一个串,这个串代表的数字最小

    关键就是要把int数组排个序,排序的cmp代码应该这样:如果两个数是A和B,那么应该比较AB和BA的大小,例如12、15,就要比较1215和1512的大小了。所以为了方便我们把int先转成char型,这样比较的时候直接strcat非常方便。
    1. #include <iostream>  
    2. using namespace std;  
    3.   
    4. void CreateArray(int *cc, int num)  
    5. {  
    6.     srand((unsigned int)time(NULL));  
    7.     for(int i = 0; i < num; i++)  
    8.         cc[i] = rand() % 50;  
    9. }  
    10.   
    11. int cmp(const void *a, const void *b)  
    12. {  
    13.     char *s1 = new char[10];  
    14.     char *s2 = new char[10];  
    15.     strcpy(s1, *(char**)a);  
    16.     strcat(s1, *(char**)b);  
    17.   
    18.     strcpy(s2, *(char**)b);  
    19.     strcat(s2, *(char**)a);  
    20.     return strcmp(s1, s2);  
    21. }  
    22.   
    23. void ConvertToMinString(int *cc, int len)  
    24. {  
    25.     char** str = (char**)new int[len];  
    26.     for(int i = 0; i < len; i++)  
    27.     {  
    28.         str[i] = new char[5];  
    29.         sprintf(str[i], "%d", cc[i]);  
    30.     }  
    31.     qsort(str, len, sizeof(char*), cmp);  
    32.     for(int i = 0; i < len; i++)  
    33.         printf("%s ", str[i]);  
    34.     putchar(10);  
    35. }  
    36.   
    37. int main()  
    38. {  
    39.     int cc[5];  
    40.     CreateArray(cc, 5);  
    41.     ConvertToMinString(cc, 5);  
    42.     getchar();  
    43.     return 0;  
    44. }  

  14. 输入一颗二叉树,输出它的镜像(每个节点的左右子节点交换位置)


    用上递归和非递归两种方法。
    1. #include <iostream>  
    2. #include <queue>  
    3. using namespace std;  
    4.   
    5. struct node{  
    6.     int val;  
    7.     node *left;  
    8.     node *right;  
    9. };  
    10.   
    11. void insert(node *&root, int data)  
    12. {  
    13.     if(root == NULL)  
    14.     {  
    15.         root = new node;  
    16.         root->val = data;  
    17.         root->left = root->right = NULL;  
    18.         return;  
    19.     }  
    20.     if(root->val > data)  
    21.         insert(root->left, data);  
    22.     else  
    23.         insert(root->right, data);  
    24. }  
    25.   
    26. node* build()  
    27. {  
    28.     node *root = NULL;  
    29.     insert(root, 10);  
    30.     for(int i = 8; i <= 12; i++)  
    31.     {  
    32.         if(i == 10)continue;  
    33.         insert(root, i);  
    34.     }  
    35.     return root;  
    36. }  
    37.   
    38. void showTree(node *root)  
    39. {  
    40.     if(root == NULL) return;  
    41.     printf("%d ", root->val);  
    42.     showTree(root->left);  
    43.     showTree(root->right);  
    44. }  
    45.   
    46. void Mirror_1(node *&root)    // 递归方法  
    47. {  
    48.     if(root == NULL) return;  
    49.     node *t = root->left;  
    50.     root->left = root->right;  
    51.     root->right = t;  
    52.     Mirror_1(root->left);  
    53.     Mirror_1(root->right);  
    54. }  
    55.   
    56. void Mirror_2(node *&root)     // 非递归方法  
    57. {  
    58.     queue<node*> q;  
    59.     if(root != NULL)  
    60.         q.push(root);  
    61.     while(!q.empty())  
    62.     {  
    63.         node *n = q.front();  
    64.         q.pop();  
    65.         node *t = n->left;  
    66.         n->left = n->right;  
    67.         n->right = t;  
    68.         if(n->left != NULL)  
    69.             q.push(n->left);  
    70.         if(n->right != NULL)  
    71.             q.push(n->right);  
    72.     }  
    73. }  
    74.   
    75. int main()  
    76. {  
    77.     node *root = build();  
    78.     showTree(root);putchar(10);  
    79.   
    80.     Mirror_1(root);  
    81.     showTree(root);putchar(10);  
    82.   
    83.     Mirror_2(root);  
    84.     showTree(root);putchar(10);  
    85.       
    86.     getchar();  
    87.     return 0;  
    88. }  
  15. 输入两个链表,找到它们第一个公共节点

    也就是说这两个链表的形状就像“Y”一样。
    我想到了两种办法:
    ①两个链表先分别枚举一遍,计算出两个链表的长度,假设是N和M,假设长链是N的长度,即N>=M,让长链先走N-M步,短链指针先不走,这样两个指针到结尾的距离都相同了,然后两个指针一边同步走一边进行比较,什么时候两个指针相同,当前节点就是第一个公共节点了。时间复杂度:O(n+m)
    ②设一个map或者其他键值对(哈希表),然后第一个链表走一步,根据其地址或主键建立索引,第二个链表走一步,根据其地址或主键建立索引,两个链表在遍历的时候分别去检索索引,什么时候发现已经记录了,那么当前节点就是第一个公共节点了。假设两个链表在相交之前的长度分别是N和M,则空间复杂度是O(max(n, m)*2),其时间复杂度是O(max(n, m) * 2)。
    相比之下我认为②要好的多,毕竟时间相比于空间重要太多了。所以下面我给出了②的代码,用地址作为索引。其实我觉得如何创建这样交叉的链表和如何存储指针地址也可以作为一个题目哈。
    1. #include <iostream>  
    2. #include <map>  
    3. using namespace std;  
    4.   
    5. struct node{  
    6.     int val;  
    7.     node *next;  
    8. };  
    9.   
    10. node* buildFirTree()    // 第一个链表有20个节点  
    11. {  
    12.     node *root = new node;  
    13.     root->val = 1;  
    14.   
    15.     node *s = root, *t = root;  
    16.     for(int i = 2; i <= 20; i++)  
    17.     {  
    18.         t = new node;  
    19.         t->val = i;  
    20.         s = s->next = t;  
    21.     }  
    22.     s->next = NULL;  
    23.     return root;  
    24. }  
    25.   
    26. node* buildSecTree(node *firnode)// 第二个链表创建5个结点,然后接到第一个链表的第8个结点上  
    27. {  
    28.     node *root = new node;  
    29.     root->val = 11;  
    30.     node *s = root, *t = root;  
    31.     for(int i = 2; i <= 5; i++)  
    32.     {  
    33.         t = new node;  
    34.         t->val = 10 + i;  
    35.         s = s->next = t;  
    36.     }  
    37.     node *k = firnode;  
    38.     for(int i = 0; i < 7; i++)  
    39.         k = k->next;  
    40.     s->next = k;  
    41.     return root;  
    42. }  
    43.   
    44. void disp(node *root)  
    45. {  
    46.     node *t = root;  
    47.     while(t)  
    48.     {  
    49.         printf("%d ", t->val);  
    50.         t = t->next;  
    51.     }  
    52.     putchar(10);  
    53. }  
    54.   
    55. node *GetFirstCommonNode(node *fir, node *sec)  
    56. {  
    57.     map<longbool> mp;  
    58.     mp.clear();  
    59.   
    60.     while(fir && sec)  
    61.     {  
    62.         if(mp[(long)fir] == true)  
    63.             return fir;  
    64.         mp[(long)fir] = true;  
    65.         fir = fir->next;  
    66.   
    67.         if(mp[(long)sec] == true)  
    68.             return sec;  
    69.         mp[(long)sec] = true;  
    70.         sec = sec->next;  
    71.     }  
    72.     return NULL;  
    73. }  
    74.   
    75. int main()  
    76. {  
    77.     node *fir = buildFirTree();  
    78.     printf("First linklist: ");disp(fir);  
    79.     node *sec = buildSecTree(fir);  
    80.     printf("second linklist: ");disp(sec);  
    81.   
    82.     node *common = GetFirstCommonNode(fir, sec);  
    83.     if(common != NULL)  
    84.         printf("common node is: %d\n", common->val);  
    85.     else  
    86.         printf("There is no common node\n");  
    87.   
    88.     getchar();  
    89.     return 0;  
    90. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值