C面试常用基础题目

来源:互联网 发布:淘宝店铺显示多个客服 编辑:程序博客网 时间:2024/06/06 00:12
 

C面试常用基础题目


线性表之单向链表的创建、插入、删除和清除

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. typedef struct node  
  4. {  
  5.   int num;  
  6.   struct node* next;  
  7. }Lnode;  
  8.   
  9. int Dele(Lnode* head,int num)  
  10. {  
  11.   Lnode* delnode = head;  
  12.   Lnode* forword = head;  
  13.   if(head == NULL) return -1;  
  14.   else  
  15.   {  
  16.     while((delnode->num != num) && (delnode ->next != NULL))  
  17.     {  
  18.       forword = delnode;  
  19.       delnode = delnode->next;  
  20.     }  
  21.     if(delnode->next == NULL && delnode->num != num) return -1;  
  22.     if(delnode == head)  
  23.     {  
  24.       head = head->next;  
  25.     }  
  26.     else if(delnode->next == NULL)  
  27.     {  
  28.       forword->next = NULL;  
  29.     }  
  30.     else  
  31.     {  
  32.       forword->next = delnode->next;  
  33.     }  
  34.     printf("delnode->next is 0x%lx\n",delnode->next);  
  35.     free(delnode);  
  36.     delnode = NULL;  
  37.   }  
  38. }  
  39.   
  40. void InSert(Lnode* head,int num)  
  41. {  
  42.   Lnode* tail = head;  
  43.   while(tail->next != NULL)  
  44.   {  
  45.     tail = tail->next;  
  46.   }    
  47.   Lnode* node = (Lnode*)malloc(sizeof(Lnode));  
  48.   node->num = num;  
  49.   node->next = NULL;  
  50.   tail->next = node;  
  51. }  
  52.   
  53. int main()  
  54. {  
  55.   Lnode* head = NULL;  
  56.   Lnode* innode = NULL;  
  57.   Lnode* tail = NULL;  
  58.   int i = 0;  
  59.   innode = (Lnode*)malloc(sizeof(Lnode));  
  60.   innode->num = 0;  
  61.   for(i = 1;i <= 3;i++)  
  62.   {  
  63.     if(head == NULL) head = innode;  
  64.     tail = innode;  
  65.     innode = (Lnode*)malloc(sizeof(Lnode));  
  66.     innode->num = i;  
  67.     printf("i is %d\n",i);  
  68.     innode->next = NULL;  
  69.     tail->next = innode;  
  70.   }  
  71.   Lnode* p = head;  
  72.   while(p != NULL)  
  73.   {  
  74.     printf("p->num is %d\n",p->num);  
  75.     p = p->next;  
  76.   }  
  77.   InSert(head,4);  
  78.   p = head;  
  79.   while(p != NULL)  
  80.   {  
  81.     printf("p->num is %d\n",p->num);  
  82.     p = p->next;  
  83.   }  
  84.   int result = Dele(head,2);  
  85.   printf("result is %d\n",result);  
  86.   p = head;  
  87.   while(p != NULL)  
  88.   {  
  89.     printf("p->num is %d\n",p->num);  
  90.     p = p->next;  
  91.   }  
  92.   p = head;  
  93.   while(p != NULL)  
  94.   {  
  95.     free(p);  
  96.     p = p->next;  
  97.   }  
  98.   return 0;  
  99. }  

一、冒泡排序

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int paixv(int* a,int num)  
  4. {  
  5.   int i = 0; int j = 0; int temp = 0;  
  6.   for(i = 0;i < num;i++)  
  7.   {  
  8.     for(j = i; j < num; j++)  
  9.     {  
  10.       if(a[i] >= a[j])  
  11.       {  
  12.         temp = a[i];  
  13.         a[i] = a[j];  
  14.         a[j] = temp;  
  15.       }  
  16.     }  
  17.   }  
  18.   return 0;  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.   int a[10] = {2,5,9,7,1,0,8,6,4,10};  
  24.   int result = paixv(a,10);  
  25.   int i = 0;  
  26.   for(;i < 10;i++)  
  27.     printf("%d,",a[i]);  
  28.   return 0;  
  29. }  
