当前位置:网站首页>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)
{
}
}
边栏推荐
- Ubuntu 14.04 下开启PHP错误提示
- KEIL5出现中文字体乱码的解决方法
- Several common optimization methods matlab principle and depth analysis
- rxjs Observable filter Operator 的实现原理介绍
- 顺序表(C语言实现)
- Will Huawei be the next one to fall
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- 106. 如何提高 SAP UI5 应用路由 url 的可读性
- 栈应用(平衡符)
猜你喜欢

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?

DQL basic query

SQL Injection (GET/Select)

User and group command exercises

When we are doing flow batch integration, what are we doing?

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

MySQL functions and related cases and exercises

Elk note 24 -- replace logstash consumption log with gohangout

Servlet
随机推荐
Ubuntu 14.04 下开启PHP错误提示
Setting up remote links to MySQL on Linux
已解决(机器学习中查看数据信息报错)AttributeError: target_names
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Error running 'application' in idea running: the solution of command line is too long
物联网毕设 --(STM32f407连接云平台检测数据)
JSP and filter
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
logback日志的整理
Road construction issues
Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
pytorch 载入历史模型时更换gpu卡号,map_location设置
MapReduce implements matrix multiplication - implementation code
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
静态链表(数组的下标代替指针)
Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
MapReduce实现矩阵乘法–实现代码