Chạy ❯
Nhận trang web của
riêng
bạn
×
Thay đổi định hướng
Thay đổi chủ đề, Tối/Sáng
Đi tới Không gian
Python
C
Java
lớp Nút: def __init__(self, data): self.data = dữ liệu self.next = Không có self.prev = Không có nút1 = Nút(3) nút2 = Nút(5) nút3 = Nút(13) nút4 = Nút(2) node1.next = node2 node1.prev = node4 # Liên kết vòng tròn node2.prev = node1 node2.next = node3 node3.prev = node2 node3.next = node4 node4.prev = node3 node4.next = node1 # In liên kết vòng tròn("\ nDi chuyển tiếp:") currentNode = node1 startNode = node1 print(currentNode.data, end=" -> ") currentNode = currentNode.next while currentNode != startNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("...") # Cho biết đó là in tròn("\nDi chuyển ngược:") currentNode = node4 startNode = node4 print(currentNode.data, end=" -> ") currentNode = currentNode.prev while currentNode != startNode: print(currentNode.data, end=" -> ") currentNode = currentNode.prev print("...") # Cho biết đó là hình tròn #Python
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* tiếp theo; } Nút; int main() { Node* node1 = (Node*) malloc(sizeof(Node)); Node* node2 = (Node*) malloc(sizeof(Node)); Node* node3 = (Node*) malloc(sizeof(Node)); Node* node4 = (Node*) malloc(sizeof(Node)); nút1->dữ liệu = 3; nút2->dữ liệu = 5; nút3->dữ liệu = 13; nút4->dữ liệu = 2; nút1->tiếp theo = nút2; nút2->tiếp theo = nút3; nút3->tiếp theo = nút4; nút4->tiếp theo = nút1; // Nút liên kết vòng* currentNode = node1; Nút* startNode = nút1; printf("%d -> ", currentNode->data); currentNode = currentNode->next; while (currentNode != startNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("...\n"); // Hiển thị danh sách vòng lặp free(node1); miễn phí (nút2); miễn phí (nút3); miễn phí (nút4); trả về 0; } //C
lớp công khai Main { lớp tĩnh Nút { dữ liệu int; Nút tiếp theo; Nút(int data) { this.data = data; this.next = null; } } public static void main(String[] args) { Nút node1 = Nút mới(3); Nút node2 = Nút mới(5); Nút node3 = Nút mới(13); Nút nút4 = Nút mới(2); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = nút1; // Liên kết vòng Node currentNode = node1; Nút startNode = nút1; System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; while (currentNode != startNode) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("..."); // Hiển thị danh sách lặp lại } } //Java
Kết quả Python:
Kết quả C:
Kết quả Java:
Di chuyển về phía trước:
3 -> 5 -> 13 -> 2 -> ...
Đi ngược chiều:
2 -> 13 -> 5 -> 3 -> ...
3 -> 5 -> 13 -> 2 -> ...
3 -> 5 -> 13 -> 2 -> ...