1 list.h
#ifndef _LIST_H
#define _LIST_H#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct list_node
{
void *data;
struct list_node * pre; //前
struct list_node* next;//后
}NODE;
typedef struct
{
NODE * head,*last;
int length;
}LIST;
LIST * initLIST();
int insertLIST(LIST * list,void * data,int size);
void print(LIST* list);
#endif
2.list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
//返回指针 ,初始化头尾两个哨兵节点
LIST * initLIST()
{
LIST * list=(LIST*)malloc(sizeof(LIST));
if(list==NULL) exit(0);
list->head =(NODE* )malloc(sizeof(NODE));
memset(list->head,0,sizeof(NODE));
list->last =(NODE* )malloc(sizeof(NODE));
memset(list->last,0,sizeof(NODE));
list->head->next=list->last;
list->last->pre=list->head;
list->length=0;
return list;
}
int insertLIST(LIST * list,void * data,int size)
{
NODE *node=(NODE*)malloc(sizeof(NODE));
node->data=malloc(size);
memcpy(node->data,data,size);
node->next=list->last;
node->pre=list->last->pre;
list->last->pre->next=node;
list->last->pre=node;
list->length++;
return 0;
}
void print(LIST* list)
{
NODE * p=list->head->next;
int a =list->length,i=0;
int *n=&a;
while(p)
{
n=(int*)(p->data);
i++;
printf("n=%d,i=%d\n",*n,i);
p=p->next;
}
}
3.main.cpp
#include "list.h"
int main()
{
int a[]={1,2,3};
LIST * lsit=initLIST();
for(int i=0;i<3;i++)
insertLIST(lsit,&a[i],sizeof(int));
print(lsit);
return 0;
}