首页 > 其他分享 >C语言学习:案例:单链表的基本实现

C语言学习:案例:单链表的基本实现

时间:2023-02-11 20:33:05浏览次数:37  
标签:node head 单链 ListNode value next current 案例 C语言

 

  1 #include <io_utils.h>
  2 #include <stdlib.h>
  3 
  4 typedef struct ListNode {
  5   int value;
  6   struct ListNode *next;
  7 } ListNode;
  8 
  9 ListNode *CreateNode(int value) {
 10   ListNode *node = malloc(sizeof(ListNode));
 11   if (!node) exit(1);
 12 
 13   node->value = value;
 14   node->next = NULL;
 15   return node;
 16 }
 17 
 18 void DestroyNode(ListNode **node_ptr) {
 19   (*node_ptr)->next = NULL;
 20   free(*node_ptr);
 21   *node_ptr = NULL;
 22 }
 23 
 24 ListNode *CreateList(int array[], int length) {
 25   if (length <= 0) return NULL;
 26 
 27   ListNode *head = CreateNode(array[0]);
 28   ListNode *current = head;
 29   for (int i = 1; i < length; ++i) {
 30     current->next = CreateNode(array[i]);
 31     current = current->next;
 32   }
 33 
 34   return head;
 35 }
 36 
 37 void DestroyList(ListNode **head) {
 38   if (!head || !(*head)) return;
 39 
 40   ListNode *current = *head;
 41   while (current) {
 42     ListNode *to_be_destroy = current;
 43     current = current->next;
 44     DestroyNode(&to_be_destroy);
 45   }
 46 
 47   *head = NULL;
 48 }
 49 
 50 void InsertNode(ListNode **head, int value, int index) {
 51   if (!head || index < 0) return;
 52   ListNode *new_node = CreateNode(value);
 53   if (index == 0) {
 54     new_node->next = *head;
 55     *head = new_node;
 56   } else {
 57     if (!(*head)) {
 58       *head = CreateNode(0);
 59     }
 60     ListNode *current = *head;
 61     while (index > 1) {
 62       if (!current->next) {
 63         current->next = CreateNode(0);
 64       }
 65       current = current->next;
 66       index--;
 67     }
 68     new_node->next = current->next;
 69     current->next = new_node;
 70   }
 71 }
 72 
 73 ListNode *FindNode(ListNode *head, int value) {
 74   while (head && head->value != value) {
 75     head = head->next;
 76   }
 77   return head;
 78 }
 79 
 80 void DeleteNode(ListNode **head, int value) {
 81   printf("Delete: %d\n", value);
 82   if (!head || !(*head)) return;
 83   if ((*head)->value == value) {
 84     ListNode *new_head = (*head)->next;
 85     DestroyNode(head);
 86     *head = new_head;
 87     return;
 88   }
 89   ListNode *current_node = *head;
 90   while (current_node->next && current_node->next->value != value) {
 91     current_node = current_node->next;
 92   }
 93   if (current_node->next) {
 94     ListNode *node_to_delete = current_node->next;
 95     current_node->next = current_node->next->next;
 96     DestroyNode(&node_to_delete);
 97   }
 98 }
 99 
100 void PrintList(ListNode *head) {
101   while (head) {
102     printf("%d, ", head->value);
103     head = head->next;
104   }
105   printf("\n");
106 }
107 
108 int main() {
109   int array[] = {0, 1, 2, 3, 4};
110   ListNode *head = CreateList(array, 5);
111   PrintList(head);
112   InsertNode(&head, 100, 3);
113   InsertNode(&head, 200, 0);
114   InsertNode(&head, 300, 9);
115   InsertNode(&head, 400, 10);
116   PrintList(head);
117   DestroyList(&head);
118   PRINT_HEX(head);
119 
120   InsertNode(&head, 10, 0);
121   PrintList(head);
122   DestroyList(&head);
123   InsertNode(&head, 10, 10);
124   PrintList(head);
125   DestroyList(&head);
126   return 0;
127 }

 

标签:node,head,单链,ListNode,value,next,current,案例,C语言
From: https://www.cnblogs.com/liumy/p/17112506.html

相关文章