Lớp 2 - kết nối tri thức
Lớp 2 - Chân trời sáng tạo
Lớp 2 - Cánh diều
Tài liệu tham khảo
Lớp 3Sách giáo khoa
Tài liệu tham khảo
Sách VNEN
Lớp 4Sách giáo khoa
Sách/Vở bài bác tập
Đề thi
Lớp 5Sách giáo khoa
Sách/Vở bài xích tập
Đề thi
Lớp 6Lớp 6 - liên kết tri thức
Lớp 6 - Chân trời sáng sủa tạo
Lớp 6 - Cánh diều
Sách/Vở bài xích tập
Đề thi
Chuyên đề và Trắc nghiệm
Lớp 7Sách giáo khoa
Sách/Vở bài bác tập
Đề thi
Chuyên đề & Trắc nghiệm
Lớp 8Sách giáo khoa
Sách/Vở bài bác tập
Đề thi
Chuyên đề & Trắc nghiệm
Lớp 9Sách giáo khoa
Sách/Vở bài tập
Đề thi
Chuyên đề và Trắc nghiệm
Lớp 10Sách giáo khoa
Sách/Vở bài xích tập
Đề thi
Chuyên đề & Trắc nghiệm
Lớp 11Sách giáo khoa
Sách/Vở bài tập
Đề thi
Chuyên đề và Trắc nghiệm
Lớp 12Sách giáo khoa
Sách/Vở bài xích tập
Đề thi
Chuyên đề và Trắc nghiệm
ITNgữ pháp tiếng Anh
Lập trình Java
Phát triển web
Lập trình C, C++, Python
Cơ sở dữ liệu
Bạn đang xem: Danh sách liên kết đơn

Cấu trúc tài liệu và giải thuậtMột số có mang về Giải thuật cấu tạo dữ liệu mảng (Array)Danh sách links - Linked ListsNgăn xếp & Hàng đợiMột số giải thuật tìm kiếmMột số lời giải sắp xếpCấu trúc tài liệu đồ thị (Graph)Cấu trúc dữ liệu câyĐệ qui (Recursion)Tài liệu tham khảo
Danh sách link (Linked List) vào C
Trang trước
Trang sau
Một Danh sách links (Linked List) là một trong dãy các cấu trúc dữ liệu được liên kết với nhau thông qua các liên kết (link). Phát âm một cách dễ dàng và đơn giản thì Danh sách link là một cấu tạo dữ liệu gồm 1 nhóm các nút (node) tạo thành một chuỗi. Mỗi nút gồm tài liệu ở nút đó cùng tham chiếu mang đến nút sau đó trong chuỗi.
Chương trình minh họa Danh sách link (Linked List) trong C
#include #include #include #include struct node int data; int key; struct node *next;;struct node *head = NULL;struct node *current = NULL;//hien thi danh sachvoid printList() struct node *ptr = head; printf(" < "); //bat dau tu phan dau danh sach while(ptr != NULL) printf("(%d,%d) ",ptr->key,ptr->data); ptr = ptr->next; printf(" >");//chen links tai vi tri dau tienvoid insertFirst(int key, int data) //tao mot link struct node *link = (struct node*) malloc(sizeof(struct node)); link->key = key; link->data = data; //tro links nay toi first node cu link->next = head; //tro first toi first node moi head = link;//xoa phan tu dau tienstruct node* deleteFirst() //luu tham chieu toi first links struct node *tempLink = head; //danh dau next toi first link la first head = head->next; //tra ve link bi xoa return tempLink;//kiem tra menu co trong giỏi khongbool isEmpty() return head == NULL;int length() int length = 0; struct node *current; for(current = head; current != NULL; current = current->next) length++; return length;//tim mot liên kết voi key domain authority chostruct node* find(int key) //bat dau tim tu first link struct node* current = head; //neu list la vào if(head == NULL) return NULL; //duyet qua danh sách while(current->key != key) //neu day la last node if(current->next == NULL) return NULL; else //di chuyen toi next link current = current->next; //neu tim vậy du lieu, tra ve links hien tai return current;//xoa mot link voi key da chostruct node* deleteKey(int key) //bat dau tu first links struct node* current = head; struct node* previous = NULL; //neu danh sách la vào if(head == NULL) return NULL; //duyet qua menu while(current->key != key) //neu day la last node if(current->next == NULL) return NULL; else //luu tham chieu toi liên kết hien tai previous = current; //di chuyen toi next liên kết current = current->next; //cap nhat links if(current == head) //thay doi first de tro toi next links head = head->next; else //bo qua links hien tai previous->next = current->next; return current;// mê say sap xepvoid sort() int i, j, k, tempKey, tempData ; struct node *current; struct node *next; int kích cỡ = length(); k = kích cỡ ; for ( i = 0 ; i next ; for ( j = 1 ; j data > next->data ) tempData = current->data ; current->data = next->data; next->data = tempData ; tempKey = current->key; current->key = next->key; next->key = tempKey; current = current->next; next = next->next; } }// đắm đuối dao nguoc listvoid reverse(struct node** head_ref) struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) next = current->next; current->next = prev; prev = current; current = next; *head_ref = prev;main() insertFirst(1,10); insertFirst(2,20); insertFirst(3,30); insertFirst(4,1); insertFirst(5,40); insertFirst(6,56); printf("Danh sach ban dau: "); //in danh sach printList(); while(!isEmpty()) struct node *temp = deleteFirst(); printf(" Gia tri bi xoa:"); printf("(%d,%d) ",temp->key,temp->data); printf(" Danh sach sau khi da xoa gia tri: "); printList(); insertFirst(1,10); insertFirst(2,20); insertFirst(3,30); insertFirst(4,1); insertFirst(5,40); insertFirst(6,56); printf(" Phuc hoi danh sach: "); printList(); printf(" "); struct node *foundLink = find(4); if(foundLink != NULL) printf("Tim thế phan tu: "); printf("(%d,%d) ",foundLink->key,foundLink->data); printf(" "); else printf("Khong tim núm phan tu."); deleteKey(4); printf("Danh sach, sau thời điểm xoa mot phan tu: "); printList(); printf(" "); foundLink = find(4); if(foundLink != NULL) printf("Tim cố kỉnh phan tu: "); printf("(%d,%d) ",foundLink->key,foundLink->data); printf(" "); else printf("Khong tim cố phan tu."); printf(" "); sort(); printf("Danh sach sau thời điểm duoc sap xep: "); printList(); reverse(&head); printf(" Danh sach sau khi bi dao nguoc: "); printList();
Kết quả
Biên dịch với chạy chương trình C bên trên sẽ mang đến kết quả:
Đã có phầm mềm hutgiammo.com trên năng lượng điện thoại, giải bài bác tập SGK, SBT biên soạn văn, Văn mẫu, Thi online, bài xích giảng....miễn phí. Thiết lập ngay vận dụng trên android và iOS.

Xem thêm: Bài Tập Trắc Nghiệm Dòng Điện Xoay Chiều Có Đáp Án, Dòng Điện Xoay Chiều

Follow fanpage của team https://www.facebook.com/hutgiammo.comteam/ hoặc facebook cá thể Nguyễn Thanh Tuyền https://www.facebook.com/tuyen.hutgiammo.com để liên tục theo dõi các loạt bài tiên tiến nhất về Java,C,C++,Javascript,HTML,Python,Database,Mobile.... Mới nhất của chúng tôi.