数据结构之链表操作

自已动手写了一个小程序,实现链表相关的操作。

首先定义结构体,列出相关的要实现的操作,这样有利于整体思考程序结构。虽然现在程序不大:

 1 #ifndef __LIST_H

 2 #define __LIST_H

 3 

 4 #include <stdio.h>

 5 #include <stdlib.h>

 6 #include <assert.h>

 7 #include <string.h>

 8 #include "platform.h"

 9 

10 typedef struct _data

11 {

12     uint16 id;

13     uint8 age;

14     char* name;

15 }dataEntry;

16 

17 typedef struct _list

18 {

19     dataEntry data;

20     struct _list *prev;

21     struct _list *next;

22 }list;

23 

24 Ret_Status list_init(list* plist);

25 Ret_Status list_insert(list* plist,list* data);

26 Ret_Status list_remove(list* plist,uint8 id);

27 uint8 list_sizeInfo(list* plist);

28 

29 #endif

以下是具体的实现,注意一些边界条件还需要完善。我只是测试了常规情况下的工作,还正常:

  1 #include "list.h"

  2 #include "platform.h"

  3 

  4 Ret_Status list_init(list* plist)

  5 {

  6     if(plist==NULL)

  7     {

  8         ERROR("List handle error!\n");

  9         return FAIL;

 10     }

 11     

 12     plist->data.id=0;

 13     plist->data.age=0;

 14     plist->data.name=NULL;

 15     plist->prev=NULL;

 16     plist->next=NULL;

 17     

 18     return SUCCESS;

 19 }

 20 

 21 list* list_createNode(dataEntry* data)

 22 {

 23     list* node=NULL;

 24     

 25     node=(list*)malloc(sizeof(list));

 26     assert(node!=NULL);

 27     memset(node,0x0,sizeof(list));

 28     

 29     node->data.id=data->id;

 30     node->data.age=data->age;

 31     node->data.name=data->name;

 32     

 33     //DEBUG("id:%d, age:%d, name:%s\n",node->data.id,node->data.age,node->data.name);

 34     return node;

 35 }

 36 

 37 list* list_findNode(list* plist,uint8 id)

 38 {

 39     list* node=NULL;

 40     

 41     if(plist==NULL)

 42     {

 43         ERROR("List is null,please check!\n");

 44         return NULL;

 45     }

 46     

 47     node=plist;

 48     while(node!=NULL)

 49     {

 50         if(node->data.id == id)

 51         {

 52             DEBUG("find node!\n");

 53             return node;

 54         }

 55         node=node->next;

 56     }

 57     

 58     return NULL;

 59 }

 60 

 61 /*

 62 * @func:        list_insert()

 63 * @return:      SUCCESS or FAIL

 64 * @descript:    insert data by data id. 

 65                 It is always inserted by id order from little to bigger 

 66 */

 67 Ret_Status list_insert(list* plist,list* pdata)

 68 {

 69     list* plistIndex = NULL;

 70     list* plistNode = NULL;     //swap struct point use.

 71     

 72     if(plist==NULL)

 73     {

 74         DEBUG("List empty,create it!");

 75         plist=list_createNode(&(pdata->data));

 76         return SUCCESS;

 77     }

 78     

 79     if(pdata==NULL)

 80     {

 81         ERROR("Data is invalid!\n");

 82         return FAIL;

 83     }

 84 

 85     plistIndex = plist;

 86     

 87     if(NULL != list_findNode(plist,pdata->data.id))

 88     {

 89         DEBUG("data already exist in list.\n");

 90         return FAIL;

 91     }

 92     

 93     while(plistIndex->next!=NULL&&(plistIndex->data.id < pdata->data.id))

 94     {

 95         //DEBUG("List id:%d, Data id:%d\n",plistIndex->data.id, pdata->data.id);

 96         plistIndex=plistIndex->next;

 97     }

 98     

 99     plistNode=plistIndex->next;

100     plistIndex->next = pdata;

101     pdata->next=plistNode;

102     

103     return SUCCESS;

104 }

105 

106 Ret_Status list_remove(list* plist,uint8 id)

107 {

108     list* node=NULL;

109     

110     if(plist==NULL)

111     {

112         ERROR("List handle error!");

113         return FAIL;

114     }

115     

116     node=list_findNode(plist,id);

117     

118     //remove node is header, shoule special operation.

119     if(node == plist)

120     {

121         plist=node->next;

122         node->prev=NULL;

123         node->next=NULL;

124     }

125     else

126     {

127         node->prev->next=node->next;

128         node->prev=NULL;

129         node->next=NULL;

130     }

131     

132     free(node);

133     

134     return SUCCESS;

135 }

136 

137 Ret_Status list_dump(list* plist)

138 {

139     assert(plist!=NULL);

140     

141     while(plist!=NULL)

142     {

143         printf("id:%d,age:%d,name:%s\n",plist->data.id,plist->data.age,plist->data.name);

144         plist=plist->next;

145     }

146     

147     return SUCCESS;

148 }

下面是测试程序:

 1 #include "list.h"

 2 

 3 #define MAX_COUNT   3

 4 

 5 dataEntry data[MAX_COUNT]=

 6 {

 7     {12,28,"ZHY"},

 8     {13,29,"BZI"},

 9     {16,26,"QIU"},

10 };

11 

12 int main(int argc,char* argv[])

13 {

14     uint i=0;       //for loop use;

15     Ret_Status ret;

16     list* plist;

17     list* node;

18     

19     plist=(list*)malloc(sizeof(list));

20     ret = list_init(plist);

21     

22     if(!ret)

23     {

24         ERROR("List init error!\n");

25         return FAIL;

26     }

27     

28     for(i=0;i<MAX_COUNT;i++)

29     {

30         node=(list*)list_createNode((dataEntry*)&data[i]);

31         list_insert(plist,node);

32     }

33     

34     list_dump(plist);  

35     

36     return SUCCESS;

37 }

输出结果如下:

id:0,age:0,name:(null)  //这个是初始条件。

id:12,age:28,name:ZHY

id:13,age:29,name:BZI

id:16,age:26,name:QIU

 

 

你可能感兴趣的:(数据结构)