链表
来源:互联网 发布:linux 判断文件存在 编辑:程序博客网 时间:2024/06/05 20:24
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define CMB_OK 0
#define CMB_ERROR 1
#define CMB_SYSTEM_ERROR 2
struct dl_node {
void *body;
struct dl_node *next;
struct dl_node *prev;
};
typedef struct cs_list {
struct dl_node *head;
struct dl_node *tail
int number;
} CS_LIST;
int CS_List_Init(CS_LIST **ptr)
{
CS_LIST *np;
if( ptr == NULL)
return CMB_ERROR;
np = (CS_LIST *)malloc(sizeof(CS_LIST));
if( np == NULL)
return CMB_SYSTEM_ERR;
np->head = np->tail = NULL;
np->number = 0;
*ptr = np;
return CMB_OK;
}
int CS_List_Append(CS_LSIT **ptr, void *np)
{
struct dl_node *newnode;
if( ptr == NULL || np == NULL)
return CMB_ERROR;
newnode = (struct dl_node *)malloc(sizeof(struct dl_node));
if(newnode == NULL)
return CMB_SYSTEM_ERROR;
newnode->body = np;
newnode->next = newnode->prev = NULL;
if( ptr -> number == 0) {
ptr->head = ptr->tail = newnode;
return CMB_OK;
}
newnode->prev = ptr->tail;
ptr ->tail->next = newnode;
ptr ->tail =newnode;
}
int CS_List_Get(CS_LIST *ptr, size_t at, void **item)
{
int cnt,half_cnt;
struct dl_node *wp;
if(CS_LSIT == NULL || item == NULL)
return CMB_ERROR;
if( at <= 0 || at > ptr->number)
return CMB_ERROR;
half_cnt = cnt / 2;
if(at <= half_cnt) {
cnt = 1;
wp = ptr->head;
while(cnt < at) {
wp = wp->next;
cnt++;
}
*item = wp;
}
else {
cnt = ptr->number;
wp = ptr->tail;
while (cnt > at){
wp = wp->prev;
cnt--;
}
*item = wp;
}
return CMB_OK;
}
先保存一下,明天继续写,有没有人给Check一下,有没有改进的方法,感觉有点错误???