1. 程式人生 > >circular linked list&CLL(循環鏈表)

circular linked list&CLL(循環鏈表)

std rst fir 循環 新的 slow 下一個 ntc while

循環鏈表,和苯一樣,一條蛇咬住了自己的尾巴。在 操作系統 給 進程 分配運行 資源 時,有體現。

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 
  4 struct node
  5 {
  6     int data;
  7     struct node * next;
  8 };
  9 //插入動作,在頭節點之前插入
 10 void insert(struct node ** head_ref, int data)
 11 {
 12     struct node * new_node = (struct
node *) malloc (sizeof(struct node)); 13 struct node * temp = (*head_ref); 14 new_node->data = data; 15 // 頭尾相連 16 new_node->next = (*head_ref); 17 //判斷鏈表是否為空,如果不為空,則要循環到最後一個節點,讓最後一個節點的next指向新的頭結點 18 if(NULL != (*head_ref)) 19 { 20 while(temp->next != (*head_ref))
21 { 22 temp = temp->next; 23 } 24 temp->next = new_node; 25 } 26 else 27 { 28 //若鏈表為空鏈表,則將欲插入的節點的next指向他自己 29 new_node->next = new_node; 30 } 31 //將其指定為頭結點 32 (*head_ref) = new_node; 33 } 34 //判斷該鏈表是否為一個循環鏈表
35 int isCircular(struct node * head_ref) 36 { 37 if(NULL == head_ref) 38 { 39 return 1; 40 } 41 42 struct node * temp = head_ref->next; 43 44 while(temp != NULL && temp != head_ref) 45 { 46 temp = temp->next; 47 } 48 49 return (temp == head_ref) ? 1 : -1; 50 } 51 //打印循環鏈表,可以從任何一個位置打印,而單鏈表只能從頭結點開始打印(不考慮位置關系的話) 52 void printCircularLinkedList(struct node * head_ref) 53 { 54 struct node * temp = head_ref; 55 56 if(NULL != head_ref) 57 { 58 do 59 { 60 printf("%d ", temp->data); 61 temp = temp->next; 62 } 63 while(temp != head_ref); 64 printf("\n"); 65 } 66 } 67 //手撕鬼子似的將循環鏈表撕裂,WOW,這裏用到了單鏈表裏返回鏈表中間元素的 方法之一 68 void splitList(struct node * head, struct node ** head1_ref, struct node ** head2_ref) 69 { 70 struct node * fast_pointer = head; 71 struct node * slow_pointer = head; 72 73 if(-1 == headIsNULL(head)) 74 { 75 printf("ERROR!"); 76 return ; 77 } 78 79 while(fast_pointer->next->next != head && fast_pointer->next != head) 80 { 81 fast_pointer = fast_pointer->next->next; 82 slow_pointer = slow_pointer->next; 83 } 84 //如果不是當前元素不是尾節點的話,而是倒數第二個節點 85 if(head == fast_pointer->next->next) 86 { 87 fast_pointer = fast_pointer->next; 88 } 89 //判斷該鏈表是否只有一個節點 90 if(head->next != head) 91 { 92 (*head2_ref) = slow_pointer->next; 93 fast_pointer->next = slow_pointer->next; 94 } 95 (*head1_ref) = head; 96 slow_pointer->next = (*head1_ref); 97 } 98 //排序式插入節點 99 void sortInsert(struct node ** head_ref, struct node * new_node) 100 { 101 struct node * current = (*head_ref); 102 103 if(NULL == (*head_ref)) 104 { 105 new_node->next = new_node; 106 (*head_ref) = new_node; 107 } 108 //如果插入節點的data小於當前頭節點的data 109 else if(current->data >= new_node->data) 110 { 111 while(current->next != (*head_ref)) 112 current = current->next; 113 114 current->next = new_node; 115 new_node->next = (*head_ref); 116 (*head_ref) = new_node; 117 } 118 else 119 { 120 //若插入節點data大於當前節點data,循環鏈表到一個大於當前插入節點data的節點 121 while(current->next != (*head_ref) && current->next->data < new_node->data) 122 { 123 current = current->next; 124 } 125 //新節點的next = 當前節點的下一個節點 126 new_node->next = current->next; 127 current->next = new_node; 128 } 129 } 130 //判斷鏈表是否為空 131 int headIsNULL(struct node * head_ref) 132 { 133 return (NULL == head_ref)? -1 : 1; 134 } 135 int main(void) 136 { 137 struct node * head = NULL; 138 139 insert(&head, 0); 140 insert(&head, 1); 141 insert(&head, 3); 142 insert(&head, 4); 143 insert(&head, 5); 144 printCircularLinkedList(head); 145 struct node * firstList = NULL; 146 struct node * lastList = NULL; 147 splitList(head, &firstList, &lastList); 148 printCircularLinkedList(firstList); 149 printCircularLinkedList(lastList); 150 /*------------------------------------*/ 151 int array[] = {2, 5, 1, 0, 9, 10, 3}; 152 int arraySize = sizeof(array)/sizeof(int); 153 struct node * head_1 = NULL; 154 struct node * temp = NULL; 155 156 for(int i = 0; i < arraySize; i++) 157 { 158 temp = (struct node *) malloc (sizeof (struct node)); 159 temp->data = array[i]; 160 sortInsert(&head_1, temp); 161 } 162 printCircularLinkedList(head_1); 163 if(0 < isCircular(head)) 164 { 165 printf("\nThe linked list is circularLinked list!"); 166 } 167 else 168 { 169 printf("\nThe linked list isn`t circularLinked list!"); 170 } 171 return 0; 172 }

受益頗多

circular linked list&CLL(循環鏈表)