码迷,mamicode.com
首页 > 其他好文 > 详细

05 双向链表

时间:2017-03-14 00:26:18      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:nbsp   printf   技术   clu   div   double   节点   struct   log   

双向链表

技术分享

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 // 数据结构
  6 typedef struct node
  7 {
  8     int data;
  9     struct node *prev;
 10     struct node *next;
 11 }doubleList;
 12 
 13 doubleList *doubleList_init(void)
 14 {
 15     doubleList *list = (doubleList *)malloc(sizeof(doubleList));
 16 
 17     if (list == NULL)
 18     {
 19         return NULL;  
 20     }
 21 
 22     list->prev = list;
 23     list->next = list;
 24 
 25     return list;
 26 }
 27 
 28 int doubleList_add_head(doubleList *list, int data)
 29 {
 30     if (list == NULL)
 31     {
 32         return -1; 
 33     }
 34 
 35     // 记录第一个节点
 36     struct node *tmp = list->next;
 37 
 38     struct node *node = (struct node *)malloc(sizeof(struct node));
 39     if (node == NULL)
 40     {
 41         return -2; 
 42     }
 43 
 44     node->data = data;
 45     node->prev = list;
 46     node->next = tmp;
 47 
 48     list->next = node;
 49     tmp->prev = node;
 50 
 51     return 0;
 52 }
 53 
 54 int doubleList_add_tail(doubleList *list, int data)
 55 {
 56     if (list == NULL)
 57     {
 58         return -1; 
 59     }
 60 
 61     // 记录最后一个节点
 62     struct node *tmp = list->prev;
 63 
 64     struct node *node = (struct node *)malloc(sizeof(struct node));
 65     if (node == NULL)
 66     {
 67         return -2; 
 68     }
 69 
 70     node->data = data;
 71     node->prev = tmp;
 72     node->next = list;
 73 
 74     list->prev = node;
 75     tmp->next = node;
 76 
 77     return 0;
 78 }
 79 
 80 int doubleList_del_head(doubleList *list, doubleList **node)
 81 {
 82     if (list == NULL || list->next == list)
 83     {
 84         return -1; 
 85     }
 86 
 87     *node = list->next;
 88 
 89     // 记录第二个节点
 90     doubleList *tmp = list->next->next;
 91     
 92     list->next = tmp;
 93     tmp->prev = list;
 94     
 95     return 0;
 96 }
 97 
 98 int doubleList_del_tail(doubleList *list, doubleList **node)
 99 {
100     if (list == NULL || list->next == list)
101     {
102         return -1; 
103     }
104 
105     *node = list->prev;
106 
107     // 记录倒数第二个节点
108     doubleList *tmp = list->prev->prev;
109     
110     list->prev = tmp;
111     tmp->next = list;
112     
113     return 0;
114 }
115 
116 int doubleList_printf(doubleList *list)
117 {
118     if (list == NULL || list->next == list)
119     {
120         return -1; 
121     }
122 
123     // 记录下一个节点
124     doubleList *tmp = list->next;
125 
126     while (tmp != list)
127     {
128         printf("%d-", tmp->data); 
129 
130         tmp = tmp->next;
131     }
132     printf("\n");
133 
134     return 0;
135 }
136 
137 int main(void)
138 {
139     doubleList *list = NULL;
140     doubleList *node = NULL;
141     int i = 0;
142 
143     list = doubleList_init();
144 
145     for (i = 0; i < 10; i++)
146     {
147         doubleList_add_head(list, i); 
148     }
149 
150     doubleList_printf(list);
151 
152     for (i = 0; i < 10; i++)
153     {
154         doubleList_add_tail(list, i); 
155     }
156 
157     doubleList_printf(list);
158 
159     doubleList_del_head(list, &node);
160     printf("node:%d\n", node->data);
161     free(node);
162     node = NULL;
163 
164     doubleList_printf(list);
165 
166     doubleList_del_tail(list, &node);
167     printf("node:%d\n", node->data);
168     free(node);
169     node = NULL;
170 
171     doubleList_printf(list);
172 
173     return 0;
174 }

 

05 双向链表

标签:nbsp   printf   技术   clu   div   double   节点   struct   log   

原文地址:http://www.cnblogs.com/gaoningbo/p/6545604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!