乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      鏈表逆序

       @IT小小鳥@ 2012-03-22

      1. typedef struct tagListNode{  
      2.     int data;  
      3.     struct tagListNode* next;  
      4. }ListNode, *List;  

      要求將一帶鏈表頭List head的單向鏈表逆序。

      分析:

        1). 若鏈表為空或只有一個元素,則直接返回;

        2). 設(shè)置兩個前后相鄰的指針p,q. 將p所指向的節(jié)點作為q指向節(jié)點的后繼;

        3). 重復(fù)2),直到q為空

        4). 調(diào)整鏈表頭和鏈表尾

      示例:以逆序A->B->C->D為例,圖示如下

       

      實現(xiàn)及測試代碼如下:

      [cpp:nogutter] view plaincopy?
      1. #include <stdio.h>  
      2. #include <stdlib.h>  
      3.   
      4. typedef struct tagListNode{  
      5.     int data;  
      6.     struct tagListNode* next;  
      7. }ListNode, *List;  
      8.   
      9. void PrintList(List head);  
      10. List ReverseList(List head);  
      11.   
      12. int main()  
      13. {  
      14.     //分配鏈表頭結(jié)點  
      15.     ListNode *head;  
      16.     head = (ListNode*)malloc(sizeof(ListNode));  
      17.     head->next = NULL;  
      18.     head->data = -1;  
      19.   
      20.     //將[1,10]加入鏈表  
      21.     int i;  
      22.     ListNode *p, *q;  
      23.     p = head;  
      24.     for(int i = 1; i <= 10; i++)  
      25.     {  
      26.         q = (ListNode *)malloc(sizeof(ListNode));  
      27.         q->data = i;  
      28.         q->next = NULL;  
      29.         p->next = q;  
      30.         p = q;          
      31.     }  
      32.   
      33.     PrintList(head);           /*輸出原始鏈表*/  
      34.     head = ReverseList(head);  /*逆序鏈表*/  
      35.     PrintList(head);           /*輸出逆序后的鏈表*/  
      36.     return 0;  
      37. }  
      38.   
      39. List ReverseList(List head)  
      40. {  
      41.     if(head->next == NULL || head->next->next == NULL)    
      42.     {  
      43.        return head;   /*鏈表為空或只有一個元素則直接返回*/  
      44.     }  
      45.   
      46.     ListNode *t = NULL,  
      47.              *p = head->next,  
      48.              *q = head->next->next;  
      49.     while(q != NULL)  
      50.     {          
      51.       t = q->next;  
      52.       q->next = p;  
      53.       p = q;  
      54.       q = t;  
      55.     }  
      56.   
      57.     /*此時q指向原始鏈表最后一個元素,也是逆轉(zhuǎn)后的鏈表的表頭元素*/  
      58.     head->next->next = NULL;  /*設(shè)置鏈表尾*/  
      59.     head->next = p;           /*調(diào)整鏈表頭*/  
      60.     return head;  
      61. }  
      62.   
      63. void PrintList(List head)  
      64. {  
      65.     ListNode* p = head->next;  
      66.     while(p != NULL)  
      67.     {  
      68.         printf("%d ", p->data);  
      69.         p = p->next;  
      70.     }  
      71.     printf("/n");  
      72. }  

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多