Files
hello-algo/ja/codes/c/utils/list_node.h
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

60 lines
1.3 KiB
C

/**
* File: list_node.h
* Created Time: 2023-01-09
* Author: Reanon ([email protected])
*/
#ifndef LIST_NODE_H
#define LIST_NODE_H
#ifdef __cplusplus
extern "C" {
#endif
/* 連結リストのノードの構造体 */
typedef struct ListNode {
int val; // ノードの値
struct ListNode *next; // 次のノードへの参照
} ListNode;
/* コンストラクタ:新しいノードを初期化する */
ListNode *newListNode(int val) {
ListNode *node;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = val;
node->next = NULL;
return node;
}
/* 配列をデシリアライズして連結リストにする */
ListNode *arrToLinkedList(const int *arr, size_t size) {
if (size <= 0) {
return NULL;
}
ListNode *dummy = newListNode(0);
ListNode *node = dummy;
for (int i = 0; i < size; i++) {
node->next = newListNode(arr[i]);
node = node->next;
}
return dummy->next;
}
/* 連結リストに確保したメモリ領域を解放する */
void freeMemoryLinkedList(ListNode *cur) {
// メモリを解放する
ListNode *pre;
while (cur != NULL) {
pre = cur;
cur = cur->next;
free(pre);
}
}
#ifdef __cplusplus
}
#endif
#endif // LIST_NODE_H