Lớp 2 - liên kết 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 tập
Đề thi
Lớp 6Lớp 6 - kết nối 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 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 bác 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 giờ 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:

Cấu trúc dữ liệu và giải thuậtMột số định nghĩa về Giải thuật kết cấu dữ liệu mảng (Array)Danh sách links - Linked ListsNgăn xếp & Hàng đợiMột số giải mã tìm kiếmMột số giải mã 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 links (Linked List) trong C
Trang trước
Trang sau
Một Danh sách link (Linked List) là 1 trong dãy các cấu trúc dữ liệu được liên kết với nhau trải qua các link (link). đọc một cách đơn giản dễ dàng thì Danh sách link là một cấu tạo dữ liệu gồm một nhóm những nút (node) chế tạo ra thành một chuỗi. Từng nút gồm tài liệu ở nút đó và tham chiếu đến nút tiếp đến trong chuỗi.
Chương trình minh họa Danh sách links (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 link tai vi tri dau tienvoid insertFirst(int key, int data) //tao mot links 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 link struct node *tempLink = head; //danh dau next toi first links la first head = head->next; //tra ve liên kết 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 links struct node* current = head; //neu danh mục la trong if(head == NULL) return NULL; //duyet qua menu while(current->key != key) //neu day la last node if(current->next == NULL) return NULL; else //di chuyen toi next links current = current->next; //neu tim nỗ lực du lieu, tra ve liên kết hien tai return current;//xoa mot links voi key da chostruct node* deleteKey(int key) //bat dau tu first links struct node* current = head; struct node* previous = NULL; //neu các mục la trong 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 //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 liên kết if(current == head) //thay doi first de tro toi next links head = head->next; else //bo qua link hien tai previous->next = current->next; return current;// yêu thích sap xepvoid sort() int i, j, k, tempKey, tempData ; struct node *current; struct node *next; int kích cỡ = length(); k = size ; 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 say 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 thời điểm 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 nuốm phan tu: "); printf("(%d,%d) ",foundLink->key,foundLink->data); printf(" "); else printf("Khong tim cố gắng phan tu."); deleteKey(4); printf("Danh sach, sau khoản thời gian xoa mot phan tu: "); printList(); printf(" "); foundLink = find(4); if(foundLink != NULL) printf("Tim chũm phan tu: "); printf("(%d,%d) ",foundLink->key,foundLink->data); printf(" "); else printf("Khong tim cố phan tu."); printf(" "); sort(); printf("Danh sach sau khoản thời gian duoc sap xep: "); printList(); reverse(&head); printf(" Danh sach sau thời điểm bi dao nguoc: "); printList();
Kết quả
Biên dịch và chạy lịch trình C trên sẽ đến kết quả:
Đã có ứng dụng reset1010.com trên điện thoại, giải bài bác tập SGK, SBT soạn văn, Văn mẫu, Thi online, bài giảng....miễn phí. Thiết lập ngay áp dụng trên android và iOS.

Xem thêm: Mã Ngành Đại Học Luật Tp Hcm, Thông Tin Tuyển Sinh Trường Đại Học Luật Tp

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