当前位置:网站首页>Bidirectional linked list (we only need to pay attention to insert and delete functions)
Bidirectional linked list (we only need to pay attention to insert and delete functions)
2022-07-03 13:33: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;// It can replace complex structures
};
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);
// Additional extended functions
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;
// Draw your own picture
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");
}
}
Test code
#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)
{
}
}
边栏推荐
- Open PHP error prompt under Ubuntu 14.04
- Kivy tutorial how to automatically load kV files
- 显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- 用户和组命令练习
- File uploading and email sending
- 物联网毕设 --(STM32f407连接云平台检测数据)
- [redis] cache warm-up, cache avalanche and cache breakdown
- Libuv Library - Design Overview (Chinese version)
- 开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
猜你喜欢

MySQL constraints

The difference between stratifiedkfold (classification) and kfold (regression)

Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

18W word Flink SQL God Road manual, born in the sky

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team

掌握Cypress命令行选项,是真正掌握Cypress的基础

Libuv Library - Design Overview (Chinese version)

MySQL_ JDBC
随机推荐
Detailed explanation of multithreading
The difference between stratifiedkfold (classification) and kfold (regression)
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Red hat satellite 6: better management of servers and clouds
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
Ubuntu 14.04 下开启PHP错误提示
Spark practice 1: build spark operation environment in single node local mode
R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
MySQL_ JDBC
Task6: using transformer for emotion analysis
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
18W word Flink SQL God Road manual, born in the sky
regular expression