当前位置:网站首页>Using C language to realize bidirectional linked list
Using C language to realize bidirectional linked list
2022-07-28 11:28:00 【Mid winter 29】
Use c Language implementation of two-way linked list
Studying recently C Language , In the study , Have a new understanding of a lot of knowledge .
I am from java Start learning programming , Now I've learned it again C in the future , It strengthened my recognition of my own learning route . C It's a programming language you have to learn , But it is by no means the first door .
There are several reasons why I firmly believe in this idea :
- c The language is too low , If you start with C Start learning the main language , Study 、 Understanding the cost is relatively large , In this way, the efficiency will not be so high .
- C The dominant domain of language is very different from other programming languages . such as java Good at web,c++ Good at client , and C Suitable system . The job threshold of system development is high , It requires a high level of language understanding and development ability . And I find it hard to reach .
- c The language is too low , Process oriented , Manual memory , Grammar is simple . The development process seems to be implemented bit by bit . Really adapted C Then turn to other languages , You will feel limited everywhere . This is how the discrimination chain between languages comes into being .
I'm glad I have learned twoorthree advanced languages , Go back to study C. Maybe I won't be engaged in direct C Development work , but C It must have a great impact on my programming thoughts .
I've heard many people say before , Write C It's from 0 Start , In addition to simple function libraries , There's nothing left . The data structure needs to be implemented by itself . I'm learning java when , I have read most data structure Source code , And put it into practice . Two years later , use c Once again , Should have different feelings .
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <stddef.h>
typedef int ll_data_t;
struct list;
// constructs a new (empty) list
struct list *list_create(void);
// counts the items on a list
size_t list_count(const struct list *list);
// inserts item at back of a list
void list_push(struct list *list, ll_data_t item_data);
// removes item from back of a list
ll_data_t list_pop(struct list *list);
// inserts item at front of a list
void list_unshift(struct list *list, ll_data_t item_data);
// removes item from front of a list
ll_data_t list_shift(struct list *list);
// deletes a node that holds the matching data
void list_delete(struct list *list, ll_data_t data);
// destroys an entire list
// list will be a dangling pointer after calling this method on it
void list_destroy(struct list *list);
#endif
#include "linked_list.h"
#include <stdlib.h>
struct list_node
{
struct list_node *prev, *next;
ll_data_t data;
};
struct list
{
struct list_node *first, *last;
};
// constructs a new (empty) list
struct list *list_create(void)
{
struct list *res = malloc(sizeof(struct list));
res->first = NULL;
res->last = NULL;
return res;
}
// counts the items on a list
size_t list_count(const struct list *list)
{
if (!list)
return 0;
struct list_node *list_node = list->first;
size_t res = 0;
while (list_node)
{
res++;
list_node = list_node->next;
}
return res;
}
// inserts item at back of a list
void list_push(struct list *list, ll_data_t item_data)
{
if (!list)
{
return;
}
struct list_node *node = malloc(sizeof(struct list_node));
node->next = NULL;
node->data = item_data;
node->prev = list->last;
if (!(list->first))
{
list->first = node;
}
else
{
list->last->next = node;
}
list->last = node;
}
// removes item from back of a list
ll_data_t list_pop(struct list *list)
{
struct list_node *prev = list->last->prev;
ll_data_t res = list->last->data;
if (!prev)
{
list->first = NULL;
}
else
{
prev->next = NULL;
}
free(list->last);
list->last = prev;
return res;
}
// inserts item at front of a list
void list_unshift(struct list *list, ll_data_t item_data)
{
if(!list)
{
return;}
struct list_node *new_node = malloc(sizeof(struct list_node));
new_node->data = item_data;
new_node->prev = NULL;
new_node->next = (list->first) ? list->first : NULL;
list->first = new_node;
}
// removes item from front of a list
ll_data_t list_shift(struct list *list)
{
if(!list || !list->first)
{
return 0;}
struct list_node *first = list->first;
ll_data_t val = first->data;
if(!list->first->next) {
// node was the only item in the list
list->first = NULL;
list->last= NULL;
} else {
list->first = first->next;
list->first->prev = NULL;
}
free(first);
return val;
}
// deletes a node that holds the matching data
void list_delete(struct list *list, ll_data_t data)
{
if (!list)
return;
struct list_node *next = list->first;
while (next)
{
if (next->data == data)
{
if (!next->prev)
{
list->first = next->next;
}
else
{
next->prev->next = next->next;
}
if (!next->next)
{
list->last = next->prev;
}
else
{
next->next->prev = next->prev;
}
free(next);
break;
}
next = next->next;
}
}
// destroys an entire list
// list will be a dangling pointer after calling this method on it
void list_destroy(struct list *list)
{
if (!list)
{
return;
}
struct list_node *node = list->first;
while (node)
{
struct list_node *tmp = node->next;
free(node);
node = tmp;
}
free(list);
}
边栏推荐
- 什么是WordPress
- RHEL 6.4 安装svn和apache
- C language to convert float data into BCD data
- 【cesium】entity属性和时许绑定:SampledProperty方法简单使用
- [MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index
- WPF dependent attribute (WPF dependent attribute)
- Two point, three point, 01 point plan [bullet I]
- EC20/EC25 4G模块AT指令开发总结
- 精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”
- mysql的左连接和右连接(内连接和自然连接的区别)
猜你喜欢
微软安全团队发现一家利用Windows零日漏洞销售间谍软件的奥地利公司

什么是WordPress

keil和IAR中lib库文件的生成和使用

万字详解 Google Play 上架应用标准包格式 AAB

Refresh your understanding of redis cluster

开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开

【MySQL从入门到精通】【高级篇】(九)InnoDB的B+树索引的注意事项

好用到爆!IDEA 版 Postman 面世了,功能真心强大
![Two point, three point, 01 point plan [bullet I]](/img/12/5cc55b5f4f0bbcd5b89a9601eed824.png)
Two point, three point, 01 point plan [bullet I]

Do data analysis, do you still not understand RFM analysis method (model)?
随机推荐
c语言实现float型数据转成BCD数据
[half understood] zero value copy
Learning notes tree array
【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
「学习笔记」树状数组
Why is low code (apaas) popular again recently?
Usage of memory operation functions memcpy() and memmove()
Iterative method for determinant (linear algebraic formula)
内存操作函数memcpy()和memmove()的用法
WinForm generates random verification code
Office2013 input mathematical formula above
RHEL 6.4 安装svn和apache
outlook突然变得很慢很卡怎么解决
echo -ne(echo line)
【cesium】entity属性和时许绑定:SampledProperty方法简单使用
GIS数据漫谈(五)— 地理坐标系统
leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
What is WordPress
【一知半解】零值拷贝