最近给自己正在做的项目写了一个链表代码, 主要用于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
#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 *l = (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 *n = 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;
}
#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 *l = (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 *n = 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 *i = 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 ;
}
#include "db_list.h"
static void print_list(db_list * list)
{
db_node *i = 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) -c -g $<
db_list.o: db_list.c db_list.h
$(CC) -c -g $<
clean:
rm - rf test
rm -rf *.o
EXE = test
CC = gcc
$(EXE): $(OBJ)
$(CC) -o $@ $^
main.o: main.c db_list.h
$(CC) -c -g $<
db_list.o: db_list.c db_list.h
$(CC) -c -g $<
clean:
rm - rf test
rm -rf *.o