Hướng dẫn what are the types of linked list in python? - các loại danh sách liên kết trong python là gì?
Các loại danh sách được liên kết - liên kết đơn lẻ, liên kết gấp đôi và hình trònTrong hướng dẫn này, bạn sẽ tìm hiểu các loại danh sách được liên kết khác nhau. Ngoài ra, bạn sẽ tìm thấy việc triển khai danh sách được liên kết trong C. Show
Trước khi bạn tìm hiểu về loại danh sách được liên kết, hãy chắc chắn rằng bạn biết về cấu trúc dữ liệu LinkedList. Có ba loại danh sách liên kết phổ biến.
Danh sách liên kết đơn lẻDanh sách liên kết gấp đôi Nó là phổ biến nhất. Mỗi nút có dữ liệu và một con trỏ tới nút tiếp theo.
Danh sách liên kết đơn lẻ
Danh sách liên kết gấp đôiDanh sách liên kết tròn Danh sách liên kết đơn lẻ
Nút được biểu diễn là:
Danh sách liên kết đơn ba thành viên có thể được tạo như: Danh sách liên kết trònNó là phổ biến nhất. Mỗi nút có dữ liệu và một con trỏ tới nút tiếp theo. Nút được biểu diễn là:
Danh sách liên kết gấp đôi
Một nút được biểu diễn dưới dạng Danh sách được liên kết là một cấu trúc dữ liệu tuyến tính, trong đó các phần tử không được lưu trữ tại các vị trí bộ nhớ liên tục. Các yếu tố trong một danh sách được liên kết được liên kết bằng con trỏ. Nói một cách đơn giản, một danh sách được liên kết bao gồm các nút trong đó mỗi nút chứa một trường dữ liệu và một tham chiếu (liên kết) đến nút tiếp theo trong danh sách. Là liên kết ngăn xếp hoặc hàng đợi?Trong một ngăn xếp, chúng tôi loại bỏ các mặt hàng gần đây nhất; Trong một hàng đợi, chúng tôi loại bỏ các mặt hàng ít được thêm vào gần đây. Có nhiều cách để thực hiện cấu trúc dữ liệu hàng đợi, nhưng chúng tôi sẽ thực hiện nó bằng một danh sách được liên kết.Singly Linked List
Các loại danh sách được liên kết: 1. Danh sách liên kết đơn lẻ C++Đây là loại danh sách được liên kết đơn giản nhất trong đó mọi nút chứa một số dữ liệu và con trỏ tới nút tiếp theo của cùng loại dữ liệu. & NBSP; 1 2Nút chứa một con trỏ tới nút tiếp theo có nghĩa là nút lưu trữ địa chỉ của nút tiếp theo trong chuỗi. Một danh sách được liên kết duy nhất cho phép truyền dữ liệu chỉ theo một cách. Dưới đây là hình ảnh cho cùng: 3 7 8Dưới đây là cấu trúc của danh sách liên kết đơn lẻ
0Nút chứa một con trỏ tới nút tiếp theo có nghĩa là nút lưu trữ địa chỉ của nút tiếp theo trong chuỗi. Một danh sách được liên kết duy nhất cho phép truyền dữ liệu chỉ theo một cách. Dưới đây là hình ảnh cho cùng: 3 6 8Python3Dưới đây là cấu trúc của danh sách liên kết đơn lẻ
0 3 4 5Java C# 9 class 0
9 3 1 2 3 4 8JavaScriptCreated DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 17 Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 3Contents of Circular Linked List 11 2 56 120 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5Contents of Circular Linked List 11 2 56 124 Contents of Circular Linked List 11 2 56 125 5Contents of Circular Linked List 11 2 56 124 Contents of Circular Linked List 11 2 56 128 Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Sáng tạo và truyền tải danh sách liên kết đơn lẻ:C++Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 74 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 75 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 76 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 77
0 1 2 3 4 5 3 7 8List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 159 Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 3class 2 class 3 5class 5class 6Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5class 9 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 4 04Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 3 07 3 09 3 11 3 13 14 15 3 17 14 15 3 21 14 15 3 25 3 27 3 29 3 31 3 33 3 35 3 37 3 39 40Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Java
43 3 9 class 0 5 4 5 5 6 3 8 3 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 58 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5class 2 63Contents of Circular Linked List 11 2 56 129 65 66 67class 6 69 66 71 5Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 3 1 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 80 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5 84Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 88Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 92Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 13 14 15 5 17 14 15 5 21 14 15 5 08 09Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 12 5 14 15Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 18 5 20 21Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 24Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 37 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 C#Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 75 33
43 3 9 class 0 5 4 5 3 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 58 3 8 3 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 58 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5class 2 63Contents of Circular Linked List 11 2 56 129 65 66 67class 6 69 66 71 5Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 3 1 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 80 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5 84Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 88Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 92Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 5 13 14 15 5 17 14 15 5 21 14 15 5 02 5 12 5 06 5 18 5 10 5 18 5 37 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Python3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 75 33 3 1 class 0 5 1 4 5 5 1 6 66 61class 6 69 3 1 9 List After inserting 3 elements: 11 12 13 List After inserting 2 more elements: 11 12 13 14 158 74 5 24Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70
9 3 1 2 3 4 5 3 7 8 9 5 3 2 3 8 5
39 5 79 2 3 1 2 3 44 5 3 47 8 5 5 3 522____38 5 3 1 57 3 4 5 61 3 522____364 5 2 66 12 13 8 15 16 66 3 47 8 71 5 12 24 66 3 52 8 3 77 66 3 83 3 8 71 3 36 09 16 3 36 15 16 3 36 21 16 3 48JavaScriptCreated DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 17 66 3 52 8 3 83 3 3Contents of Circular Linked List 11 2 56 120 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5Contents of Circular Linked List 11 2 56 124 Contents of Circular Linked List 11 2 56 125 5Contents of Circular Linked List 11 2 56 124 Contents of Circular Linked List 11 2 56 128 Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 3Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 3 1 96 3 44Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 5 00 8 3 77 69Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 18 3 77class 6 69 3 71 69Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 72 85Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 88Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 91Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 13 14 15 17 14 15 21 14 15 02 12 06 18 10 24Contents of Circular Linked List 11 2 56 129 Created circular doubly linked list is: Traversal in forward direction 7 4 5 Traversal in reverse direction 5 4 70 37Độ phức tạp về thời gian: O (N) Không gian phụ trợ: O (N) O(N) 2. Danh sách liên kết gấp đôiDoubly Linked List
Do đó, nó chứa ba phần của dữ liệu, một con trỏ đến nút tiếp theo và một con trỏ tới nút trước đó. Điều này cũng sẽ cho phép chúng tôi vượt qua danh sách theo hướng lạc hậu. Dưới đây là hình ảnh cho cùng: Cấu trúc của danh sách liên kết gấp đôi:C++Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 112 0 3 4 5 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 112 7 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 112 Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 122 8Java 9 class 0 3 4 5 3 6 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 133 8Python3 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 112 7 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 112 Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 122 Java 9 class 0
9C# 3 1 2 3 4 5 3Created DLL is: Traversal in forward direction 1 7 6 Traversal in reverse direction 6 7 1444____38 5 5 3 7 8 9 5 3 2 3 8 5 8
/* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data = 3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
/* Save address of first node in head */
head = one; /* Initialize nodes */
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
/* Allocate memory */
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
/* Assign data values */
one->data = 1;
two->data = 2;
three->data = 3;
/* Connect nodes */
one->next = two;
two->next = three;
three->next = NULL;
/* Save address of first node in head */
head = one; |