#include "stdio.h"
#include "stdlib.h" //有malloc()和free()
#include "string.h" //有strcpy()
typedef struct DNode//定义结构体
{
int data; //数据域
struct DNode* next; //指针域
struct DNode* prior;//指针域
}DNode,*DLinkList;
struct DNode* head = NULL;//头指针
struct DNode* end = NULL; //尾指针
//头插法,增加数据至头部
void AddDListHead(int a)
{
struct DNode* tmp = (struct DNode*)malloc(sizeof(struct DNode));//分配空间
tmp->data = a;//赋值
tmp->next = NULL;
tmp->prior = NULL;
//连接分两种 ①一个节点都没有;②已经有节点了,加到头部
if(head == NULL)
{
head = tmp;
end = head;
}
else
{
tmp->next = head;
head->prior = tmp;
}
head = tmp; //头结点始终指向第一个
}
//尾插法,增加数据至尾部
void AddDListTail(int a)
{
struct DNode* tmp = (struct DNode*)malloc(sizeof(struct DNode));//分配空间
tmp->data = a;//赋值
tmp->next = NULL;
tmp->prior = NULL;
//连接分两种 ①一个节点都没有;②已经有节点了,加到尾巴
if(head == NULL)
{
head = tmp;
}
else
{
end->next = tmp;
tmp->prior = end;
}
end = tmp; //尾结点始终指向最后一个
}
//遍历查询链表:设置一个临时变量++操作
void ScanList(
C++ 双链表基本操作
最新推荐文章于 2023-03-17 16:18:23 发布