数据结构之链表操作
2013-01-14 22:25
295 查看
自已动手写了一个小程序,实现链表相关的操作。
首先定义结构体,列出相关的要实现的操作,这样有利于整体思考程序结构。虽然现在程序不大:
以下是具体的实现,注意一些边界条件还需要完善。我只是测试了常规情况下的工作,还正常:
下面是测试程序:
输出结果如下:
首先定义结构体,列出相关的要实现的操作,这样有利于整体思考程序结构。虽然现在程序不大:
#ifndef __LIST_H #define __LIST_H #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h> #include "platform.h" typedef struct _data { uint16 id; uint8 age; char* name; }dataEntry; typedef struct _list { dataEntry data; struct _list *prev; struct _list *next; }list; Ret_Status list_init(list* plist); Ret_Status list_insert(list* plist,list* data); Ret_Status list_remove(list* plist,uint8 id); uint8 list_sizeInfo(list* plist); #endif
以下是具体的实现,注意一些边界条件还需要完善。我只是测试了常规情况下的工作,还正常:
#include "list.h" #include "platform.h" Ret_Status list_init(list* plist) { if(plist==NULL) { ERROR("List handle error!\n"); return FAIL; } plist->data.id=0; plist->data.age=0; plist->data.name=NULL; plist->prev=NULL; plist->next=NULL; return SUCCESS; } list* list_createNode(dataEntry* data) { list* node=NULL; node=(list*)malloc(sizeof(list)); assert(node!=NULL); memset(node,0x0,sizeof(list)); node->data.id=data->id; node->data.age=data->age; node->data.name=data->name; //DEBUG("id:%d, age:%d, name:%s\n",node->data.id,node->data.age,node->data.name); return node; } list* list_findNode(list* plist,uint8 id) { list* node=NULL; if(plist==NULL) { ERROR("List is null,please check!\n"); return NULL; } node=plist; while(node!=NULL) { if(node->data.id == id) { DEBUG("find node!\n"); return node; } node=node->next; } return NULL; } /* * @func: list_insert() * @return: SUCCESS or FAIL * @descript: insert data by data id. It is always inserted by id order from little to bigger */ Ret_Status list_insert(list* plist,list* pdata) { list* plistIndex = NULL; list* plistNode = NULL; //swap struct point use. if(plist==NULL) { DEBUG("List empty,create it!"); plist=list_createNode(&(pdata->data)); return SUCCESS; } if(pdata==NULL) { ERROR("Data is invalid!\n"); return FAIL; } plistIndex = plist; if(NULL != list_findNode(plist,pdata->data.id)) { DEBUG("data already exist in list.\n"); return FAIL; } while(plistIndex->next!=NULL&&(plistIndex->data.id < pdata->data.id)) { //DEBUG("List id:%d, Data id:%d\n",plistIndex->data.id, pdata->data.id); plistIndex=plistIndex->next; } plistNode=plistIndex->next; plistIndex->next = pdata; pdata->next=plistNode; return SUCCESS; } Ret_Status list_remove(list* plist,uint8 id) { list* node=NULL; if(plist==NULL) { ERROR("List handle error!"); return FAIL; } node=list_findNode(plist,id); //remove node is header, shoule special operation. if(node == plist) { plist=node->next; node->prev=NULL; node->next=NULL; } else { node->prev->next=node->next; node->prev=NULL; node->next=NULL; } free(node); return SUCCESS; } Ret_Status list_dump(list* plist) { assert(plist!=NULL); while(plist!=NULL) { printf("id:%d,age:%d,name:%s\n",plist->data.id,plist->data.age,plist->data.name); plist=plist->next; } return SUCCESS; }
下面是测试程序:
#include "list.h" #define MAX_COUNT 3 dataEntry data[MAX_COUNT]= { {12,28,"ZHY"}, {13,29,"BZI"}, {16,26,"QIU"}, }; int main(int argc,char* argv[]) { uint i=0; //for loop use; Ret_Status ret; list* plist; list* node; plist=(list*)malloc(sizeof(list)); ret = list_init(plist); if(!ret) { ERROR("List init error!\n"); return FAIL; } for(i=0;i<MAX_COUNT;i++) { node=(list*)list_createNode((dataEntry*)&data[i]); list_insert(plist,node); } list_dump(plist); return SUCCESS; }
输出结果如下:
id:0,age:0,name:(null) //这个是初始条件。 id:12,age:28,name:ZHY id:13,age:29,name:BZI id:16,age:26,name:QIU
相关文章推荐
- 数据结构--链表的操作(顺序结构)
- 一步一步学习数据结构(3)-链表及其操作实战
- 数据结构——单链表及其基本操作
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- C++数据结构之链表操作
- 数据结构(1):单向链表的基本操作
- 数据结构---链表操作(线性结构)
- 寒假第二天--线性表--数据结构上机测试2-1:单链表操作A
- 数据结构:单链表操作之如何判断链表是否带环及相关操作
- 数据结构上机测试2-1:单链表操作A
- 【1138】数据结构上机测试2-1:单链表操作A(顺序建表+删除节点)(SDUT)
- 数据结构:链表操作之反转链表
- C++ 数据结构的单链表的建立,插入,删除操作
- 再回首,数据结构——链表上的常见操作
- 数据结构中双向链表的基本操作(源代码)
- 数据结构之链表(1):单链表基本操作
- 数据结构——链表操作(电话管理系统 )
- 手把手写数据结构之单向链表操作(一)
- 数据结构之链表操作
- 数据结构实验一--单链表的基本操作的算法