当前位置:网站首页>使用c语言实现双向链表
使用c语言实现双向链表
2022-07-28 10:51:00 【中冬廿九】
使用c语言实现双向链表
最近在学习C语言,在学习中,对很多知识又有了新的认识。
我是从java开始学习编程的,现在学了一遍C以后,更坚定了我对自身学习路线的认可。 C是你必须要学的一门编程语言,但绝不是第一门。
坚定我这个想法的原因有这么几个:
- c语言过于底层,如果上手就以C为主语言开始学习,学习、理解成本相对大,这样效率就不会那么高。
- C语言的统治领域与别的编程语言有非常大的区别。比如java擅长web,c++擅长客户端,而C适合系统。系统开发的工作岗位门槛较高,对语言底层理解和开发能力要求较高。而我自觉难达。
- c语言过于底层,面向过程,手动内存,语法简单。开发过程像是一点一点的去扣去实现。真正适应了C 再去转向别的语言,会觉得处处受限。语言间的歧视链就是这样产生的。
我很庆幸我在已经学习了两三门高级语言后,再回过头学习C。或许我以后不会从事直接C开发的工作,但C对我编程的思想一定定有很大影响。
之前也听很多人说,写C就是从0做起,除了简单的函数库,什么都没了。数据结构需要全部自己实现。在学习java时,我曾阅读过大多数 数据结构 的源码,并付诸实现。两年之后,用c再来一遍,应该有不同的感受。
#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);
}
边栏推荐
- [MySQL] MySQL error "error 2006 (HY000): MySQL server has gone away"
- 小水滴2.0网站导航网模板
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
- What's the secret of creating a popular short video?
- 【Gradle】This version of the JMH Gradle plugin requires Gradle 6+, you are using 6.6.
- Two point, three point, 01 point plan [bullet III]
- [FPGA tutorial case 41] image case 1 - reading pictures through Verilog
- Why should coding and modulation be carried out before transmission
- STM32驱动ST7701S芯片(vⅰV0手机换屏价)
- 苹果手机iCloud钥匙串的加密缺陷
猜你喜欢

What is WordPress

ZBrush 2022软件安装包下载及安装教程

Here is a super practical excel shortcut set (common + summary of eight categories)

分体式测斜探头安装要点及注意事项

Installation points and precautions of split angle probe

Crm+ zero code: easily realize enterprise informatization

Office2013 input mathematical formula above

什么是WordPress

What is WordPress

Do data analysis, do you still not understand RFM analysis method (model)?
随机推荐
Inventory: 144 free learning websites, the most complete collection of resources in the whole network
What is WordPress
mysql的左连接和右连接(内连接和自然连接的区别)
本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
Office2013 input mathematical formula above
苹果手机iCloud钥匙串的加密缺陷
Learn these analysis methods and models, and no longer have no ideas when encountering problems
字节一面:如何用 UDP 实现可靠传输?
精品方案|海泰方圆全栈式数据安全治理方案 为数据设一把“安全锁”
Nodejs: mongodb simple fuzzy + paging query instance
[FPGA tutorial case 41] image case 1 - reading pictures through Verilog
Question of hanging the interviewer
The use of C language linked list
Two dimensional prefix and
Understanding of the return value of the structure pointer function passed to the structure pointer
使用 Terraform 在 AWS 上快速部署 MQTT 集群
Encryption defect of icloud Keychain in Apple mobile phone
I use the applet container to improve the efficiency of mobile R & D by 5 times!
postgres概述
好用到爆!IDEA 版 Postman 面世了,功能真心强大