自己写的一个通用链表

最近给自己正在做的项目写了一个链表代码, 主要用于PRO*C的select数据查询结果的存储. 发到上面来分享一下, 代码很短.

db_list.h:

#ifndef DB_LIST_H
#define DB_LIST_H



/* List node structure */
typedef 
struct  db_list_node
{
    
void *data;                /* Store the date */

    
struct db_list_node *next; /* Point to next node */
    
struct db_list_node *prev; /* Point to previous node */
}db_node;

/* List structure */
typedef 
struct  db_list
{
    db_node 
*
head;
    db_node 
*
cur;
}db_list;

/*
*
 * Initialize the database list.
 *
 * return the database list.
 
*/

db_list 
* init_db_list();

/*
*
 * Insert a data structure into a database list.
 *
 * data: the data structure
 * list: the database list
 *
 * return the database list node.
 
*/

db_node 
*add_to_list(void *data, db_list * list);

/*
*
 * Clear the database list and release the memory.
 *
 * list: the database list
 
*/

void clear_db_list(db_list * list);


#endif

 db_list.c:

#include <stdlib.h>
#include 
"db_list.h"

/* *
 * Initialize a database list.
 *
 * return the database list.
 * if return value is NULL, means initilization error.
 
*/

db_list 
* init_db_list()
{
    
/* Alloc memory for database list */

    db_list 
*= (db_list *)malloc(sizeof (db_list));
    l
->head =
 NULL;
    l
->cur =
 NULL;

    
/* Alloc memory for data base list head node */

    l
->head = (db_node *)malloc(sizeof (db_node));
    l
->head->data =
 NULL;
    l
->head->prev =
 NULL;
    l
->head->next =
 NULL;

    
/* cur point to head node */

    l
->cur = l-> head;

    
return
 l;
}

/*
*
 * Insert a data structure into a database list.
 *
 * data: the data structure
 * list: the database list
 *
 * return the database list node.
 
*/

db_node 
*add_to_list(void *data, db_list * list)
{
    db_node 
*= NULL; /* Store data node */


    
/* No head */
    
if (list ==  NULL)
        
return
 NULL;

    
/* Incorrect data */

    
if (data ==  NULL)
        
return
 NULL;

    
/* Alloc memory for data */

    n 
= (db_node *)malloc(sizeof (db_node));
    n
->data =
 data;
    n
->prev =
 NULL;
    n
->next =
 NULL;

    
/* Move current pointer of database list */

    list
->cur->next =  n;
    n
->prev = list->
cur;

    list
->cur = list->cur->
next;

    
return list->
cur;
}

/*
*
 * Clear the database list and release the memory.
 *
 * list: the database list
 
*/

void clear_db_list(db_list * list)
{
    
while (list->cur != NULL && list->cur != list->
head)
    {
        
/* Move current to previous node */

        list
->cur = list->cur-> prev;
        free(list
->cur->
next);
    }

    
/* Release list head node and assign the pointers to NULL*/

    free(list
-> head);
    list
->head =
 NULL;
    list
->cur =
 NULL;

    free(list);
    list 
=
 NULL;
}

这是main.c文件, 拿几个int型简单的测试了一下, 附带一个简单的print函数, 这个可根据需要简单修改. 没有加到数据结构的标准函数中.

#include <stdio.h>
#include 
"db_list.h"

static void print_list(db_list * list)
{
    db_node 
*=
 NULL;

    
for (i = list->head->next; i != NULL; i = i->
next)
        printf(
"0x%d "*(int *)(i->
data));
}

int
 main()
{
    
int a = 1000
;
    
int b = 2000
;
    
int c = 3000
;
    
int d = 4000
;

    db_list 
*list =
 init_db_list();
    insert_to_list(
&
a, list);
    insert_to_list(
&
b, list);
    insert_to_list(
&
c, list);
    insert_to_list(
&
d, list);

    print_list(list);

    clear_db_list(list);

    
return 0
;
}

Makefile文件如下:

OBJ =  main.o db_list.o
EXE 
=
 test
CC 
=
 gcc

$(EXE): $(OBJ)
    $(CC) 
-o $@ $^


main.o: main.c db_list.h
    $(CC) 
--g $<

db_list.o: db_list.c db_list.h
    $(CC) 
--g $<

clean:
    rm 
- rf test
    rm 
-rf *.o
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值