双链表 dNode原创
# 链表(链式存储)
使用链式存储的顺序表,称为链表
。
# 双链表
『双链表』逻辑上相邻的两个元素物理上不一定相邻。也是线性表中的一种,使用链式存储。
::: detail
#include <stdio.h>
#include <stdlib.h>
#define ElementType int //宏 不能由';'结尾
// 使用别名 定义结构体
typedef struct DNode{
ElementType data; // 存储数据类型
struct DNode *prior,*next; // 前驱,后继指针
}DNode,*pDNode; // 指针别名, *pLNode 等价于 struct LNode *
// 初始化双链表
pDNode InitDNode(pDNode L){
L=(pDNode)malloc(sizeof(DNode)); // 头结点
L->prior=NULL;
L->next=NULL;
L->data=0;
}
// 是否为空
bool isEmpty(pDNode L){
if(L->next ==NULL && L->prior==NULL){
return true;
}else{
return false;
}
}
// 头插法
pDNode headCreate(pDNode &L,ElementType e){
pDNode temp;
temp=(pDNode) malloc(sizeof(DNode));
temp->data=e;
temp->next = L->next;
if(L->next != NULL){
L->next->prior = temp;// 插入位置的后一个结点的前驱指向新结点
}
temp->prior = L; // 新结点前驱指向头指针
L->next = temp; // 头结点后继指向新结点
L->data++;
}
// 打印 双链表
void printDNode(pDNode L){
if(isEmpty(L)){
printf("链表为空!\n");
}else{
printf("链表数据为!\n");
for (int i = 0; i < L->data; ++i) {
printf("%4d",L->next->data);
L=L->next;
}
printf("\n");
}
}
// 尾插法
pDNode tailCreate(pDNode &L,ElementType e){
pDNode temp;
temp=(pDNode) malloc(sizeof(DNode));
temp->data=e;
temp->next = L->next;
L->next = temp;
L->data++;
}
int main() {
int x;
printf("链表为空!\n");
return 0;
}
:::
上次更新: 2022/06/30, 14:46:07