联发科笔试题编程部分

(1)实现链表的逆置

[cpp]   view plain copy
  1. node *inverselinklist(node *head)  
  2.   
  3. {  
  4.         node *p1,*p2,*p3;  
  5.         if(NULL==head||NULL==head->next) {  
  6.                return head;  
  7.         }  
  8.         p1=head;  
  9.         p2=p1->next;  
  10.         while (p2) {  
  11.                p3=p2->next;  
  12.                p2->next=p1;  
  13.                p1=p2;  
  14.                p2=p3;  
  15.         }  
  16.         head->next=NULL;  
  17.         head=p1;  
  18.         return head;  
  19. }  


(2)用普通算法实现两个有序链表的合并

  

[cpp]   view plain copy
  1. node *mergelinklist(node *head1,node *head2)  
  2. {  
  3.     node *p1,*p2,*temp=NULL,*cur=NULL,*head=NULL;  
  4.     p1=head1;  
  5.     p2=head2;  
  6.     while (p1!=NULL&&p2!=NULL) {  
  7.   
  8.         if (p1->value<p2->value) {  
  9.             temp=p1;  
  10.             p1=p1->next;  
  11.         }  
  12.         else  
  13.         {     
  14.             temp=p2;  
  15.             p2=p2->next;  
  16.         }  
  17.         if (NULL==head) {  
  18.             cur=temp;  
  19.             head=cur;  
  20.         }  
  21.         else  
  22.         {  
  23.             cur->next=temp;  
  24.             cur=temp;     
  25.         }  
  26.       
  27.     }  
  28.     temp=(p1?p1:p2);  
  29.     while (temp!=NULL) {  
  30.             cur->next=temp;  
  31.             cur=cur->next;  
  32.             temp=temp->next;  
  33.     }  
  34.     cur->next=NULL;  
  35.     return head;  
  36. }  



(3)用递归算法实现两个有序列表的合并

[cpp]   view plain copy
  1. node *mergeRecursion(node *head1,node *head2)  
  2. {  
  3.         if(NULL==head1)  
  4.         {  
  5.                return head2;  
  6.         }  
  7.         if(NULL==head2)  
  8.         {  
  9.                return head1;  
  10.         }  
  11.         node *head=NULL;  
  12.         if (head1->value<=head2->value) {  
  13.               head=head1;  
  14.               head->next=mergeRecursion(head1->next,head2);  
  15.         }  
  16.         else  
  17.         {  
  18.                head=head2;  
  19.                head->next=mergeRecursion(head1,head2->next);  
  20.         }                
  21.         return head;  
  22. }  



(4)判断单链表中是否存在环

[cpp]   view plain copy
  1. bool IsExitsLoop(slist *head)  
  2. {  
  3.     slist *slow = head, *fast = head;  
  4.     while ( fast && fast->next )   
  5.     {  
  6.         slow = slow->next;  
  7.         fast = fast->next->next;  
  8.         if ( slow == fast ) break;  
  9.     }  
  10.     return !(fast == NULL || fast->next == NULL);  
  11. }  



(5)二分查找法

[cpp]   view plain copy
  1. int binSearch(int key,int length, int array[])  
  2. {  
  3.         int mid=0;  
  4.         int start = 0;  
  5.         int end = length - 1;  
  6.         while (start <= end) {  
  7.               mid = (end - start) / 2 + start;  
  8.               if (key == array[mid]) {  
  9.                      return mid;  
  10.               }  
  11.               if (key < array[mid]) {  
  12.                      end = mid - 1;  
  13.                } else if (key > array[mid]) {  
  14.                      start = mid + 1;  
  15.                } else {  
  16.                      return mid;  
  17.                }  
  18.         }  
  19.         return -1;  
  20. }  

转载于:https://my.oschina.net/u/169988/blog/176248

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值