typeStructDefine.h
typedef struct DuLNode {
int data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;
typedef int Status;
//初始化一个空指针
Status ListInit_Dul(DuLinkList *L);
//向双向链表中插入元素
Status ListInsert_DuL(DuLinkList *L,int i,int e);
//在双向链表中删除元素
Status ListDelete_DuL(DuLinkList *L,int i);
//输出双向链表中的内容
Status OutPut_DuLinkList(DuLinkList L);
FunctionRealize.c
#include "TypeStructDefine.h"
#include "stdlib.h"
#include "stdio.h"
//初始化一个空的双向链表
Status ListInit_Dul(DuLinkList *L) {
*L = (DuLinkList)malloc(sizeof(DuLNode));
(*L)->next = NULL;
}
//双向链表在第i个位置插入元素
Status ListInsert_DuL(DuLinkList *L, int i, int e) {
DuLinkList p,node;
//插入时需要判断是否超出界限
node = (DuLinkList*)malloc(sizeof(DuLNode));
node->data = e;
p = *L;
int j = 0;
while (p&&j<i-1)
{
j++;
p = p->next;
}
node->next = p->next;
if (p->next != NULL) {
p->next->prior = node;
}
node->prior = p;
p->next = node;
}
//删除元素
Status ListDelete_DuL(DuLinkList *L, int i) {
DuLinkList p;
//插入时需要判断是否超出界限
p = *L;
int j = 0;
while (p&&j<i - 1)
{
j++;
p = p->next;
}
p->next = p->next->next;
p->next->prior = p;
}
Status OutPut_DuLinkList(DuLinkList L) {
DuLinkList p;
p = L->next;
while (p)
{
printf("对应的值为%d
",p->data);
p = p->next;
}
}
DuLinkList.c
#include "stdio.h"
#include "stdlib.h"
#include "TypeStructDefine.h"
void main() {
//指针类型
DuLinkList L;
//初始化一个空的链表
ListInit_Dul(&L);
ListInsert_DuL(&L,1,1);
ListInsert_DuL(&L,2,2);
ListInsert_DuL(&L,3,3);
ListInsert_DuL(&L,2,4);
ListDelete_DuL(&L,2);
OutPut_DuLinkList(L);
//printf("输出链表的第一个值为%d
",L->next->data);
}