当前位置:网站首页>[learning notes] linked list operation
[learning notes] linked list operation
2022-07-28 06:56:00 【Step on a meteor】
#include <stdio.h>
#include <stdlib.h>
struct list_node{
int data;
struct list_node *next;
};
// Create a linked list
int list_init(struct list_node *link_list)
{
link_list = (struct list_node*)malloc(sizeof(struct list_node));
link_list->data = 0;
link_list->next = NULL;
return 0;
}
// The linked list adds nodes after a given node
struct list_node *list_insert(struct list_node *link_list, int data, int n)
{
int i;
//printf("data = %d\n", data);
//printf("n = %d\n", n);
if (link_list == NULL) {
return NULL;
}
// Add header node
if (n == 0) {
struct list_node *link_head = (struct list_node *)malloc(sizeof(struct list_node));
link_head->data = data;
link_head->next = link_list;
//link_list = link_head;
return link_head;
}
// Add tail node
struct list_node *link_tmp = link_list;
int count = 1;
for (i = 0; i < n; i++) {
link_tmp = link_tmp->next;
}
// Add tail node
struct list_node *link_tail_tmp = link_tmp->next;
if (link_tail_tmp->next == NULL) {
struct list_node *link_tail = (struct list_node *)malloc(sizeof(struct list_node));
link_tail->data = data;
link_tail->next = NULL;
link_tail_tmp->next = link_tail;
return link_list;
}
// Add intermediate nodes
struct list_node *link_middle = (struct list_node *)malloc(sizeof(struct list_node));
link_middle->data = data;
link_middle->next = link_tail_tmp;
link_tmp->next = link_middle;
return link_list;
}
// Delete the specified node
int link_delete(struct list_node *link_list, int n)
{
int i;
int count = 0;
// Delete header node
if (n == 0) {
link_list = link_list->next;
return 0;
}
struct list_node *link_tmp = link_list;
for (i = 0; i < n - 1; i++) {
link_tmp = link_tmp->next;
}
// Delete tail node
struct list_node *link_tail = link_tmp->next;
if (link_tail->next == NULL) {
link_tmp->next = NULL;
return 0;
}
// Delete intermediate nodes
struct list_node *link_middle = link_tail->next;
link_tmp->next = link_middle;
return 0;
}
int link_show(struct list_node *link_list)
{
while (link_list->next != NULL) {
printf("%d\t", link_list->data);
link_list = link_list->next;
}
printf("%d\n", link_list->data);
return 0;
}
边栏推荐
- PKU-2739-Sum of Consecutive Prime Numbers(筛素数法打表)
- 搭建PHP7私有仓库
- Technology sharing | how to simulate real use scenarios? Mock technology to help you
- Tcp/ip five layer model
- 网络——传输层(详细版)
- Common models in software development
- 修复故障扇区
- Technology sharing | interface testing value and system
- 测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
- Ten thousand words summarize and realize the commonly used sorting and performance comparison
猜你喜欢

SSH service configuration

JS reverse question 100 - question 1

Technology sharing | do you know the functions of the server interface automated testing and requests library?

NFS shared storage service

Principle and configuration of NAT and pat

Which brand of air conduction earphones is good and highly praised

如何描述一个BUG以及BUG级别的定义、生命周期

How to simulate the implementation of strcpy library functions

Archery database audit platform deployment

Technology sharing | send requests using postman
随机推荐
技术分享 | 使用 cURL 发送请求
OSI七层模型
TCP/IP五层模型
测试面试题集锦(二)| 测试工具篇(附答案)
HDU-1159-CommonSubsequence(LCS最长公共子序列)
进程和线程的区别
Pku-2739-sum of constructive prime numbers
搭建PHP7私有仓库
Pku-2524-ubiquitous relations (parallel search template)
[C language] dynamic memory management
HDU-5806-NanoApeLovesSequenceⅡ(尺取法)
MySQL主从
如何描述一个BUG以及BUG级别的定义、生命周期
2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation
Cocos2d-x learning notes Tile Map tiledmap
Iptables firewall
Installation and configuration of unit test framework jest with typescript
Method of designing test cases
LNMP搭建过程详解
What is a linear table?