環形雙鏈表與蛋疼的鏈表反轉

C代碼
  1.   

錯誤蛋疼的反轉 temp引用的地址已經被改變了所以不能實現反轉

105 void reverse(void)

106 {

107     link node = head->next;

108     link temp;

109     temp = head;

110     head->next = head->pre;

111     head->pre = temp ->next;

112     while(node!=head){

113         temp = node;

114         node->next = node->pre;

115         node->pre = temp->next;

116         node = temp->next;

117     }

118 }

119

120

C代碼
  1. 蛋疼的反轉  
  2. 105 void reverse(void)  
  3. 106 {  
  4. 107     link node = head->next;  
  5. 108     link temp;  
  6. 109     temp = head;  
  7. 110     head->next = head->pre;  
  8. 111     head->pre = temp ->next;  
  9. 112     while(node!=head){  
  10. 113         temp = node->next;  
  11. 114         node->next = node->pre;  
  12. 115         node->pre = temp;  
  13. 116         node = temp;  
  14. 117     }  
  15. 118 }  
  16. 119  
  17. 120  
  18.   
  19. 100  //通過pre照樣能實現反序  
  20. 101     for(; node->pre!=head; node=node->pre){  
  21. 102         printf("node->item=%d\n", node->pre->item);  
  22. 103     }  
  23. 104    
  24.   
  25.   
  26. /* circular.h */                                                                                                              
  27.   2                            
  28.   3 #ifndef CIRCULAR_H  
  29.   4 #define CIRCULAR_H         
  30.   5  
  31.   6 typedef struct node *link;  
  32.   7 struct node {              
  33.   8      int item;             
  34.   9     link pre, next;        
  35.  10 };  
  36.  11     
  37.  12 link make_node( int item);  
  38.  13 void free_node(link p);    
  39.  14 link search( int key);     
  40.  15 void insert(link p);       
  41.  16 void delete(link p);  
  42.  17 void traverse(void (*visit)(link));  
  43.  18 void destroy(void);  
  44.  19 void enqueue(link p);  
  45.  20 link dequeue(void);  
  46.  21 link get_head(void);  
  47.  22 void list_node(void);  
  48.  23 void reverse(void);  
  49.  24 #endif  
  50.   
  51.   
  52.   
  53. /* circular.c */                                                                                                                                                                                                                                                                                                          
  54.   2 #include <stdlib.h>  
  55.   3 #include <stdio.h>  
  56.   4 #include "circular.h"  
  57.   5  
  58.   6 struct node sentinel = {0, &sentinel, &sentinel};  
  59.   7  
  60.   8 static link head = &sentinel;  
  61.   9  
  62.  10 link make_node( int item)  
  63.  11 {  
  64.  12     link p = malloc(sizeof *p);  
  65.  13     p->item = item;  
  66.  14     p->pre  = p->next = NULL;  
  67.  15     return p;  
  68.  16 }  
  69.  17  
  70.  18 void insert(link p)  
  71.  19 {  
  72.  20     p->next = head->next;  
  73.  21     head->next->pre = p;  
  74.  22     head->next = p;  
  75.  23     p->pre = head;  
  76.  24 }  
  77.  25  
  78.  26 void delete(link p)  
  79.  27 {  
  80.  28     p->pre->next = p->next;  
  81.  29     p->next->pre = p->pre;  
  82.  30 }  
  83.  31  
  84.  32  
  85.  33  
  86.  34 void free_node(link p)  
  87.  35 {  
  88.  36     free(p);  
  89.  37 }  
  90.  38  
  91.  39 link search( int key)  
  92.  40 {  
  93.  41     link p;  
  94.  42     for(p=head->next; p!=head; p=p->next){  
  95.  43         if(p->item == key){  
  96.  44             return p;  
  97.  45         }  
  98.  46     }  
  99.  47     return NULL;  
  100.  48 }  
  101.  49  
  102.  50 void traverse(void (*visit)(link))  
  103.  51 {  
  104.  52     link p;  
  105.  53     for(p=head->next; p!=head; p=p->next){  
  106.  54         visit(p);  
  107.  55     }  
  108.  56 }  
  109.  57  
  110.  58  
  111.  59 void destroy(void)  
  112.  60 {  
  113.  61     link q, p = head->next;  
  114.  62     head->next = head;  
  115.  63     head->pre = head;  
  116.  64     while(p!=head){  
  117.  65         q = p;  
  118.  66         p=p->next;  
  119.  67         free_node(q);  
  120.  68     }  
  121.  69 }  
  122.  70  
  123.  71 void enqueue(link p)  
  124.  72 {  
  125.  73     insert(p);  
  126.  74 }  
  127.  75  
  128.  76 link dequeue(void)  
  129.  77 {  
  130.  78     if(head->pre ==head){  
  131.  79         return NULL;  
  132.  80     }else{  
  133.  81         link p = head->pre;  
  134.  82         delete(p);  
  135.  83         return p;  
  136.  84     }  
  137.  85 }  
  138.  86  
  139.  87  
  140.  88 link get_head(void)  
  141.  89 {  
  142.  90     return head;  
  143.  91 }  
  144.  92  
  145.  93 void list_node(void)  
  146.  94 {  
  147.  95     link node = head;  
  148.  96     for(; node->next!=head; node=node->next){  
  149.  97         printf("node->item=%d\n", node->next->item);  
  150.  98     }  
  151.  99 }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章