版权声明:转载请注明出处 https://blog.csdn.net/qq_37689106/article/details/82951020
#include<stdio.h>
#include<stdlib.h>
typedef struct note
{
int xuhao;
struct note *prior;
struct note *next;
}linklist_t;
linklist_t * creat_linklist() //创建一个空的双链表 将头结点的值 指定为 0 指针指向空
{
linklist_t * h ;
h = (linklist_t *)malloc(sizeof(linklist_t));
h->xuhao=0;
h->next = NULL;
h->prior = NULL;
return h;
}
void append_linklist(linklist_t *a,int value) //双向链表的插入 (我是先接上后链,在接上前链)
{
linklist_t *p;
p = (linklist_t *)malloc(sizeof(linklist_t));
p->xuhao = value;
p->next = a->next; //接后链
a->next = p;
p->prior =a ; //接前链
if (p->next !=NULL)
p->next->prior = p;
}
void remove_linklist(linklist_t *h,int value) //删除双向链表的元素(也是先接上的后链 再接上的前链)
{
linklist_t *p = h->next;
while(p != NULL)
{
if (p->xuhao == value)
{
if (p->prior != NULL) p->prior->next = p->next;
if (p->next != NULL) p->next->prior = p->prior;
free(p);
break;
}
p = p->next;
}
}
void modify_linklist(linklist_t *h,int ol_value,int ne_value) //修改链表的值
{
linklist_t *p = h ;
while(p->next != NULL)
{
if (p->next->xuhao == ol_value)
{
p->next->xuhao = ne_value;
break;
}
p = p->next;
}
}
int search_linklist(linklist_t *h, int value) //查找是否在链表中是否有值
{
linklist_t *p = h;
int ret ;
while(p->next != NULL)
{
if (p->next->xuhao == value)
{
ret = 0;
break;
}
else
{
ret = -1;
}
p = p->next;
}
return ret;
}
void linklist_t_show(linklist_t *h) //打印全部内容
{
linklist_t *p = h;
while(p->next !=NULL)
{
printf("%d ",p->next->xuhao);
p = p->next;
}
printf("\n");
while(p->prior != NULL)
{
printf("%d ",p->xuhao);
p = p->prior;
}
printf("\n");
}
int main()
{
linklist_t *H =creat_linklist();
int i,ret;
for(i=0;i<10;i++)
{
append_linklist(H,i+1);
}
remove_linklist(H,2);
linklist_t_show(H);
#if 1
ret = search_linklist(H,3);
if (ret == 0)
{
printf("have\n");
}
else
{
printf("have no\n");
}
#endif
return 0;
}