Cách chia một danh sách liên kết vòng trong C
Bài tập C: chia một danh sách liên kết vòng Bài tập C này giúp bạn làm quen dần với cách tạo danh sách liên kết vòng và cách chia một danh sách liên kết vòng trong C. Để giải bài tập này, mình sử dụng cấu trúc struct trong C. Chương trình C Dưới đây là chương trình C để giải bài ...
Bài tập C: chia một danh sách liên kết vòng
Bài tập C này giúp bạn làm quen dần với cách tạo danh sách liên kết vòng và cách chia một danh sách liên kết vòng trong C. Để giải bài tập này, mình sử dụng cấu trúc struct trong C.
Chương trình C
Dưới đây là chương trình C để giải bài tập chia một danh sách liên kết vòng trong C:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *even = NULL;
struct node *odd = NULL;
struct node *list = NULL;
//tao danh sach lien ket
void insert(int data) {
// cap phat bo nho cho node moi;
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;
// chen link vao phan cuoi cua list
current->next = link;
link->next = list;
}
void display(struct node *head) {
struct node *ptr = head;
printf("[head] =>");
//bat dau tu phan dau cua list
while(ptr->next != head) {
printf(" %d =>",ptr->data);
ptr = ptr->next;
}
printf(" %d =>",ptr->data);
printf(" [head]
");
}
void split_list() {
int count = 0;
// cap phat bo nho cho node moi;
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;
}
// chen link vao phan cuoi cua 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;
}
// chen link vao phan cuoi cua list
current->next = link;
link->next = odd;
}
list1 = list1->next;
}
}
// xu ly 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;
}
// chen link vao phan cuoi cua list
current->next = link;
link->next = even;
}else {
current = odd;
while(current->next!= odd) {
current = current->next;
}
// chen link vao phan cuoi cua list
current->next = link;
link->next = odd;
}
}
int main() {
int i;
for(i=1; i<=10; i++)
insert(i);
printf("Danh sach ban dau:
");
display(list);
split_list();
printf("
Danh sach le: ");
display(odd);
printf("Danh sach chan: ");
display(even);
return 0;
}
Biên dịch chương trình C trên sẽ cho kết quả:

Loạt bài Bài tập C có giải của chúng tôi một phần dựa trên Tutorialspoint.com
Follow fanpage của team hoặc facebook cá nhân Nguyễn Thanh Tuyền để tiếp tục theo dõi các loạt bài mới nhất về Ngữ pháp tiếng Anh, luyện thi TOEIC, Java,C,C++,Javascript,HTML,Python,Database,Mobile ... mới nhất của chúng tôi.
Bài học Bài tập C phổ biến tại vietjack.com: