c++常见笔试题(4)

 
 

40. 链表题:一个链表的结点结构

   
   
  1. struct Node  
  2. {  
  3. int data ;  
  4. Node *next ;  
  5. };  
  6. typedef struct Node Node ; 

(1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)

   
   
  1. Node * ReverseList(Node *head) //链表逆序  
  2. {  
  3. if ( head == NULL || head->next == NULL )  
  4. return head;  
  5. Node *p1 = head ;  
  6. Node *p2 = p1->next ;  
  7. Node *p3 = p2->next ;  
  8. p1->next = NULL ;  
  9. while ( p3 != NULL )  
  10. {  
  11. p2->next = p1 ;  
  12. p1 = p2 ;  
  13. p2 = p3 ;  
  14. p3 = p3->next ;  
  15. }  
  16. p2->next = p1 ;  
  17. head = p2 ;  
  18. return head ;  
  19. }  

(2)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)

   
   
  1. Node * Merge(Node *head1 , Node *head2)  
  2. {  
  3. if ( head1 == NULL)  
  4. return head2 ;  
  5. if ( head2 == NULL)  
  6. return head1 ;  
  7. Node *head = NULL ;  
  8. Node *p1 = NULL;  
  9. Node *p2 = NULL;  
  10. if ( head1->data < head2->data )  
  11. {  
  12. head = head1 ;  
  13. p1 = head1->next;  
  14. p2 = head2 ;  
  15. }  
  16. else 
  17. {  
  18. head = head2 ;  
  19. p2 = head2->next ;  
  20. p1 = head1 ;  
  21. }  
  22. Node *pcurrent = head ;  
  23. while ( p1 != NULL && p2 != NULL)  
  24. {  
  25. if ( p1->data <= p2->data )  
  26. {  
  27. pcurrent->next = p1 ;  
  28. pcurrent = p1 ;  
  29. p1 = p1->next ;  
  30. }  
  31. else 
  32. {  
  33. pcurrent->next = p2 ;  
  34. pcurrent = p2 ;  
  35. p2 = p2->next ;  
  36. }  
  37. }  
  38. if ( p1 != NULL )  
  39. pcurrent->next = p1 ;  
  40. if ( p2 != NULL )  
  41. pcurrent->next = p2 ;  
  42. return head ;  
  43. }  

(3)已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk)

答案:

   
   
  1. Node * MergeRecursive(Node *head1 , Node *head2)  
  2. {  
  3. if ( head1 == NULL )  
  4. return head2 ;  
  5. if ( head2 == NULL)  
  6. return head1 ;  
  7. Node *head = NULL ;  
  8. if ( head1->data < head2->data )  
  9. {  
  10. head = head1 ;  
  11. head->next = MergeRecursive(head1->next,head2);  
  12. }  
  13. else 
  14. {  
  15. head = head2 ;  
  16. head->next = MergeRecursive(head1,head2->next);  
  17. }  
  18. return head ;  
  19. }

41. 分析一下这段程序的输出 (Autodesk)

   
   
  1. class B  
  2. {  
  3. public:  
  4. B()  
  5. {  
  6. cout<<"default constructor"<<endl; span=""  <="">
  7. }  
  8. ~B()  
  9. {  
  10. cout<<"destructed"<<endl; span=""  <="">
  11. }  
  12. B(int i):data(i) //B(int) works as a converter ( int -> instance of B)  
  13. {  
  14. cout<<"constructed by parameter " << data <<endl; span=""  <="">
  15. }  
  16. private:  
  17. int data;  
  18. };  
  19.  
  20.  
  21. B Play( B b)  
  22. {  
  23. return b ;  
  24. }  
  25.  
  26. (1) results:  
  27. int main(int argc, char* argv[]) constructed by parameter 5  
  28. { destructed B(5)形参析构  
  29. B t1 = Play(5); B t2 = Play(t1);   destructed t1形参析构  
  30. return 0; destructed t2 注意顺序!  
  31. } destructed t1  
  32.  
  33. (2) results:  
  34. int main(int argc, char* argv[]) constructed by parameter 5  
  35. { destructed B(5)形参析构  
  36. B t1 = Play(5); B t2 = Play(10);   constructed by parameter 10  
  37. return 0; destructed B(10)形参析构  
  38. } destructed t2 注意顺序!  
  39.  
  40. destructed t1  

42. 写一个函数找出一个整数数组中,第二大的数 (microsoft)

答案:

   
   
  1. const int MINNUMBER = -32767 ;  
  2. int find_sec_max( int data[] , int count)  
  3. {  
  4. int maxnumber = data[0] ;  
  5. int sec_max = MINNUMBER ;  
  6. for ( int i = 1 ; i < count ; i++)  
  7. {  
  8. if ( data[i] > maxnumber )  
  9. {  
  10. sec_max = maxnumber ;  
  11. maxnumber = data[i] ;  
  12. }  
  13. else 
  14. {  
  15. if ( data[i] > sec_max )  
  16. sec_max = data[i] ;  
  17. }  
  18. }  
  19. return sec_max ;  
  20. }  

43. 写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。

KMP算法效率最好,时间复杂度是O(n+m)。

44. 多重继承的内存分配问题:比如有class A : public class B, public class C {}那么A的内存结构大致是怎么样的?

这个是compiler-dependent的, 不同的实现其细节可能不同。

如果不考虑有虚函数、虚继承的话就相当简单;否则的话,相当复杂。

45. 如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)

struct node { char val; node* next;}

bool check(const node* head) {} //return false : 无环;true: 有环

一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):

   
   
  1. bool check(const node* head)  
  2. {  
  3. if(head==NULL) return false;  
  4. node *low=head, *fast=head->next;  
  5. while(fast!=NULL && fast->next!=NULL)  
  6. {  
  7. low=low->next;  
  8. fast=fast->next->next;  
  9. if(low==fast) return true;  
  10. }  
  11. return false;   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值