每日微软面试题——day 1

.编写反转字符串的程序,要求优化速度、优化空间。

分析:构建两个迭代器p 和 q ,在一次遍历中,p的位置从字串开头向中间前进,q从字串末尾向中间后退,反转字串只要每次遍历都交换p和q所指向的内容即可,直到p和q在中间相遇,这时循环次数刚好等于 字串的长度/2。

实现代码:


  1. /** 
  2. author: 花心龟 
  3. blog:http://blog.csdn.net/zhanxinhang 
  4. **/  
  5.   
  6. #include <stdio.h>  
  7. void reverse(char *_str,int _l) //反转函数,_l指要反转字串的长度  
  8. {  
  9.   char*p=_str,*q=_str+_l-1;  
  10.   _l/=2;  
  11.  while(_l>0)  
  12.     {  
  13.       //为了使代码得到优化 采用异或操作符进行交换  
  14.       *p=*p^*q;  
  15.       *q=*p^*q;  
  16.       *p=*p^*q;  
  17.    
  18.       p++;  
  19.       q--;  
  20.       _l--;  
  21.     }  
  22. }  
  23.    
  24. int main()  
  25. {  
  26.   charstr0[11]= "0123456789";       
  27.  reverse(str0,sizeof(str0)-1);  
  28.  printf("str0 = %s\n",str0);  
  29.    
  30.   char str1[6]="01234";  
  31.  reverse(str1,sizeof(str1)-1);  
  32.  printf("str1 = %s",str1);  
  33.   return 0;  
  34. }  



题:.在链表里如何发现循环链接?

分析:可以构建两个迭代器p和pp,p一次向移动一个节点,pp一次移动两个节点,如果p和pp在某个时刻指向了同一个节点,那么该链表就有循环链接。

实现代码:

  1. /** 
  2. Author:花心龟 
  3. Blog:http://blog.csdn.net/zhanxinhang 
  4. **/  
  5. #include <stdio.h>  
  6. #include <malloc.h>  
  7.   
  8. #define TEST  
  9.   
  10. struct list_node  
  11. {  
  12.   int data;  
  13.   list_node * next;  
  14. };  
  15. list_node *head; //指向头结点  
  16.   
  17. void list_create()  
  18. {  
  19.   int i;  
  20.   list_node *p=NULL;  
  21.   head = (list_node*)malloc(sizeof(list_node));  
  22.   head->data=0;     //头结点数据设为  
  23.   head->next = NULL;  
  24.   
  25.   p=head;  
  26.   for(i=1; i<6; i++) //创建个结点  
  27.     {  
  28.       p->next = (list_node*)malloc(sizeof(list_node));  
  29.       p->next->data = i;  
  30.       p->next->next = NULL;  
  31.       p=p->next;  
  32.     }  
  33.   p->next = head;  //使尾结点的下一个结点指针指向头结点,构成循环链表  
  34.   
  35. #ifdef TEST  
  36.   p=head;  
  37.   for(i=0; i<12&&p!=NULL; i++)  
  38.     {  
  39.       printf("%d ",p->data);  
  40.       p=p->next;  
  41.     }  
  42.   printf("\n");  
  43. #endif  
  44. }  
  45.   
  46. void cycleList_test()  
  47. {  
  48.   list_node *p=NULL,*pp=NULL;  
  49.   p=head;  
  50.   pp=head->next->next;  
  51.   while(p!=pp )  
  52.     {  
  53.       p=p->next;  
  54.       pp=pp->next->next;  
  55.   
  56.       if(p==NULL || pp==NULL)  
  57.     {  
  58.       printf("不是循环链表");  
  59.       return ;  
  60.     }  
  61.     }  
  62.   printf("是循环链表");  
  63. }  
  64.   
  65. void list_destroy()  
  66. {  
  67.   list_node *pmove=NULL,*pdel=NULL;  
  68.   pmove=head->next;  
  69.   
  70.  while(pmove!=head)  
  71.    {  
  72.      pdel=pmove;  
  73.      pmove=pmove->next;  
  74.      free(pdel);     
  75.    }  
  76.   
  77.   free(head);  
  78. }  
  79. int main()  
  80. {  
  81.   list_create();   //构建循环链表  
  82.   cycleList_test();//测试是否是循环链表  
  83.   list_destroy();  //销毁链表  
  84.   return 0;  
  85. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值