#ifndef __LINKE_H__
#define __LINKE_H__
typedef int datatype;
union lost
{
datatype data; //有效数据节点中的数据使用
int len; //头结点中的数据使用
};
typedef struct node{ //指针域指向下一个节点
union lost text; //数据域,若是头结点,使用text
struct node* next; //指针域,存储下一个节点的位置
struct node * prev;
}linkelist;
linkelist* create_double(void);
void send_nont(linkelist* heand,datatype num);
void nont_send(linkelist*heand,datatype data);
void insderlen_lent(linkelist* heand,datatype data,int pos);
void maloc_funt(linkelist*heand);
void funt_maloc(linkelist*heand);
void like_like(linkelist* heand, int pos);
void rede_funct(linkelist*heand);
#endif
#include <stdio.h>
#include "./001double.h"
int main(int argc, const char *argv[])
{
linkelist* heand = create_double();
send_nont(heand,500);
send_nont(heand,600);
nont_send(heand,500);
nont_send(heand,700);
insderlen_lent(heand,888,1);
insderlen_lent(heand,999,4);
insderlen_lent(heand,666,7);
maloc_funt(heand);
funt_maloc(heand);
like_like(heand,3);
rede_funct(heand);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "./001double.h"
linkelist* create_double(void)
{
linkelist* heand = (linkelist*)malloc(sizeof(linkelist));
if(NULL==heand)
{
printf("创建失败,双向链表\n");
return NULL;
}
heand->text.len=0;
heand->next = NULL;
heand->prev = NULL;
return heand;
}
//判断双向链表是否为空
int isEmpty_link(linkelist* heand)
{
return heand->next == NULL?1:0;
}
//头插
void send_nont(linkelist* heand,datatype num)
{
linkelist* tmep = (linkelist*)malloc(sizeof(linkelist));
if(NULL == tmep)
{
printf("插入失败\n");
return;
}
tmep->text.data = num;
tmep->next = NULL;
tmep->prev = NULL;
//头插法插入数据
if(isEmpty_link(heand))
{
tmep->next = heand->next;
heand->next = tmep;
tmep->prev = heand;
}
else
{
tmep->next = heand->next;
heand->next = tmep;
tmep->next->prev = tmep;
tmep->prev = heand;
}
//更新链表长度
heand->text.len++;
return;
}
//尾插
void nont_send(linkelist*heand,datatype data)
{
linkelist* tmep = (linkelist*)malloc(sizeof(linkelist));
if(NULL == tmep)
{
printf("插入失败\n");
return;
}
tmep->text.data = data;
tmep->next = NULL;
tmep->prev = NULL;
linkelist* p = heand;
while(p->next != NULL)
{
p=p->next;
}
tmep->next = p->next;
p->next = tmep;
tmep->prev = p;
heand->text.len++;
return;
}
//遍历
void rede_funct(linkelist*heand)
{
linkelist* p = heand;
while(p->next != NULL)
{
p=p->next;
printf("%d ",p->text.data);
}
printf("\n");
printf("len=%d\n",heand->text.len);
}
//按位置插入
void insderlen_lent(linkelist* heand,datatype data,int pos)
{
linkelist* tmep = (linkelist*)malloc(sizeof(linkelist));
if(NULL == tmep)
{
printf("插入失败\n");
return;
}
tmep->text.data = data;
tmep->next = NULL;
tmep->prev = NULL;
if(pos<1 || pos>heand->text.len+1)
{
printf("位置非法,插入失败%d\n",pos);
return;
}
linkelist* p = heand;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
if(p->next != NULL)
{
tmep->next = p->next;
p->next = tmep;
tmep->next->prev = tmep;
tmep->prev = p;
}
else
{
tmep->next = p->next;
p->next = tmep;
tmep->prev = p;
}
heand->text.len++;
return;
}
//头删
void maloc_funt(linkelist*heand)
{
linkelist*p = heand;
p = heand->next;
if(p->next ==NULL)
{
printf("头删失败\n");
return;
}
heand->next = p->next;
p->next->prev = heand;
free(p);
heand->text.len--;
}
//尾删
void funt_maloc(linkelist*heand)
{
linkelist* p = heand;
if(p->next == NULL)
{
printf("尾删失败\n");
return;
}
while(p->next != NULL)
{
p=p->next;
}
p->prev->next = NULL;
free(p);
heand->text.len--;
}
//按位删除
void like_like(linkelist* heand, int pos)
{
linkelist* p = heand;
for(int i=0;i<pos;i++)
{
p=p->next;
}
if(p->next !=NULL)
{
p->prev->next = p->next;
p->next->prev = p->prev;
}
if(p->next == NULL)
{
p->prev->next = p->next;
}
free(p);
heand->text.len--;
}