日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

關于鏈表筆試題的一些收集

 Kinetis 2013-12-24

1.已知鏈表的頭結點head,寫一個函數(shù)把這個鏈表逆序

  1. void List::reverse()  
  2. {  
  3.         list_node * p = head;  
  4.         list_node * q = p->next;  
  5.         list_node * r = NULL;  
  6.         while(q){  
  7.                 r = q->next;  
  8.                 q->next = p;  
  9.                 p = q;  
  10.                 q = r;  
  11.         }  
  12.         head->next  = NULL;  
  13.         head = p;  
  14. }  

遞歸方法:

  1. void List::reverse2(list_node * curnode)  
  2. {  
  3.         if(curnode ==NULL)curnode = head;  
  4.         if(curnode->next==NULL)  
  5.         {  
  6.                 cur = curnode;  
  7.                 return;  
  8.         }  
  9.   
  10.         reverse2(curnode->next);  
  11.         curnode->next->next=curnode;  
  12.         if(curnode == head)  
  13.         {  
  14.                 head=cur;  
  15.                 cur = curnode;  
  16.                 cur->next = NULL;  
  17.         }  
  18. }  

2.已知兩個鏈表head1 和head2 各自有序,請把它們合并成一個鏈表依然有序。

  1. void List::merge(List & list)  
  2. {         
  3.         list_node * a = head;  
  4.         list_node * b = list.head;  
  5.         list_node * tempa= a;  
  6.         list_node * tempb = b;  
  7.         while(a&&b)   
  8.         {  
  9.                 if(a->value <= b->value)  
  10.                 {  
  11.                         while(a&&b&&a->value <= b->value)  
  12.                         {  
  13.                                 tempa = a;  
  14.                                 a = a->next;  
  15.                         }  
  16.                         tempa->next=b;  
  17.                 }  
  18.                 else  
  19.                 {  
  20.                         while(a&&b&&a->value > b->value)  
  21.                         {  
  22.                                 tempb = b;  
  23.                                 b = b->next;  
  24.                         }  
  25.                         tempb->next=a;  
  26.                 }  
  27.   
  28.         }  
  29. }  

遞歸方法:

  1. list_node* List::recursive_merge(list_node * a,list_node * b)  
  2. {  
  3.         if(a == NULL)return b;  
  4.         if(b == NULL)return a;  
  5.         if(a->value <= b->value){  
  6.                 a->next=recursive_merge(a->next,b);  
  7.                 return a;  
  8.         }  
  9.         if(a->value > b->value)  
  10.         {  
  11.                 b->next=recursive_merge(a,b->next);  
  12.                 return b;  
  13.         }  
  14. }  

3.有一個鏈表L,其每個節(jié)點有2個指針,一個指針next指向鏈表的下個節(jié)點,另一個random隨機指向鏈表中的任一個節(jié)點,可能是自己或者為空,寫一個程序,要求復制這個鏈表的結構并分析其復雜性

這個題目的方法很巧妙,將兩個鏈表連接起來形成一個鏈表,設置好random指針后再將兩個鏈表分割開來。

 

  1. List List::copyRndList()  
  2. {  
  3.         list_node * newhead = NULL;  
  4.         list_node * newcur = NULL;  
  5.         list_node * cur = head;  
  6.         while(cur)  
  7.         {  
  8.                 if(newhead == NULL){  
  9.                         newhead = new list_node;  
  10.                         newhead->value = cur->value;  
  11.                         newhead->next = cur->next;  
  12.                         cur->next = newhead;  
  13.                 }  
  14.                 else{  
  15.                         newcur = new list_node;  
  16.                         newcur->value = cur->value;  
  17.                         newcur->next = cur->next;  
  18.                         cur->next = newcur;  
  19.                 }  
  20.                 cur=cur->next->next;  
  21.         }  
  22.         cur = head;  
  23.         while(cur){  
  24.                 if(cur->rnd)  
  25.                         cur->next->rnd=cur->rnd->next;  
  26.                 else  
  27.                         cur->next->rnd = NULL;  
  28.                 cur = cur->next->next;  
  29.         }  
  30.         cur = head;  
  31.         list_node * dst = cur->next;  
  32.         while(cur)  
  33.         {  
  34.                 list_node * temp = dst->next;  
  35.                 cur->next = temp;  
  36.                 if(temp)dst->next = temp->next;  
  37.                 cur = cur->next;  
  38.                 dst=dst->next;  
  39.         }  
  40.         List newList;  
  41.         newList.head = newhead;  
  42.         return newList;  
  43. }  

4. 找出單向鏈表中中間結點

兩個指針,一個步長為1,另一個步長為2.步長為2的走到底后步長為1的正好到中間。

  1. list_node * List::middleElement()  
  2. {  
  3.         list_node * p = head;  
  4.         list_node * q = head->next;  
  5.         while(q){  
  6.                 p = p->next;  
  7.                 if(q)q=q->next;  
  8.                 if(q)q=q->next;  
  9.         }  
  10. }  

5. 如何檢查一個單向鏈表上是否有環(huán)

同樣兩個指針,一個步長為1,另一個步長為2,如果兩個指針能相遇則有環(huán)。

  1. list_node * List::getJoinPointer()  
  2. {  
  3.   
  4.         if(head == NULL || head->next == NULL)return NULL;  
  5.         list_node * one = head;  
  6.         list_node * two = head->next;  
  7.         while(one != two){  
  8.                 one = one->next;  
  9.                 if(two)two=two->next;  
  10.                 else break;  
  11.                 if(two)two=two->next;  
  12.                 else break;  
  13.         }  
  14.         if(one == NULL || two == NULL)return NULL;  
  15.         return one;  
  16. }  

6. 給定單鏈表(head),如果有環(huán)的話請返回從頭結點進入環(huán)的第一個節(jié)點。

設鏈表頭到環(huán)入口節(jié)點距離為x,環(huán)入口節(jié)點到兩個指針相遇節(jié)點距離為z,換長度為y,則有x+z+1=y,所以z=y-1-x,即一個指針從鏈表頭部開始移動,一個指針兩個指針相遇后一個節(jié)點開始移動,相遇的地方即為環(huán)入口

  1. list_node * List::findCycleEntry()  
  2. {  
  3.         if(checkCycle()==false)return NULL;  
  4.         list_node * joinPointer = getJoinPointer();  
  5.         list_node * p = head;  
  6.         list_node * q = joinPointer->next;  
  7.         while(p!=q)  
  8.         {  
  9.                 p=p->next;  
  10.                 q=q->next;  
  11.         }  
  12.         return p;  
  13. }  

7.只給定單鏈表中某個結點p(并非最后一個結點,即p->next!=NULL)指針,刪除該結點。

將p后面那個節(jié)點的值復制到p,刪除p后面的節(jié)點

  1. void List::deleteByPointer(list_node * node)  
  2. {  
  3.         if(node)  
  4.         {  
  5.                 if(node->next){  
  6.                         node->value = node->next->value;  
  7.                         node->next = node->next->next;  
  8.                 }  
  9.         }  
  10. }  

8.在p前面插入一個節(jié)點

在p后面插入新節(jié)點,將p的值與新建的節(jié)點值互換。

 

9.給定單鏈表頭結點,刪除鏈表中倒數(shù)第k個結點

一個指針指向鏈表頭,另一個指針指向第k個指針,然后兩個指針一起移動,第二個指針到了末端則第一個指針就是倒數(shù)第k個節(jié)點