二、自己完成srtcpy函数
[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #incluce <stdio.h>  
  2. #define INPUT  
  3. int My_StrCpy(char* src, char* dst)  
  4. {  
  5.   if(src == NULL || dst == NULL)  
  6.   {  
  7.     printf("TK------>>>>>this is error\n");  
  8.     return -1;  
  9.   }  
  10.   while(*src != '\0')  
  11.   {  
  12.     *dst = *src;  
  13.     src ++;  
  14.     dst ++;  
  15.     if(*dst == '\0'break;  
  16.   }  
  17.   return 0;  
  18. }  
  19.   
  20. int main()  
  21. {  
  22. #ifdef INPUT  
  23.   char a[10];  
  24.   char b[4];  
  25.   printf("please input src:\n");  
  26.   scanf("%s",a);  
  27.   printf("please input dst:\n");  
  28.   scanf("%s",b);  
  29. #else  
  30.   char a[10] = "1234";  
  31.   char b[10] = "4321";  
  32. #endif  
  33.   int result = My_StrCpy(a,b);  
  34.   printf("TK---->>>>a is %s,b is %s\n",a,b);  
  35.   return 0;  
  36. }  

三、斐波那契数列

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int FiBo(int i)  
  4. {  
  5.   if(i == 1 || i == 2)  
  6.   {  
  7.     return 1;  
  8.   }  
  9.   else  
  10.   {  
  11.     return FiBo(i - 1) + FiBo(i - 2);  
  12.   }  
  13. }  
  14.   
  15.   
  16. int main()  
  17. {  
  18.   int num = 0;  
  19.   printf("please input :\n");  
  20.   scanf("%d",&num);  
  21.   int i = 1;  
  22.   for(;i <= num;i++)  
  23.   {  
  24.     printf("%d,",FiBo(i));  
  25.   }  
  26.   return 0;  
  27. }  

四、查找子字符串

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int FindSubString(char* str,char* substr)  
  4. {  
  5.   int result = 1;  
  6.   char* sub = substr;  
  7.   
  8.   while(*str != '\0')  
  9.   {  
  10.     sub = substr;  
  11.     if(*sub != *str)  
  12.     {  
  13.       str++;  
  14.       result++;  
  15.       continue;  
  16.     }  
  17.     while(*sub != '\0')  
  18.     {  
  19.       if(*str == *sub)  
  20.       {  
  21.         str++;  
  22.         sub++;  
  23.         result++;  
  24.       }  
  25.       else break;  
  26.     }  
  27.     if(*sub == '\0'return result - (sub - substr);  
  28.   }  
  29.   return -1;  
  30. }  
  31. int main()  
  32. {  
  33.   char a[20] = "01133412123456";  
  34.   char b[4] = "123";  
  35.   int result = FindSubString(a,b);  
  36.   printf("result is %d\n",result);  
  37.   return 0;   
  38. }  

五、自己写strcmp函数

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. int My_Strcmp(char* src, char* dst)  
  4. {  
  5.   if(src == NULL && dst == NULL) return -1;  
  6.   while(*src != '\0' && *dst != '\0')  
  7.   {  
  8.     if(*src != *dst) break;  
  9.     src++;  
  10.     dst++;  
  11.   }  
  12.   if(*src == '\0' && *dst == '\0'return 0;  
  13.   else return -1;  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.   char a[10];  
  19.   char b[10];  
  20.   printf("please input a:\n");  
  21.   scanf("%s",a);  
  22.   printf("please input b:\n");  
  23.   scanf("%s",b);  
  24.   int result = My_Strcmp(a,b);  
  25.   printf("result is %d\n",result);  
  26.   return 0;  
  27. }  

六、回文数

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. int huiwen(char* p)  
  5. {  
  6.   if(p == NULL) return -1;  
  7.   char* pbegin = p;  
  8.   char* pend = p;  
  9.   while(*pend != '\0')  
  10.   {  
  11.     pend ++;  
  12.   }    
  13.   pend --;  
  14.   while(pbegin < pend)  
  15.   {  
  16.     if(*pbegin != *pend)  
  17.     {  
  18.       return 0;  
  19.     }  
  20.     else  
  21.     {  
  22.       pbegin ++;  
  23.       pend --;  
  24.     }  
  25.   }  
  26.   return 1;  
  27. }  
  28.   
  29.   
  30. int main()  
  31. {  
  32.   char a[64];  
  33.   printf("please input :\n");  
  34.   scanf("%s",a);  
  35.   int result = huiwen(a);  
  36.   printf("result is %d\n",result);  
  37.   return 0;  
  38. }  

七、一些机试题

1.

  给定一个数组input[] ,如果数组长度n为奇数,则将数组中最大的元素放到 output[] 数组最中间的位置,如果数组长度n为偶数,则将数组中最大的元素放到 output[] 数组中间两个位置偏右的那个位置上,然后再按从大到小的顺序,依次在第一个位置的两边,按照一左一右的顺序,依次存放剩下的数。

  例如:input[] = {3, 6, 1, 9, 7}   output[] = {3, 7, 9, 6, 1};             input[] = {3, 6, 1, 9, 7, 8}    output[] = {1, 6, 8, 9, 7, 3}函数接口   void sort(int input[[, int n, int output[])

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. void sort(int input[],int n,int output[])  
  4. {  
  5.   int i = 0; int j = 0; int k = 0; int temp = 0; int flag = 0;  
  6.   for(;i < n;i++)  
  7.   {  
  8.     for(j = i; j < n;j++)  
  9.     {  
  10.       if(input[i] < input[j])  
  11.       {  
  12.         temp = input[i]; input[i] = input[j]; input[j] = temp;  
  13.       }  
  14.     }  
  15.     if(flag == 0)  
  16.     {  
  17.       output[n/2 + k] =  input[i];  
  18.       printf("output[%d] is %d\n",n/2 + k,input[i]);  
  19.       flag = 1;  
  20.     }  
  21.     else  
  22.     {   
  23.       k++;  
  24.       output[n/2 - k] =  input[i];  
  25.       printf("output[%d] is %d\n",n/2 - k,input[i]);  
  26.       flag = 0;  
  27.     }  
  28.   }  
  29. }  
  30.   
  31. int main()  
  32. {  
  33.   int i = 0;  
  34.   int output[10];  
  35. #ifdef IS_5  
  36.   int input[10] = {3,6,1,9,7};  
  37.   sort(input,5,output);  
  38.   for(;i < 5;i++)  
  39. #else  
  40.   int input[10] = {3, 6, 1, 9, 7, 8};   
  41.   sort(input,6,output);  
  42.   for(;i < 6;i++)   
  43. #endif  
  44.     printf("%d,",output[i]);  
  45.   return 0;  
  46. }  

2.

  操作系统任务调度问题。操作系统任务分为系统任务和用户任务两种。其中,系统任务的优先级 < 50,用户任务的优先级 >= 50且 <= 255。优先级大于255的为非法任务,应予以剔除。现有一任务队列task[],长度为n,task中的元素值表示任务的优先级,数值越小,优先级越高。函数scheduler实现如下功能,将task[] 中的任务按照系统任务、用户任务依次存放到 system_task[] 数组和 user_task[] 数组中(数组中元素的值是任务在task[] 数组中的下标),并且优先级高的任务排在前面,优先级相同的任务按照入队顺序排列(即先入队的任务排在前面),数组元素为-1表示结束。

  例如:task[] = {0, 30, 155, 1, 80, 300, 170, 40, 99}    system_task[] = {0, 3, 1, 7, -1}    user_task[] = {4, 8, 2, 6, -1}函数接口    void scheduler(int task[], int n, int system_task[], int user_task[])

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. void scheduler(int task[], int n, int system_task[], int user_task[])  
  4. {  
  5.   int sys_id[10];  
  6.   int us_id[10];  
  7.   int i = 0; int stotal = 0; int utotal = 0;  
  8.   for(;i < n;i++)  
  9.   {  
  10.     if(task[i] >= 50 && task[i] <= 255)  
  11.     {  
  12.       us_id[utotal] = task[i];  
  13.       user_task[utotal] = i;  
  14.       utotal++;   
  15.     }  
  16.     else if(task[i] < 50)  
  17.     {  
  18.       sys_id[stotal] = task[i];  
  19.       system_task[stotal] = i;  
  20.       stotal++;  
  21.     }  
  22.   }  
  23.   i = 0; int j = 0; int tempid = 0; int temp;  
  24.   for(;i < utotal;i++)  
  25.   {  
  26.     for(j = i;j < utotal;j++)  
  27.     {  
  28.       if(us_id[i] > us_id[j])  
  29.       {  
  30.         temp = user_task[i];  
  31.         user_task[i] = user_task[j];  
  32.         user_task[j] = temp;  
  33.         tempid = us_id[i];  
  34.         us_id[i] = us_id[j];  
  35.         us_id[j] = tempid;  
  36.       }  
  37.     }  
  38.   }  
  39.   user_task[utotal] = -1;  
  40.   i = 0; j = 0;  
  41.   for(;i < stotal;i++)  
  42.   {  
  43.     for(j = i;j < stotal;j++)  
  44.     {  
  45.       if(sys_id[i] > sys_id[j])  
  46.       {  
  47.         temp = system_task[i];  
  48.         system_task[i] = system_task[j];  
  49.         system_task[j] = temp;  
  50.         tempid = sys_id[i];  
  51.         sys_id[i] = sys_id[j];  
  52.         sys_id[j] = tempid;  
  53.       }  
  54.     }  
  55.   }  
  56.   system_task[stotal] = -1;  
  57. }  
  58.   
  59.   
  60. int main()  
  61. {  
  62.   int task[9] = {0, 30, 155, 1, 80, 300, 170, 40, 99};  
  63.   int system_task[20];  
  64.   int user_task[20];  
  65.   scheduler(task,9,system_task,user_task);  
  66.   int i = 0;  
  67.   printf("user_task is :\n");  
  68.   while(user_task[i] != -1)  
  69.   {  
  70.     printf("%d",user_task[i]);  
  71.     i++;  
  72.   }  
  73.   i = 0;  
  74.   printf("\nsystem_task is :\n");  
  75.   while(user_task[i] != -1)  
  76.   {  
  77.     printf("%d",system_task[i]);  
  78.     i++;  
  79.   }  
  80.   return 0;  
  81. }  
0 0