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 tập chia một danh sách liên kết vòng trong C:

Quảng cáo
#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]\n");
}

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: \n");
   display(list);
    
   split_list();
    
   printf("\nDanh 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ả:

Quảng cáo
Chia danh sách liên kết vòng trong C

Đã có app VietJack trên điện thoại, giải bài tập SGK, SBT Soạn văn, Văn mẫu, Thi online, Bài giảng....miễn phí. Tải ngay ứng dụng trên Android và iOS.

Theo dõi chúng tôi miễn phí trên mạng xã hội facebook và youtube:

Các bạn có thể mua thêm khóa học JAVA CORE ONLINE VÀ ỨNG DỤNG cực hay, giúp các bạn vượt qua các dự án trên trường và đi thực tập Java. Khóa học có giá chỉ 300K, nhằm ưu đãi, tạo điều kiện cho sinh viên cho thể mua khóa học.

Nội dung khóa học gồm 16 chuơng và 100 video cực hay, học trực tiếp tại https://www.udemy.com/tu-tin-di-lam-voi-kien-thuc-ve-java-core-toan-tap/ Bạn nào có nhu cầu mua, inbox trực tiếp a Tuyền, cựu sinh viên Bách Khoa K53, fb: https://www.facebook.com/tuyen.vietjack

Follow facebook cá nhân Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.vietjack để tiếp tục theo dõi các loạt bài mới nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... mới nhất của chúng tôi.

danh-sach-lien-ket-trong-c.jsp


Tài liệu giáo viên