日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言循环拆分成和,C语言拆分循环链表程序

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言循环拆分成和,C语言拆分循环链表程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建一個循環鏈表,并將這個循環鏈表拆分成為兩個循環鏈表的示例程序,將以下代碼保存到一個源文件中:split_circular_linked_list.c, 如下所示 –

#include #include struct node { int data; struct node *next; }; struct node *even = NULL; struct node *odd = NULL; struct node *list = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); struct node *current; link->data = data; link->next = NULL; if (list == NULL) { list = link; list->next = link; return; } current = list; while (current->next != list) current = current->next; // Insert link at the end of the list current->next = link; link->next = list; } void display(struct node *head) { struct node *ptr = head; printf("[head] =>"); //start from the beginning while (ptr->next != head) { printf(" %d =>", ptr->data); ptr = ptr->next; } printf(" %d =>", ptr->data); printf(" [head]n"); } void split_list() { int count = 0; // Allocate memory for new node; struct node *list1; struct node *link; struct node *current; list1 = list; while (list1->next != list) { struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = list1->data; link->next = NULL; if (list1->data % 2 == 0) { if (even == NULL) { even = link; even->next = link; list1 = list1->next; continue; } else { current = even; while (current->next != even) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = even; } list1 = list1->next; } else { if (odd == NULL) { odd = link; odd->next = link; list1 = list1->next; continue; } else { current = odd; while (current->next != odd) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = odd; } list1 = list1->next; } } // Lets handle the last node link = (struct node*) malloc(sizeof(struct node)); link->data = list1->data; link->next = NULL; if (list1->data % 2 == 0) { current = even; while (current->next != even) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = even; } else { current = odd; while (current->next != odd) { current = current->next; } // Insert link at the end of the list current->next = link; link->next = odd; } } int main() { int i; for (i = 1; i <= 10; i++) insert(i); printf("Complete list: n"); display(list); split_list(); printf("nOdd : "); display(odd); printf("Even : "); display(even); return 0; }

執行上面程序,得到以下結果 –

Complete list: [head] => 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8 => 9 => 10 => [head] Odd : [head] => 1 => 3 => 5 => 7 => 9 => [head] Even : [head] => 2 => 4 => 6 => 8 => 10 => [head]

¥ 我要打賞 糾錯/補充 收藏

總結

以上是生活随笔為你收集整理的c语言循环拆分成和,C语言拆分循环链表程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。