首先我们来解释一下这三个词,即双向、带头和循环。
首先双向即我们可以从链表的一个节点向前找到前一个节点的地址,也可以向后找到后一个节点的地址,这其实也暗示了我们双向链表的结构需要多加入一个指针变量。
而带头即我们的哨兵位节点,他不是我们单链表里面的第一个节点的头结点,而是在链表之外的一个节点,他不存储有效的数据,但是会链接在我们的循环列表里,以防止我们的遍历链表出现死循环。
这也就引出了我们最后要说的循环,即这个链表是一个环,头连结着尾巴(想到了苯环和衔尾蛇hhhhh),而这就会引起一些问题,比如要是打印这个链表我们的循环打印应该怎么开始和结束,要查找节点的话头定在哪,因为环的话每个节点都可以是头和尾,而上面的哨兵位节点就相当于给我们定义了头和尾节点。
struct ListNode
{
struct ListNode* prev;//指向前一个节点的指针
struct ListNode* next;//指向后一个节点的指针
int data; //存储的数据
};
以下为双向链表的实战操作
//头文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode* next;
struct ListNode* prev;
}LNode;
void LNodeInit(LNode** pphead);//初始化双向链表
LNode* BuyListNode(DataType x);//增加新链表
void Print(LNode* phead);//打印链表
void LNodePushBack(LNode* phead, DataType x);//尾插
void LNodePushFront(LNode* phead, DataType x);//头插
LNode* LNodeSearch(LNode* phead, DataType x);//查x
void LNodeRewrite(LNode* phead, DataType x, DataType y);//改x为y
void LNodeDelete(LNode* phead, DataType x);//删除x
//源文件进行定义
#include"List.h"
LNode* BuyListNode(DataType x)
{
LNode* new = (LNode*)malloc(sizeof(LNode));
if (new == NULL)
{
perror("malloc");
exit(1);
}
new->data = x;
new->next = new->prev = new;
return new;
}
void Print(LNode* phead)
{
LNode* pcur = phead->next;
//printf("%d->", phead->data);
while (pcur != phead)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("%d\n",phead->next->data);
}
void LNodeInit(LNode** pphead)
{
*pphead = BuyListNode(114514);
}
void LNodePushBack(LNode* phead, DataType x)
{
assert(phead);
LNode* newnode = BuyListNode(x);
newnode->next = phead;
newnode->prev = phead->prev;
phead->prev->next = newnode;
phead->prev = newnode;
}
void LNodePushFront(LNode* phead, DataType x)
{
assert(phead);
LNode* newnode = BuyListNode(x);
newnode->prev = phead;
newnode->next = phead->next;
phead->next->prev = newnode;
phead->next = newnode;
}
LNode* LNodeSearch(LNode* phead, DataType x)
{
assert(phead);
LNode* pcur = phead->next;
while (pcur != phead)
{
if (pcur->data == x)
{
printf("Your data has been found!\n");
return pcur;
}
pcur = pcur->next;
}
printf("Your data is not in the chain\n");
return phead;
}
void LNodeRewrite(LNode* phead, DataType x, DataType y)
{
assert(phead && phead->next);
LNode* pcur = (phead)->next;
LNode* find = LNodeSearch(phead,x);
if (find == phead)
{
printf("Data doesn't exist!\n");
exit(1);
}
find->data = y;
printf("Your data has been rewrited!\n");
}
void LNodeDelete(LNode* phead, DataType x)
{
assert(phead && phead->next);
LNode* pcur = phead->next;
LNode* find = LNodeSearch(phead, x);
if (find == phead)
{
printf("Data doesn't exist!\n");
exit(1);
}
find->prev->next = find->next;
find->next->prev = find->prev;
free(find);
find = NULL;
printf("Your data has been deleted!\n");
}
//测试文件
#include"List.h"
void test()
{
LNode* test;
LNodeInit(&test);
//Print(test);
LNodePushBack(test,1);
Print(test);
LNodePushBack(test,2);
Print(test);
LNodePushBack(test,3);
Print(test);
LNodePushFront(test, 4);
Print(test);
LNodePushFront(test, 5);
Print(test);
LNodePushFront(test, 6);
Print(test);
LNode* search = LNodeSearch(test, 7);
LNodeRewrite(test, 6, 7);
Print(test);
LNodeDelete(test, 1);
Print(test);
}
int main()
{
test();
return 0;
}