基本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

(C/C++) Link List - C 語言版本相关推荐

  1. 使用 TOGAF 9.1 框架與 ArchiMate 3.0 建模語言

    使用 TOGAF 9.1 框架與 ArchiMate 3.0 建模語言 Warren2Lynch 2018-05-21 09:18:08  1029  收藏 分类专栏: TOGAF ArchiMate ...

  2. python中国官网-中蟒 (中文 Python) 編程語言網站 chinesepython

    1. 什么是中蟒 ? 中蟒可以算是 Python 編程語言的一個中文翻譯版. 不過除了用戶信息, 中蟒還翻譯了 Python 的保留字, 內建函數, 類別定義等等. 也就是說, 在一般情況下, 你可以 ...

  3. Visual Basic 2005 中的程式語言加強功能

    Visual Basic 2005 中的程式語言加強功能 作者:Stan Schultes Microsoft MVP 2004 年 10 月 摘要:本文介紹許多 Visual Basic 2005 ...

  4. C语言中单引号 39 97 39,C語言程序设计实验指导书.doc

    C語言程序设计实验指导书 C语言程序设计 实验指导书 ? ? ? ? ? ? ? 计算机学院计算机科学教研室 武汉科技大学 2005年 ? ? 实验一 Turbo C编译环境的使用 [实验目的] 1. ...

  5. 进制选择器c语言算法,c語言编程技巧第1章走进Delph.doc

    c語言编程技巧第1章走进Delph 第1章 走进Delphi 本章回答了谁是本书的读者.本书适用于中.高级的Delphi程序员,以及从Visual Basic或C++语言迁移到Delphi的专业程序员 ...

  6. python之父面试谷歌_Python之父Guido Rossum:打造Google第三大開發語言-经管之家官网!...

    Python之父Guido Rossum:打造Google第三大開發語言 酷勤網 23-Jan-10 IT人物 2009年4月1日凌晨,Guido van Rossum(吉多•範羅蘇姆)在Python ...

  7. F#:微軟的下一代重量級語言

    F#:微軟的下一代重量級語言   文 / 蔡學鏞 微軟從2002年開始研發F#,2005年推出第一個版本,而2008年的現在,F#已經接近成熟.2007年底,微軟宣布將F#從研究室的專案轉移到產品部門 ...

  8. notepad++ c语言编译,Notepad++編譯和運行C語言 (GCC)

    我們在學習C語言的時候,實際上只需要編譯器和編輯器就能開搞了.(初學者過早接觸IDE不利於理解程序構建的過程) 在看這篇文章的時候,假設你已經知道如何把GCC配置到環境變量,並且會在命令行/終端下使用 ...

  9. 正序 逆序写 java_C語言版和JAVA版 把一個字節正序(高位在前)轉為逆序(低位在前) 和 逆序轉為正序...

    一.C語言版 把一個字節正序(高位在前)轉為逆序(低位在前) 和 逆序轉為正序 // xhrrj.cpp : Defines the entry point for the console appli ...

  10. c语言中continue在case中,C語言switch case 語句中能否使用continue 關鍵字?

    在C語言的學習中,我學習到switch case語句,我發現不能使用continue關鍵字. 代碼如下: #include int main() { int a; printf("input ...

最新文章

  1. Java设计模式(七大原则和单例模式篇)
  2. 6 redhat 查看rtc时间_甜甜老师的DB Fun圈第2讲:GaussDB 100 OLTP 单机在RHEL7.6上的安装...
  3. Javascript作用域原理---预编译
  4. 多看系统下载_漫画迷手机必备,「漫画之家」全网漫画随意看
  5. 【实验】广域网点到点协议PPP PAP CHAP的双向验证、单项认证
  6. java cmd 返回结果_Java调用cmd命令行并返回执行结果
  7. java获取数据库MetaData
  8. ORB-SLAM3 代码解读
  9. Selenium模拟浏览器常见操作及问题
  10. TensorFlow/Python的一个范例代码及阅读说明
  11. kali社工密码字典生成
  12. 2022-2028全球与中国智能家居安防系统市场现状及未来发展趋势
  13. 等保三级核心-网络安全
  14. 妹子图APP(一)—— Retrofit+Glide+Gson加载网络图片
  15. 微信小程序入门之常用组件(04)
  16. mac 下使用ssh
  17. matalb读取txt文件以及将数据写入txt文件
  18. 腾讯微服务框架 Tars 的 Go 性能提升之路
  19. C语言青蛙过河游戏超详细教程【附源码】
  20. 一篇关于串口的经典文章

热门文章

  1. 我要发明计算机作文,我要发明机器人作文700字
  2. 1.4.2.PHP5.6 狐教程-环境(Mac下 PHP开发环境 配置及安装 php5.6.x nginx mysql)
  3. Redis 常见问题 与 常见错误
  4. Lucene 索引维护 之 删除 与 更新 文档
  5. 云接入时发现的一个问题,记录避免以后踩坑
  6. tomcat部署安全证书文件(阿里云SSL证书)
  7. Luogu4897 【模板】最小割树
  8. C# 创建 读取 更新 XML文件
  9. 关于IDE集成开发环境,Pycharm小技巧
  10. javascript焦点图自动播放