1. 程式人生 > >(C/C++) Link List - C 語言版本

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

基本Link List 用C語言實現

先附上標頭檔

 1 /**
 2  * @author      Chen-Hao Lin
 3  * @email       [email protected]
 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 }