当前位置:网站首页>双链表:初始化 插入 删除 遍历
双链表:初始化 插入 删除 遍历
2022-07-01 00:32:00 【qq_50104963】
双链表不可随机存取,按位查找,按值查找操作都只能用遍历的方式实现。时间复杂度0(n)
双链表的初始化
//双链表的初始化(带头结点)
typedef struct DNode{ //定义双链表结点类型
ElemType data; //每个节点存放一个数据元素【data被称为数据域】
struct DNode *prior,*next;//前驱和后继指针
}LNode,*LinkList;
//初始化双链表
bool InitDLinkList(DLinkList &L){
L = (DNode *)malloc(sizeof(DNode));//分配一个头结点
if (L==NULL)//内存不足,分配失败
return false;
L->prior = NULL;//头结点的prior永远指向NULL
L->next = NULL;//头结点之后暂时还没有结点
return true;
}
void testDLinkLink(){
//初始化双链表
DLinkLink L;
InitDLinkList(L);
//后续代码。。。
}
//判断双链表是否为空
bool Empty(DLinkList L){
if (L->next == NULL)
return true;
else
return false;
}双链表的插入
【双链表的插入】
//在p结点之后插入s结点
bool InsertNextDNode(DNode *p,DNode *s){
if (p==NULL || s==NULL)//非法参数
return false;
s->next = p->next;
if (p->next != NULL)//如果p结点有后继结点
p->next->prior = s;
s->prior = p;
p->next = s;
return = s;
}【双链表的删除】
【双链表的删除】
//删除p结点的后继结点
bool DeleteNextDNode(DNode *p){
if (p==NULL)
return false;
DNode *q = p->next;//找到p的后继结点q
if (q==NULL)
return false;//p没有后继结点
p->next = q->next;
if (q->next != NULL)//q结点不是最后一个结点
q->next->prior = p;
free(q);//释放空间
return true;
}
void DestoryList(DLinklist &L){
//循环释放各个数据结点
while (L->next != NULL)
DeleteNextDNode(L);
free(L);//释放头结点 摧毁表时才能释放头结点
L =NULL;//头指针指向NULL
}【双链表的遍历】
【双链表的遍历】
//后向遍历
while(p!=NULL){
//对结点p做相应的处理 如:打印
p = p->next;
}
//前向遍历
while(p!=NULL){
//对结点p做相应的处理
p = p->prior;
}
//前向遍历--跳过头结点
while(p->prior!=NULL){
//对结点p做相应的处理
p = p->prior;
}边栏推荐
- What value should testers play in requirements review? Two minutes will stop you from being stupid
- Oracle-数据完整性
- Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908
- IBL预计算的疑问终于解开了
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- P4学习——p4runtime
- 2022-2028 global herbal diet tea industry research and trend analysis report
- [untitled]
- BeanUtils. Copyproperties() vs. mapstruct
- 2022-2028 global electric yacht industry research and trend analysis report
猜你喜欢

Host FL Studio fruit music production daw20.9

2022-2028 global capsule shell industry research and trend analysis report

Confirm() method of window

Two-stage RO: part 1

Gateway service gateway

第53章 从业务逻辑实现角度整体性理解程序

2022-2028 global rampant travel industry research and trend analysis report

New trend of embedded software development: Devops

2022-2028 global single travel industry research and trend analysis report

2022-2028 global elevator emergency communication system industry research and trend analysis report
随机推荐
CentOS install MySQL
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
Bridge emqx cloud data to AWS IOT through the public network
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
2022-2028 global plant peptone industry research and trend analysis report
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
JS bubble sort and select sort
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
What does it mean to open an account online? Is it safe to open an account online?
File reading and writing for rust file system processing - rust Practice Guide
网上开华泰证券的股票账户是否安全呢?
Kubernetes ---- pod configuration container start command
What SQL statements are supported for data filtering
Luogu p1168 median
How do it outsourcing resident personnel position their pain points?
Which is better, server rental or hosting services in the United States?
Luogu p1144 shortest circuit count
Development of wireless U-shaped ultrasonic electric toothbrush
Stack frame
Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions