双向链表与单向链表的区别
1.单向链表因为只有一个指针指向下一个指针,所以只能从前往后找,速度慢
2.为了更迅速,于是就在加了一个前指针,也就变成了双向链表
1) 新节点的前指针指向上一个节点;
2) (新)节点的后指针指向下一个节点;
3.两者写法基本相似,重点要画图理解双向链表的含义
代码
#include<stdio.h>
#include<stdlib.h>
#define Null 0
struct doublelist
{
int date;
struct doublelist* front ;
struct doublelist* tail;
};
struct doublelist* createlist()
{
struct doublelist* headNode = (struct doublelist*)malloc(sizeof(struct doublelist));
headNode->front = headNode->tail = headNode;
return headNode;
}
//创建节点
struct doublelist* createNode(int date)
{
struct doublelist* newNode = (struct doublelist*)malloc(sizeof(struct doublelist));
//创建过程-> 描述最初状态->初始化结构体变量中的成员
newNode->date =date;
newNode->front = Null;
newNode->tail =Null;
}
void insertNodeBytail(struct doublelist* headNode,int date)//用尾部插入
{
struct doublelist* newNode = createNode(date);
struct doublelist* tailNode = headNode->front;
tailNode->tail = newNode;
newNode->front = tailNode;
headNode->front =newNode;
newNode->tail = headNode;
}
void printList (struct doublelist* headNode)
{
struct doublelist* pMove= headNode->tail;
while(pMove->front != headNode->front)
{
printf("%d->",pMove->date);
pMove = pMove->tail ;
}
printf("\n");
}
int main()
{
struct doublelist* list = createlist();
insertNodeBytail(list,1);
insertNodeBytail(list,2);
insertNodeBytail(list,3);
printList(list);
system("pause");
return 0;
}
遇到的困难
第一个看视频看不懂总感觉双指针有些变扭,总是一个点理解不了,但看了两三变视频后一起看一些前人发的文章,慢慢的有了点懂的感觉
C语言双向链表讲解
这个讲解更为详细
B站学习的视频
想法
无