#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
//创建节点
Node* makeNode(const int data)
{
Node * p = (Node *)malloc(sizeof(Node));
if( p == NULL )
{
exit(1);
printf("Malloc failed");
}
p->data = data;
p->next = NULL;
p->previous = NULL;
return p;
}
//销毁节点
void destroyNode(Node* node)
{
free((Node *) node);
}
//带头节点的单链表
void dInit(Dlist * list)
{
list->head = makeNode(INI_MIN);
}
//按顺序插入
bool dInsert(Dlist * list, const int data)
{
Node * ptem = list->head;
Node* current;
Node * node;
node = makeNode(data);
if(ptem->data > data)
{
list->head = node;
node->next = ptem;
ptem->previous = node;
} else {
while((current = ptem->next) != NULL && current->data < data )
ptem = ptem->next;
ptem->next = node;
node->previous = ptem;
if( current != NULL)
{
node->next = current;
current->previous = node;
}
}
return true;
}
//移除节点
bool dRemove(Dlist * list, const int key)
{
Node* previous = list->head;
Node* current;
//删除头结点
if( previous->data == key )
{
list->head = previous->next;
return true;
}
//找到删除节点
while( ( current = previous->next ) != NULL && current->data != key)
previous = previous->next;
if(current != NULL)
{
if (current->next != NULL)
{
previous->next = current->next;
current->next->previous = previous;
} else {
previous->next = NULL;
}
free( current );
}
return true;
}
//修改,先删后插入,因为这是有序链表
bool dModify(Dlist * list, const int key, const int data)
{
if( dRemove(list, key) )
dInsert(list, data);
else
return false;
return true;
}
//找到返回关键字的节点,否则返回null指针
Node* dFind(Dlist * list, const int key)
{
Node * current = list->head;
while ( (current = current->next) != NULL && current->data != key)
if( current->data > key )
return NULL;
return current;
}
//遍历
void dTreaverse(Dlist * dlist, void (*func) (Node* p))
{
Node * current = dlist->head;
func(current);
while ( (current = current->next) != NULL )
func(current);
}
//销毁节点
void dDestrory(Dlist * list)
{
dTreaverse(list, destroyNode);
free(list->head);
}
//print
void printData(Node * p)
{
printf("%d ", p->data);
}
基于C语言的数据结构-双链表doubleLinkedList
版权申诉
199 浏览量
2022-06-25
11:22:08
上传
评论
收藏 2KB RAR 举报
Sherry_shiry
- 粉丝: 2
- 资源: 1097
最新资源
- 基于QT+QML+C++实现的仿 Windows10 画图3D 的颜色选择器+源码
- 32代码四驱简易电动小车蓝牙控制、语音控制、寻光、巡线、避障(内含语音模块为天问ASR-PRO开发板与32单片机串口通信的代码)
- 基于STM32的计步器的设计
- 基于Qt与STM32平台开发的汽车车机系统上位机
- 基于MQTT的智能宠物投喂系统
- 312749069629470selfieU重绘.apk
- http%3A%2F%2Fimg.wsdl.vivo.com.cn%2Fappstore%2Fdeveloper%2Ficon%2F201412%2F201412231038336
- 主要记录B站up主莫烦matplotlib教程中的代码.zip
- Java SE Development Kit 8u411 Windows x64 Installer
- 用于科学绘图的 Matplotlib 样式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