• Tạo danh sách liên kết trong C

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//hien thi list
void printList() {

   struct node *ptr = head;

   printf("\n[head] =>");
   //bat dau tu phan dau cua list
   while(ptr != NULL) {        
      printf(" %d =>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [null]\n");
}

//chen link tai vi tri dau tien
void insert(int data) {
   //tao mot link
   struct node *link = (struct node*) malloc(sizeof(struct node));

   //link->key = key;
   link->data = data;

   //tro link do toi first node cu
   link->next = head;

   //tro first toi first node moi
   head = link;
}

int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   printList();
   return 0;
}

[/bg_collapse]

  • Chương trình C để hiển thị danh sách liên kết theo chiều đảo ngược

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//hien thi list
void reverse_print(struct node *list) {
   if(list == NULL) {
      printf("[null] => ");
      return;
   }
   reverse_print(list->next);
   printf(" %d =>",list->data);
   
}

//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));

   link->data = data;
   link->next = NULL;

   // neu head la trong, tao list moi
   if(head==NULL) {
      head = link;
      return;
   }


   current = head;
   
   // di chuyen toi phan cuoi list
   while(current->next!=NULL)
      current = current->next;
   
   // chen link vao phan cuoi cua list
   current->next = link; 
}

int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   reverse_print(head);
   return 0;
}

[/bg_collapse]

  • Tìm kích cỡ của danh sách liên kết trong C

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = 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));

   link->data = data;
   link->next = NULL;

   // neu head la trong, tao list moi
   if(head==NULL) {
      head = link;
      return;
   }

   current = head;
   
   // di chuyen toi phan cuoi list
   while(current->next!=NULL)
      current = current->next;
   
   // chen link vao phan cuoi cua list
   current->next = link; 
}

void size_of_list() {
   int size = 0;
   
   if(head==NULL) {
      printf("Kich co cua Danh sach lien ket: %d ", size);
      return;
   } 

   current = head;
   size = 1;
   while(current->next!=NULL) {
      current = current->next;
      size++;
   }
   printf("Kich co cua Danh sach lien ket: %d ", size);
}
int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   size_of_list();
   return 0;
}

[/bg_collapse]

  • Chương trình C để tìm kiếm một phần tử trong danh sách liên kết

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = 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));

   link->data = data;
   link->next = NULL;

   // neu head la trong, tao list moi
   if(head==NULL) {
      head = link;
      return;
   }

   current = head;
   
   // di chuyen toi phan cuoi list
   while(current->next!=NULL)
      current = current->next;
   
   // chen link vao phan cuoi cua list
   current->next = link; 
}

void find_data(int item) {
   int pos = 0;
   
   if(head==NULL) {
      printf("Danh sach lien ket chua duoc khoi tao");
      return;
   } 

   current = head;
   while(current->next!=NULL) {
      if(current->data == item) {
         printf("Tim thay %d tai vi tri %d\n", item, pos);
         return;
      }
      current = current->next;
      pos++;
   }
   printf("%d khong ton tai trong list", item);
}
int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   find_data(40);
   find_data(44);
   
   return 0;
}

[/bg_collapse]

  • Cập nhật một phần tử của danh sách liên kết trong C

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = 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));

   link->data = data;
   link->next = NULL;

   // neu head la trong, tao list moi
   if(head==NULL) {
      head = link;
      return;
   }

   current = head;
   
   // di chuyen toi phan cuoi list
   while(current->next!=NULL)
      current = current->next;
   
   // chen link vao phan cuoi cua list
   current->next = link; 
}

void display() {
   struct node *ptr = head;

   printf("\n[head] =>");
   //bat dau tu phan dau cua list
   while(ptr != NULL) {
      printf(" %d =>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [null]\n");
}

void update_data(int old, int new1) {
   int pos = 0;
   
   if(head==NULL) {
      printf("Danh sach lien ket chua duoc khoi tao");
      return;
   } 

   current = head;
   while(current->next!=NULL) {
      if(current->data == old) {
         current->data = new1;
         printf("\nTim thay %d tai vi tri %d, duoc thay the voi %d\n", old, pos, new1);
         return;
      }
      current = current->next;
      pos++;
   }
   printf("%d khong ton tai trong list\n", old);
}


int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   display();
   update_data(40, 44);
   display();
   
   return 0;
}

[/bg_collapse]

  • Chương trình C để xóa một phần tử từ danh sách liên kết

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#include <stdio.h>
#include <stdlib.h>

