C语言数据结构双向链表[领卓教育]
改和查相信大家肯定会,我就不再加入进去了下面是代码:
typedef struct node //创建一个结构体
{
int data;
struct node * prior;
struct node * next;
}linklist_t ;
linklist_t * create_empty_linklist(void) //创建一个空的链表
{
linklist_t * h =(linklist_t *)malloc(sizeof(linklist_t)) ;
h->next = NULL;
h->prior = NULL ;
h->data = 0 ;
return h;
}
int linklist_insert(linklist_t * h,int value) //插入数据
{
linklist_t * p =(linklist_t *)malloc(sizeof(linklist_t)) ;
p->data = value;
p->next = h->next;
h->next = p ;
p->prior = h ;
if(p->next != NULL) p->next->prior = p ;
return 0;
}
int linklist_show(linklist_t *h) //显示数据
{
linklist_t * p = h;
while(p->next != NULL)
{
printf("%4d",p->next->data);
p = p->next ;
}
printf("\n");
while(p->prior != NULL)
{
printf("%4d",p->data);
p = p->prior ;
}
printf("\n");
}
int linklist_remove(linklist_t * h,int value) //删除数据
{
linklist_t * p = h->next;
while(p != NULL)
{
if(p->data == value)
{
if(p->prior != NULL) p->prior->next = p->next ;
if(p->next != NULL) p->next->prior = p->prior;
free(p);
return 0;
}
p = p->next;
}
return 0;
}
int linklist_modify(linklist_t * h,int old,int new) //更改数据
{
linklist_t * p = h->next ;
while(p != NULL)
{
if(p->data == old)
{
p->data = new;
return 0 ;
}
p = p->next ;
}
return 0;
}
int linklist_search(linklist_t * h,int value) //查找数据
{
linklist_t * p = h->next ;
while(p != NULL)
{
if(p->data == value)
{
return 1;
}
p = p->next ;
}
return 0;
}