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 node2.prev = node1 node2.next = node3 node3.prev = node2 node3.next = node4 node4.prev = node3 print("\nDi chuyển tiếp:") currentNode = node1 while currentNode: print(currentNode.data , end=" -> ") currentNode = currentNode.next print("null") print("\nDi chuyển ngược:") currentNode = node4 while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode .prev print("null") #Python
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* tiếp theo; struct Node* trước; } Nút; int main() { Node* node1 = (Node*) malloc(sizeof(Node)); nút1->dữ liệu = 3; nút1->tiếp theo = NULL; nút1->trước = NULL; Node* node2 = (Node*) malloc(sizeof(Node)); nút2->dữ liệu = 5; node2->next = NULL; nút2->prev = nút1; nút1->tiếp theo = nút2; Node* node3 = (Node*) malloc(sizeof(Node)); nút3->dữ liệu = 13; nút3->tiếp theo = NULL; nút3->prev = nút2; nút2->tiếp theo = nút3; Node* node4 = (Node*) malloc(sizeof(Node)); nút4->dữ liệu = 2; node4->next = NULL; nút4->prev = nút3; nút3->tiếp theo = nút4; Nút* currentNode = nút1; printf("Chuyển tiếp: "); while (currentNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("NULL\n"); currentNode = node4; printf("Lùi: "); while (currentNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->prev; } printf("NULL\n"); miễn phí (nút1); 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 trước; Nút(int data) { this.data = data; this.next = null; this.prev = null; } } public static void main(String[] args) { Nút node1 = Nút mới(3); Nút node2 = Nút mới(5); node1.next = node2; node2.prev = nút1; Nút node3 = Nút mới(13); node2.next = node3; node3.prev = node2; Nút nút4 = Nút mới(2); node3.next = node4; node4.prev = node3; Nút hiện tạiNode = nút1; System.out.print("Chuyển tiếp: "); while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); currentNode = node4; System.out.print("Lùi: "); while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.prev; } System.out.println("null"); } } //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 -> rỗng
Đi ngược chiều:
2 -> 13 -> 5 -> 3 -> rỗng
Chuyển tiếp: 3 -> 5 -> 13 -> 2 -> NULL
Lùi lại: 2 -> 13 -> 5 -> 3 -> NULL
3 -> 5 -> 13 -> 2 -> rỗng