struct node {
   int data;
   struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;
struct node *prev = 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));

   link->data = data;
   link->next = NULL;

   // neu head la trong, tao list moi
   if(head==NULL) {
      head = link;
      return;
   }

   current = head;
   
   // di chuyen toi phan cuoi list
   while(current->next!=NULL)
      current = current->next;
   
   // chen link vao phan cuoi cua list
   current->next = link; 
}

void display() {
   struct node *ptr = head;

   printf("head] =>");
   //bat dau tu phan dau cua list
   while(ptr != NULL) {        
      printf(" %d =>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [null]\n");
}

void remove_data(int data) {
   int pos = 0;
   
   if(head==NULL) {
      printf("Danh sach lien ket chua duoc khoi tao");
      return;
   } 
    
   if(head->data == data) {
      if(head->next != NULL) {
         head = head->next;
         return;
      }else {
         head = NULL;
         printf("Bay gio List la trong");
         return;
      }
   }else if(head->data != data && head->next == NULL) {
      printf("Khong tim thay %d trong list\n", data);
      return;
   }
        
   
   
// prev = head;
   current = head;
   
   while(current->next != NULL && current->data != data) {
      prev = current;
      current = current->next;
   }        

   if(current->data == data) {
      prev->next = prev->next->next;
      free(current);
   }else
      printf("Khong tim thay %d trong list.", data);

}

int main() {
   insert(10);
   insert(20);
   insert(30);
   insert(1);
   insert(40);
   insert(56); 

   printf("Truoc khi xoa: ");
   display();
   remove_data(30);
   printf("Sau khi xoa: ");
   display();
   
   return 0;
}

[/bg_collapse]

  • Nối hai danh sách liên kết trong C

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#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(data%2 == 0) {
      if(even == NULL) {
         even = link;
         return;
      }else {
         current = even;

         while(current->next != NULL)
         current = current->next;

         // chen link vao phan cuoi cua list
         current->next = link; 
      }
   }else {
      if(odd == NULL) {
         odd = link;
         return;
      }else {
         current = odd;

         while(current->next!=NULL)
            current = current->next;

         // chen link vao phan cuoi cua list
         current->next = link; 
      }
   }
}

void display(struct node *head) {
   struct node *ptr = head;

   printf("[head] =>");
   
   while(ptr != NULL) {        
      printf(" %d =>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [null]\n");
}

void combine() {
   struct node *link;

   list = even;
   link = list;
    
   while(link->next!= NULL) {
      link = link->next;
   }
        
   link->next = odd;
}

int main() {
   int i;

   for(i=1; i<=10; i++)
      insert(i);

   printf("Danh sach chan: ");
   display(even);

   printf("Danh sach le: ");
   display(odd);

   combine();
   
   printf("Sau khi noi: \n");
   display(list);
   
   return 0;
}

[/bg_collapse]

  • Chương trình C để chia một danh sách liên kết thành hai

[bg_collapse view=”button-red” color=”#ffffff” icon=”arrow” expand_text=”Xem bài giải” collapse_text=”Ẩn bài giải” ]

/**
* Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
*
* @author cafedevn
* Contact: cafedevn@gmail.com
* Fanpage: https://www.facebook.com/cafedevn
* Instagram: https://instagram.com/cafedevn
* Twitter: https://twitter.com/CafedeVn
* Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
*/

#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;
      return;
   }
   current = list;
    
   while(current->next!=NULL)
      current = current->next;
    
   // chen link vao phan cuoi cua list
   current->next = link; 
}

void display(struct node *head) {
   struct node *ptr = head;

   printf("[head] =>");
   //bat dau tu phan dau cua list
   while(ptr != NULL) {        
      printf(" %d =>",ptr->data);
      ptr = ptr->next;
   }

   printf(" [null]\n");
}

void split_list() {
   // cap phat bo nho cho node moi;
   struct node *link;
   struct node *current;

   while(list != NULL) {
      struct node *link = (struct node*) malloc(sizeof(struct node));
         
      link->data = list->data;
      link->next = NULL;
         
      if(list->data%2 == 0) {
         if(even == NULL) {
            even = link;
            list = list->next;
            continue;
         }else {
            current = even;
                
            while(current->next != NULL)
            current = current->next;
                
            // chen link vao phan cuoi cua list
            current->next = link; 
         }
         list = list->next;
      }else {
         if(odd == NULL) {
            odd = link;
            list = list->next;
            continue;
         }else {
            current = odd;
            while(current->next!=NULL)
            current = current->next;
                
            // chen link vao phan cuoi cua list
            current->next = link; 

         }
         list = list->next;
      }
   }
}
    
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("\nDanh sach chan: ");
   display(even);

   return 0;
}

[/bg_collapse]

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!