目录
1.定义结构体
typedef struct dlnode { char data; struct dlnode* prior, * next; }DLNode;
对于双向循环链表,与单链表的区别在于:双向循环链表的每一个节点都比单链表多了一个前驱,用来实现双向查找。所以对结构体的定义:用 char 类型的 data 来存放数据元素; *prior , *next 分别来表示前驱和后继。
2.头插法初始化创建双向循环链表
DLNode *Creat_LinkList()//初始化 { char x; DLNode* head, * p; head = (DLNode*)malloc(sizeof(DLNode)); head->prior = head; head->next = head; printf("请输入链表的值:"); scanf_s("%c", &x); while (x != '\n') { p = (DLNode*)malloc(sizeof(DLNode)); p->data = x; p->next = head->next; head->next->prior = p; p->prior = head; head->next = p; scanf_s("%c", &x); } return head; }
对于用头插法创建初始化双向循环链表 ,我们首先为头指针申请动态空间,然后将头指针的前驱和后继都指向头指针自己,接着为暂存指针 p 申请动态空间,将输入数据 x 存到 p->data 中,然后将 p 的后继指向 head 的后继即(head自己),接着将 head 的下一个节点的前驱指向 p从而实现链表的首位相连。最后将 p 的前驱指向 head ,head 的后继指向 p ,从而实现链表双向的功能。
3.尾插法初始化创建双向循环链表
DLNode *Creat_LinkList()//初始化 { char x; DLNode* head, * p, * q; head = (DLNode*)malloc(sizeof(DLNode)); q = head; head->prior = head; head->next = head; printf("请输入链表的值:"); scanf_s("%c", &x); while (x != '\n') { p = (DLNode*)malloc(sizeof(DLNode)); p->data = x; q->next = p; p->prior = q; p->next = head; head->prior = p; q = p; scanf_s("%c", &x); } return head; }
对于用尾插法创建初始化双向循环链表 ,我们首先为头指针申请动态空间,令暂存指针 q = head 保持头指针位置不变, 然后将头指针的前驱和后继都指向头指针自己,接着为 p 申请动态空间,将输入数据 x 存到 p->data 中,接着将 q 的后继指向 p ,p 的前驱指向 q ,从而实现链表双向的功能。最后将 p的后继指向 head , head 的前驱指向 p 从而实现链表首位相连。
4.查找第i个结点
DLNode* Get_DLinkList(DLNode* head, int i)//查找第i个结点 { DLNode* p; p = head; for (int j=0;j < i;j++) { if (p->next != head) { p=p->next; } else { return NULL; } } return p; }
此功能在上一个博客已经讲述,此处不在过多阐述。
5.插入
void Insert_DLinkLIst(DLNode* head, int i, char x)//插入 { DLNode* p, * q; q = Get_DLinkList(head, i-1); if (q == NULL) { printf("插入错误"); } p = (DLNode*)malloc(sizeof(DLNode)); p->data = x; p->next = q->next; q->next->prior = p; p->prior = q; q->next = p; }
对于插入操作,我们调用 Get_LinkList 函数来将暂存指针定位到第 i-1 的位置,然后为 p 申请动态空间,将输入数据 x 存到 p->data 中,然后将 p 的后继指向 q 的下一个节点,q 的下一个节点的前驱指向 p ,p 的前驱指向 q ,q 的后继指向 p 。
6.删除
void Del_DLinkList(DLNode* head, int i)//删除 { DLNode* p; p = Get_DLinkList(head, i); if (p == NULL) { printf("删除错误"); } p->prior->next = p->next; p->next->prior = p->prior; free(p); }
对于删除操作,我们同样调用 Get_LinkList 函数来将暂存指针定位到第 i 的位置,然后将 p 的上一个节点的后继指向 p 的下一个节点,将 p 的后一个节点的前驱指向 p 的上一个节点,最后释放 p 节点。
7.打印输出
void print(DLNode* head)//打印输出 { DLNode* p; p= head->next; while (p != head) { printf("%c", p->data); p = p->next; } printf("\n"); }
此功能在上一个博客已经讲述,此处不在过多阐述。
8.源代码
尾插法
#include<stdio.h>
#include<stdlib.h>
typedef struct dlnode
{
char data;
struct dlnode* prior, * next;
}DLNode;
DLNode *Creat_LinkList()//初始化
{
char x;
DLNode* head, * p, * q;
head = (DLNode*)malloc(sizeof(DLNode));
q = head;//尾插法
head->prior = head;
head->next = head;
printf("请输入链表的值:");
scanf_s("%c", &x);
while (x != '\n')
{
/*p = (DLNode*)malloc(sizeof(DLNode));
p->data = x;
p->next = head->next;
head->next->prior = p;
p->prior = head;
head->next = p;
scanf_s("%c", &x);*/
p = (DLNode*)malloc(sizeof(DLNode));
p->data = x;
q->next = p;
p->prior = q;
p->next = head;
head->prior = p;
q = p;
scanf_s("%c", &x);
}
return head;
}
void print(DLNode* head)//打印输出
{
DLNode* p;
p= head->next;
while (p != head)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
DLNode* Get_DLinkList(DLNode* head, int i)//查找第i个结点
{
DLNode* p;
p = head;
for (int j=0;j < i;j++)
{
if (p->next != head)
{
p=p->next;
}
else
{
return NULL;
}
}
return p;
}
void Insert_DLinkLIst(DLNode* head, int i, char x)//插入
{
DLNode* p, * q;
q = Get_DLinkList(head, i-1);
if (q == NULL)
{
printf("插入错误");
}
p = (DLNode*)malloc(sizeof(DLNode));
p->data = x;
p->next = q->next;
q->next->prior = p;
p->prior = q;
q->next = p;
}
void Del_DLinkList(DLNode* head, int i)//删除
{
DLNode* p;
p = Get_DLinkList(head, i);
if (p == NULL)
{
printf("删除错误");
}
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
int main()
{
int i;
char x;
DLNode* L, * p;
L = Creat_LinkList();
print(L);
printf("请输入要查找的节点i");
scanf_s("%d", &i);
p = Get_DLinkList(L, i);
if (p == NULL)
{
printf("第i个节点不存在\n");
}
else
{
printf("第i个节点的值为%c\n", p->data);
}
printf("请输入要插入的位置和值:");
scanf_s("%d %c", &i, &x);
Insert_DLinkLIst(L, i, x);
print(L);
printf("请输入要删除的位置:");
scanf_s("%d", &i);
Del_DLinkList(L, i);
print(L);
return 0;
}
头插法
#include<stdio.h>
#include<stdlib.h>
typedef struct dlnode
{
char data;
struct dlnode* prior, * next;
}DLNode;
DLNode *Creat_LinkList()//初始化
{
char x;
DLNode* head, * p;
head = (DLNode*)malloc(sizeof(DLNode));
head->prior = head;
head->next = head;
printf("请输入链表的值:");
scanf_s("%c", &x);
while (x != '\n')
{
p = (DLNode*)malloc(sizeof(DLNode));
p->data = x;
p->next = head->next;
head->next->prior = p;
p->prior = head;
head->next = p;
scanf_s("%c", &x);
}
return head;
}
void print(DLNode* head)//打印输出
{
DLNode* p;
p= head->next;
while (p != head)
{
printf("%c", p->data);
p = p->next;
}
printf("\n");
}
DLNode* Get_DLinkList(DLNode* head, int i)//查找第i个结点
{
DLNode* p;
p = head;
for (int j=0;j < i;j++)
{
if (p->next != head)
{
p=p->next;
}
else
{
return NULL;
}
}
return p;
}
void Insert_DLinkLIst(DLNode* head, int i, char x)//插入
{
DLNode* p, * q;
q = Get_DLinkList(head, i-1);
if (q == NULL)
{
printf("插入错误");
}
p = (DLNode*)malloc(sizeof(DLNode));
p->data = x;
p->next = q->next;
q->next->prior = p;
p->prior = q;
q->next = p;
}
void Del_DLinkList(DLNode* head, int i)//删除
{
DLNode* p;
p = Get_DLinkList(head, i);
if (p == NULL)
{
printf("删除错误");
}
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
int main()
{
int i;
char x;
DLNode* L, * p;
L = Creat_LinkList();
print(L);
printf("请输入要查找的节点i");
scanf_s("%d", &i);
p = Get_DLinkList(L, i);
if (p == NULL)
{
printf("第i个节点不存在\n");
}
else
{
printf("第i个节点的值为%c\n", p->data);
}
printf("请输入要插入的位置和值:");
scanf_s("%d %c", &i, &x);
Insert_DLinkLIst(L, i, x);
print(L);
printf("请输入要删除的位置:");
scanf_s("%d", &i);
Del_DLinkList(L, i);
print(L);
return 0;
}
9. 执行结果
尾插法
头插法