(C/C++) Link List - C 語言版本

本文详细介绍了使用C语言实现链表的基本方法,包括创建、打印、释放链表,以及节点的搜索、插入等操作。通过具体代码示例,读者可以学习到如何在C语言中高效地管理和操作链表数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本Link List 用C語言實現

先附上標頭檔

 1 /**
 2  * @author      Chen-Hao Lin
 3  * @email       westgate.skater@gmail.com
 4  * @website   https://www.cnblogs.com/ollie-lin
 5  * @link     https://www.cnblogs.com/ollie-lin/p/9927405.html
 6  * @version     v1.0
 7  * @ide         CodeBlocks 17.12
 8  * @license     GUN GCC
 9  * @brief       link list template
10  * @file        Linklist.h
11  */
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 
16 /**
17  * @defgroup    Link list node
18  * @brief
19  * @{
20  */
21 
22 typedef struct node
23 {
24     int data;
25     struct node * next;
26 }Node;
27 
28 /**
29  * @brief   Create link list.
30  * @param   arr: pointer to integer data array. link list data array to assign link list data.
31  * @param   size: describe data array size
32  * @retval  first link list node.
33  */
34 Node *CreateList(int *arr, int size);
35 
36 
37 /**
38  * @brief   Show all of link list.
39  * @param   *node: print link list form this node.
40  * @retval  None
41  */
42 void PrintList(Node *node);
43 
44 /**
45  * @brief   Release link list space.
46  * @param   *node: Release link list form this node.
47  * @retval  None
48  */
49 void FreeList(Node *node);
50 
51 /**
52  * @brief   Search the specific node.
53  * @param   *node: Search the specific node form this pointer.
54  * @param   data: Search the specific node information.
55  * @retval  find the specific node
56  */
57 Node *SearchNode(Node *node, int data);
58 
59 /**
60  * @brief   Insert node
61  * @param   *node: Insert node after the this param.
62  * @param   item: Search data of specific node to insert node.
63  * @param   data: The data of Insert node.
64  * @retval  None
65  */
66 void InsertNode(Node *node, int item, int data);
67 
68 /**
69  * @brief   Before insert node at first node.
70  * @param   *node: first node
71  * @param   data: The data of Insert node.
72  * @retval  first node
73  */
74 Node *Push_front(Node *node, int data);
75 
76 /**
77  * @brief   Insert node at last
78  * @param   *node: form last node
79  * @param   data: The data of last node.
80  * @retval  None
81  */
82 void Push_back(Node *node, int data);

各項功能實做 Create List

 1 Node * CreateList(int *arr, int size)
 2 {
 3     if(arr == 0 || size == 0)
 4         return NULL;
 5 
 6     Node *previous, *first;
 7     for(int i = 0; i < size; i++)
 8     {
 9         Node *current = (Node*)malloc(sizeof(Node));
10         current->data = arr[i];
11 
12         if(i == 0)
13             first = current;
14         else{
15             previous->next = current;
16         }
17         current->next = NULL;
18         previous = current;
19     }
20     return first;
21 }

PrintList

 1 void PrintList(Node *node)
 2 {
 3     if(node == 0){
 4         printf("List is empty.\n");
 5         return;
 6     }
 7 
 8     Node *current = node;
 9     while(current)
10     {
11         printf("%d  ", current->data);
12         current = current->next;
13     }
14     printf("\n");
15 }

Release List

 1 void FreeList(Node *node)
 2 {
 3     Node *current, *temp;
 4     current = node;
 5     while(current)
 6     {
 7         temp = current;
 8         current = current->next;
 9         free(temp);
10     }
11 }

Search node

 1 Node *SearchNode(Node *node, int data)
 2 {
 3     Node *temp = node;
 4     while(temp)
 5     {
 6         if(temp->data == data)
 7             return temp;
 8         else
 9             temp = temp->next;
10     }
11     return NULL;
12 }

Insert node

 1 void InsertNode(Node *node, int item, int data)
 2 {
 3     Node *current = node;
 4     Node *previous = (Node*)malloc(sizeof(Node));
 5     Node *newNode = (Node*)malloc(sizeof(Node));
 6     while(current)
 7     {
 8             if(current->data == item)
 9             {
10                 newNode->data = data;
11                 previous->next = newNode;
12                 newNode->next = current;
13                 break;
14             }
15             else
16             {
17                 previous = current;
18                 current = current->next;
19             }
20     }
21 }

push front/back node

 1 Node* Push_front(Node *node, int data)
 2 {
 3     Node *temp = (Node*)malloc(sizeof(Node));
 4     temp->data = data;
 5     temp->next = node;
 6     node->next = node->next;
 7     return temp;
 8 }
 9 
