双向链表->非循环

  11)定义双向链表的基本结构
  2     typedef struct _DOUBLE_LINK_NODE  
  3     {  
  4         int data;  
  5         struct _DOUBLE_LINK_NODE* prev;  
  6         struct _DOUBLE_LINK_NODE* next;  
  7     }DOUBLE_LINK_NODE;  
  8 
  92)创建双向链表节点
 10 [cpp] view plaincopy
 11 
 12     DOUBLE_LINK_NODE* create_double_link_node(int value)  
 13     {  
 14         DOUBLE_LINK_NODE* pDLinkNode = NULL;  
 15         pDLinkNode = (DOUBLE_LINK_NODE*)malloc(sizeof(DOUBLE_LINK_NODE));  
 16         assert(NULL != pDLinkNode);  
 17       
 18         memset(pDLinkNode, 0, sizeof(DOUBLE_LINK_NODE));  
 19         pDLinkNode->data = value;  
 20         return pDLinkNode;  
 21     }  
 22 
 233)删除双向链表
 24 [cpp] view plaincopy
 25 
 26     void delete_all_double_link_node(DOUBLE_LINK_NODE** pDLinkNode)  
 27     {  
 28         DOUBLE_LINK_NODE* pNode;  
 29         if(NULL == *pDLinkNode)  
 30             return ;  
 31       
 32         pNode = *pDLinkNode;  
 33         *pDLinkNode = pNode->next;  
 34         free(pNode);  
 35         delete_all_double_link_node(pDLinkNode);  
 36     }  
 37 
 384)在双向链表中查找数据
 39 [cpp] view plaincopy
 40 
 41     DOUBLE_LINK_NODE* find_data_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode, int data)  
 42     {  
 43         DOUBLE_LINK_NODE* pNode = NULL;  
 44         if(NULL == pDLinkNode)  
 45             return NULL;  
 46       
 47         pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
 48         while(NULL != pNode){  
 49             if(data == pNode->data)  
 50                 return pNode;  
 51             pNode = pNode ->next;  
 52         }  
 53           
 54         return NULL;  
 55     }  
 56 
 575)双向链表中插入数据
 58 [cpp] view plaincopy
 59 
 60     STATUS insert_data_into_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int data)  
 61     {  
 62         DOUBLE_LINK_NODE* pNode;  
 63         DOUBLE_LINK_NODE* pIndex;  
 64       
 65         if(NULL == ppDLinkNode)  
 66             return FALSE;  
 67       
 68         if(NULL == *ppDLinkNode){  
 69             pNode = create_double_link_node(data);  
 70             assert(NULL != pNode);  
 71             *ppDLinkNode = pNode;  
 72             (*ppDLinkNode)->prev = (*ppDLinkNode)->next = NULL;  
 73             return TRUE;  
 74         }  
 75       
 76         if(NULL != find_data_in_double_link(*ppDLinkNode, data))  
 77             return FALSE;  
 78       
 79         pNode = create_double_link_node(data);  
 80         assert(NULL != pNode);  
 81       
 82         pIndex = *ppDLinkNode;  
 83         while(NULL != pIndex->next)  
 84             pIndex = pIndex->next;  
 85       
 86         pNode->prev = pIndex;  
 87         pNode->next = pIndex->next;  
 88         pIndex->next = pNode;  
 89         return TRUE;  
 90     }  
 91 
 926)双向链表中删除数据
 93 [cpp] view plaincopy
 94 
 95     STATUS delete_data_from_double_link(DOUBLE_LINK_NODE** ppDLinkNode, int data)  
 96     {  
 97         DOUBLE_LINK_NODE* pNode;  
 98         if(NULL == ppDLinkNode || NULL == *ppDLinkNode)  
 99             return FALSE;  
100       
101         pNode = find_data_in_double_link(*ppDLinkNode, data);  
102         if(NULL == pNode)  
103             return FALSE;  
104       
105         if(pNode == *ppDLinkNode){  
106             if(NULL == (*ppDLinkNode)->next){  
107                 *ppDLinkNode = NULL;  
108             }else{  
109                 *ppDLinkNode = pNode->next;  
110                 (*ppDLinkNode)->prev = NULL;  
111             }  
112       
113         }else{  
114             if(pNode->next)  
115                 pNode->next->prev = pNode->prev;  
116             pNode->prev->next = pNode->next;  
117         }  
118       
119         free(pNode);  
120         return TRUE;  
121     }  
122 
1237)统计双向链表中数据的个数
124 [cpp] view plaincopy
125 
126     int count_number_in_double_link(const DOUBLE_LINK_NODE* pDLinkNode)  
127     {  
128         int count = 0;  
129         DOUBLE_LINK_NODE* pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
130       
131         while(NULL != pNode){  
132             count ++;  
133             pNode = pNode->next;  
134         }  
135         return count;  
136     }  
137 
1388)打印双向链表中数据
139 [cpp] view plaincopy
140 
141     void print_double_link_node(const DOUBLE_LINK_NODE* pDLinkNode)  
142     {  
143         DOUBLE_LINK_NODE* pNode = (DOUBLE_LINK_NODE*)pDLinkNode;  
144       
145         while(NULL != pNode){  
146             printf("%d\n", pNode->data);  
147             pNode = pNode ->next;  
148         }  
149     }  
150 
151     注意:
152 
153         今天我们讨论的双向链表是非循环的,大家可以考虑一下如果改成循环双向链表,应该怎么写?如果是有序的循环双向链表,又该怎么写?

posted on 2012-11-01 16:37  凌峰布衣  阅读(633)  评论(0编辑  收藏  举报

导航