当前位置:网站首页>双向链表(我们只需要关注插入和删除函数)
双向链表(我们只需要关注插入和删除函数)
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)
{
}
}
边栏推荐
- Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
- Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
- Setting up remote links to MySQL on Linux
- 显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
- The principle of human voice transformer
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- stm32和电机开发(从mcu到架构设计)
- 106. 如何提高 SAP UI5 应用路由 url 的可读性
- Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
猜你喜欢
Flink SQL knows why (19): the transformation between table and datastream (with source code)
regular expression
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Annotation and reflection
Detailed explanation of multithreading
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
随机推荐
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
MapReduce implements matrix multiplication - implementation code
Server coding bug
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Some thoughts on business
8 Queen question
Mysql database basic operation - regular expression
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
Sword finger offer 14- ii Cut rope II
File uploading and email sending
Sword finger offer 15 Number of 1 in binary
Spark practice 1: build spark operation environment in single node local mode
Sword finger offer 17 Print from 1 to the maximum n digits
服务器硬盘冷迁移后网卡无法启动问题
Libuv库 - 设计概述(中文版)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
Red Hat Satellite 6:更好地管理服务器和云
Sword finger offer 14- I. cut rope
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
February 14, 2022, incluxdb survey - mind map