10 void Push_back(Node *node, int data)
11 {
12     Node *newNode = (Node*)malloc(sizeof(Node));
13     newNode->data = data;
14     while(node->next)
15     {
16         node = node->next;
17     }
18     node->next = newNode;
19     newNode->next = NULL;
20 }

 

转载于:https://www.cnblogs.com/ollie-lin/p/9927405.html

载入需要的程辑包:nloptr 试开URL’http://cran.us.r-project.org/src/contrib/nloptr_2.2.1.tar.gz' Content type 'application/x-gzip' length 2253853 bytes (2.1 MB) ================================================== downloaded 2.1 MB * installing *source* package ‘nloptr’ ... ** 成功将‘nloptr’程序包解包并MD5和检查 ** using staged installation checking whether the C++ compiler works... yes checking for C++ compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether the compiler supports GNU C++... yes checking whether x86_64-conda-linux-gnu-c++ -std=gnu++11 accepts -g... yes checking for x86_64-conda-linux-gnu-c++ -std=gnu++11 option to enable C++11 features... unsupported checking for x86_64-conda-linux-gnu-c++ -std=gnu++11 option to enable C++98 features... unsupported checking how to run the C++ preprocessor... x86_64-conda-linux-gnu-c++ -std=gnu++11 -E checking whether the compiler supports GNU C++... (cached) yes checking whether x86_64-conda-linux-gnu-c++ -std=gnu++11 accepts -g... (cached) yes checking for x86_64-conda-linux-gnu-c++ -std=gnu++11 option to enable C++11 features... (cached) unsupported checking for x86_64-conda-linux-gnu-c++ -std=gnu++11 option to enable C++98 features... (cached) unsupported checking for pkg-config... /usr/bin/pkg-config checking if pkg-config knows NLopt... yes checking for pkg-config checking NLopt version... >= 2.7.0 Copying headers found in /usr/include into inst/include configure: creating ./config.status config.status: creating src/Makevars ** libs x86_64-conda-linux-gnu-c++ -std=gnu++11 -I"/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/include" -DNDEBUG -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -I/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -Wl,-rpath-link,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -fpic -fvisibility-inlines-hidden -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -fdebug-prefix-map=/home/conda/feedstock_root/build_artifacts/r-base_1621283253293/work=/usr/local/src/conda/r-base-3.6.3 -fdebug-prefix-map=/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats=/usr/local/src/conda-prefix -c dummy.cpp -o dummy.o x86_64-conda-linux-gnu-cc -I"/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/include" -DNDEBUG -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -I/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -Wl,-rpath-link,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -fpic -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -fdebug-prefix-map=/home/conda/feedstock_root/build_artifacts/r-base_1621283253293/work=/usr/local/src/conda/r-base-3.6.3 -fdebug-prefix-map=/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats=/usr/local/src/conda-prefix -c init_nloptr.c -o init_nloptr.o x86_64-conda-linux-gnu-cc -I"/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/include" -DNDEBUG -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -I/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -Wl,-rpath-link,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -fpic -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/include -fdebug-prefix-map=/home/conda/feedstock_root/build_artifacts/r-base_1621283253293/work=/usr/local/src/conda/r-base-3.6.3 -fdebug-prefix-map=/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats=/usr/local/src/conda-prefix -c nloptr.c -o nloptr.o x86_64-conda-linux-gnu-c++ -std=gnu++11 -shared -L/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/lib -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -Wl,-rpath-link,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -L/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -Wl,-rpath-link,/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib -o nloptr.so dummy.o init_nloptr.o nloptr.o -llapack -lblas -lgfortran -lm -lgomp -lquadmath -lpthread -lnlopt -L/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/lib -lR /mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/bin/../lib/gcc/x86_64-conda-linux-gnu/9.5.0/../../../../x86_64-conda-linux-gnu/bin/ld: cannot find -lnlopt: 没有那个文件或目录 collect2: error: ld returned 1 exit status make: *** [/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/share/make/shlib.mk:6: nloptr.so] Error 1 ERROR: compilation failed for package ‘nloptr’ * removing ‘/mnt/940660EA0660CEB4/rmats_turbo_v4_1_2/conda_envs/rmats/lib/R/library/nloptr’ 下载的程序包在 ‘/tmp/RtmpZ3TSeH/downloaded_packages’里 更新'.Library'里的HTML程序包列表 Making 'packages.html' ... 做完了。 载入需要的程辑包:nloptr 错误: could not install nloptr 此外: Warning messages: 1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : 不存在叫‘nloptr’这个名字的程辑包 2: In install.packages(package$name, repos = repos) : 安装程序包‘nloptr’时退出狀態的值不是0 3: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : 不存在叫‘nloptr’这个名字的程辑包 停止执行
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值