#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
typedef struct dnode
{
int data;
struct dnode *next, *prior;
} dnode, *dlinklist;
bool initlinklist(dlinklist &l)
{ // 初始化双链表
l = (dnode *)malloc(sizeof(dnode)); // 分配链表
if (l == NULL)
{ // 如果分配的内存失败的话就返回false
return false;
}
l->prior = NULL; // 前驱后继都为NULL
l->next = NULL;
}
// 判断双链表是否为空(带头结点)
bool empty(dlinklist l)
{
if (l->next == NULL)
{ // 第一个l的next 是否为null;
return true;
}
else
{
return false;
}
}
//后插法,在p之后插入s的结点
bool insernextdnode(dnode *p, dnode *s) //在执行插入的时候要注意顺序,推荐先处理后面两个再处理前面两个
{ if(p==NULL || s== NULL){
return false; //说p和s是非法的参数。
}
s->next = p->next; // 要插入的s,他的后继指向p的后继
if(p->next !=NULL){ //判断p是不是最后一个结点
p->next->prior = s; // 因为这里只输入了p,假如说p下一个是s,那就是s的前驱指向s
}
// 上面的两个互指操作完成,下面也是两个同样的操作。
s->prior = p;
p->next = s;
}
//删除p的后继结点q
bool deletenextdnode(dnode *p){
if (p==NULL){ //先判断当前结点是不是空
return false;
}
dnode * q = p->next;
if(q==NULL){// 再判断后一个结点是不是为空
return false ;
}
p->next = q->next;
if(q->next != NULL){ //q有后继不为空
q->next->prior =p;//q后 前 指向p
}
free(q); //释放q;
return true;
}
int main()
{
dlinklist l;
initlinklist(l);
return 0;
}
408-双链表
最新推荐文章于 2024-09-14 21:53:48 发布