博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(C/C++) Link List - C 語言版本
阅读量:5172 次
发布时间:2019-06-13

本文共 4474 字,大约阅读时间需要 14 分钟。

基本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 template10  * @file        Linklist.h11  */12 13 #include 
14 #include
15 16 /**17 * @defgroup Link list node18 * @brief19 * @{20 */21 22 typedef struct node23 {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 size32 * @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 None41 */42 void PrintList(Node *node);43 44 /**45 * @brief Release link list space.46 * @param *node: Release link list form this node.47 * @retval None48 */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 node56 */57 Node *SearchNode(Node *node, int data);58 59 /**60 * @brief Insert node61 * @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 None65 */66 void InsertNode(Node *node, int item, int data);67 68 /**69 * @brief Before insert node at first node.70 * @param *node: first node71 * @param data: The data of Insert node.72 * @retval first node73 */74 Node *Push_front(Node *node, int data);75 76 /**77 * @brief Insert node at last78 * @param *node: form last node79 * @param data: The data of last node.80 * @retval None81 */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             else16             {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

你可能感兴趣的文章
深度学习之前馈神经网络(前向传播和误差反向传播)
查看>>
IEnumerable<T>和IQueryable<T>区别
查看>>
(转)MFC界面风格
查看>>
Centos7 tmux1.6 安装
查看>>
二叉树(三)
查看>>
linux加密文件系统 fsck 无法修复一例
查看>>
【linux配置】VMware安装Redhat6.5
查看>>
AI自主决策——有限状态机
查看>>
《http权威指南》阅读笔记(二)
查看>>
软件工程
查看>>
http协议
查看>>
js替换问题replace和replaceAll
查看>>
c++11 : range-based for loop
查看>>
中国农历2013,2014 (zz.IS2120@BG57IV3)
查看>>
用virtualenv建立独立虚拟环境 批量导入模块信息
查看>>
Sublime Text3 插件:convertToUTF8
查看>>
BZOJ4060 : [Cerc2012]Word equations
查看>>
hdu2089不要62(数位dp)
查看>>
JAVA输出最大值和最小值
查看>>
64位weblogic11g安装
查看>>