当前位置:网站首页>双向链表(我们只需要关注插入和删除函数)
双向链表(我们只需要关注插入和删除函数)
2022-07-03 12:58:00 【fitpolo】

Dlink_list.h
#ifndef _DLINKLIST_H_
#define _DLINKLIST_H_
#include "stdio.h"
#include "stdlib.h"
typedef struct _tag_Dlink_list_node Dlink_list_node;
struct _tag_Dlink_list_node
{
Dlink_list_node *next;
Dlink_list_node *pre;
};
typedef struct _tag_Dlink_list TDlink_list;
struct _tag_Dlink_list
{
Dlink_list_node header;
int length;
};
typedef struct _tag_Dlink_list_value Dlink_list_value;
struct _tag_Dlink_list_value
{
Dlink_list_node node;
int value;//可以替换复杂的结构体
};
TDlink_list* DLinkList_Create(void);
int DLinkList_Insert(TDlink_list* list, Dlink_list_node* node, int pos);
Dlink_list_node* DLinkList_Get(TDlink_list* list, int pos);
Dlink_list_node* DLinkList_Delete(TDlink_list* list, int pos);
//额外扩展的函数
Dlink_list_node* DLinkList_DeleteNode(TDlink_list* list, Dlink_list_node* node);
void DLinkList_printf(TDlink_list* list);
#endif
Dlink_list.c
#include "Dlink_list.h"
TDlink_list* DLinkList_Create(void)
{
TDlink_list* ret = (TDlink_list*)malloc(sizeof(TDlink_list));
if( ret != NULL )
{
ret->length = 0;
ret->header.next = NULL;
ret->header.pre = NULL;
}
return ret;
}
int DLinkList_Insert(TDlink_list* list, Dlink_list_node* node, int pos)
{
int i;
Dlink_list_node* next = NULL;
Dlink_list_node* current = &(list->header);
if (current==NULL && list->length==0)
{
list->header.next = node;
list->header.pre = NULL;
node->next = NULL;
node->pre = NULL;
}else
{
for (i=0; i<pos&&(current->next!=NULL); i++)
{
current = current->next;
}
next = current->next;
//自己画图看看
current->next = node;
node->next = next;
if (next!=NULL)
{
next->pre = node;
}
node->pre = current;
}
list->length++;
return 0;
}
Dlink_list_node* DLinkList_Get(TDlink_list* list, int pos)
{
int i = 0;
Dlink_list_node *ret = NULL;
if (list->length>pos)
{
ret = list->header.next;
for (i=0; i<pos; i++)
{
ret = ret->next;
}
}
return ret;
}
Dlink_list_node* DLinkList_Delete(TDlink_list* list, int pos)
{
int i = 0;
Dlink_list_node *current = NULL;
Dlink_list_node *ret = NULL;
Dlink_list_node *next = NULL;
if (list->length && list->length>pos)
{
current = &(list->header);
for (i=0; i<pos; i++)
{
current = current->next;
}
ret = current->next;
next = ret->next;
current->next = next;
if (next!=NULL)
{
if (current == &(list->header) )
{
next->pre = NULL;
}else
{
next->pre = current;
}
}
list->length--;
}else
{
printf("DLinkList_Delete error %d-%d\n",list->length,pos);
}
return ret;
}
Dlink_list_node* DLinkList_DeleteNode(TDlink_list* list, Dlink_list_node* node)
{
int i;
Dlink_list_node *ret = NULL;
Dlink_list_node *current = &(list->header);
for (i=0; i<list->length; i++)
{
if (current->next == node)
{
ret = current->next;
break;
}
current = current->next;
}
if (ret != NULL)
{
DLinkList_Delete(list,i);
}
return ret;
}
void DLinkList_printf(TDlink_list* list)
{
int i;
Dlink_list_node *current = list->header.next;
Dlink_list_value *tmp;
printf("len:%d-",list->length);
if (current != NULL)
{
for (i=0; i<list->length; i++)
{
tmp = (Dlink_list_value*)current;
printf("[i:%d-v:%d]-",i,tmp->value);
current = current->next;
}
printf("\n");
}
}
测试代码
#include "stdio.h"
#include "stdint.h"
#include "Dlink_list.h"
int main(void)
{
#define BUF_SIZE 10
int i;
TDlink_list *head;
Dlink_list_value buf[BUF_SIZE];
printf("hello \r\n");
for (i=0; i<BUF_SIZE; i++)
{
buf[i].value = i+1;
}
head = DLinkList_Create();
for (i=0; i<BUF_SIZE; i++)
{
DLinkList_Insert(head,(Dlink_list_node*)&buf[i],0);
}
DLinkList_printf(head);
// for (i=0; i<BUF_SIZE; i++)
// {
// DLinkList_Delete(head,0);
// DLinkList_printf(head);
// }
for (i=BUF_SIZE-1; i>=0; i--)
{
DLinkList_Delete(head,i);
DLinkList_printf(head);
}
while(1)
{
}
}
边栏推荐
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
- SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
- 正则表达式
- Anan's doubts
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- Swiftui development experience: the five most powerful principles that a programmer needs to master
- My creation anniversary: the fifth anniversary
- File uploading and email sending
- Reptile
猜你喜欢

2022-02-14 incluxdb cluster write data writetoshard parsing

February 14, 2022, incluxdb survey - mind map

Detailed explanation of multithreading

MySQL installation, uninstallation, initial password setting and general commands of Linux

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!

2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator

rxjs Observable filter Operator 的实现原理介绍

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

Seven habits of highly effective people
随机推荐
Can newly graduated European college students get an offer from a major Internet company in the United States?
Logback log framework
Reptile
Ubuntu 14.04 下开启PHP错误提示
Flink SQL knows why (12): is it difficult to join streams? (top)
正则表达式
Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
TensorBoard可视化处理案例简析
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
CVPR 2022 image restoration paper
Multi table query of MySQL - multi table relationship and related exercises
MySQL_ JDBC
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
用户和组命令练习
MySQL constraints
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Logback 日志框架
SSH login server sends a reminder