运行 ❯
获取您
的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class Node: def __init__(self, data): self.data = data self.next = None self.prev = None node1 = Node(3) node2 = Node(5) node3 = Node(13) node4 = Node(2) node1.next = node2 node1.prev = node4 # Circular link node2.prev = node1 node2.next = node3 node3.prev = node2 node3.next = node4 node4.prev = node3 node4.next = node1 # Circular link print("\nTraversing forward:") currentNode = node1 startNode = node1 print(currentNode.data, end=" -> ") currentNode = currentNode.next while currentNode != startNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("...") # Indicating it's circular print("\nTraversing backward:") currentNode = node4 startNode = node4 print(currentNode.data, end=" -> ") currentNode = currentNode.prev while currentNode != startNode: print(currentNode.data, end=" -> ") currentNode = currentNode.prev print("...") # Indicating it's circular #Python
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; struct Node* prev; } Node; 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)); node1->data = 3; node2->data = 5; node3->data = 13; node4->data = 2; node1->next = node2; node1->prev = node4; // Circular link node2->prev = node1; node2->next = node3; node3->prev = node2; node3->next = node4; node4->prev = node3; node4->next = node1; // Circular link printf("\nTraversing forward:\n"); Node* currentNode = node1; Node* startNode = node1; printf("%d -> ", currentNode->data); currentNode = currentNode->next; while (currentNode != startNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("...\n"); // Indicating the list loops back printf("\nTraversing backward:\n"); currentNode = node4; startNode = node4; printf("%d -> ", currentNode->data); currentNode = currentNode->prev; while (currentNode != startNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->prev; } printf("...\n"); // Indicating the list loops back free(node1); free(node2); free(node3); free(node4); return 0; } //C
public class Main { static class Node { int data; Node next; Node prev; Node(int data) { this.data = data; } } public static void main(String[] args) { Node node1 = new Node(3); Node node2 = new Node(5); Node node3 = new Node(13); Node node4 = new Node(2); node1.next = node2; node1.prev = node4; // Circular link node2.prev = node1; node2.next = node3; node3.prev = node2; node3.next = node4; node4.prev = node3; node4.next = node1; // Circular link System.out.println("\nTraversing forward:"); Node currentNode = node1; Node startNode = node1; System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; while (currentNode != startNode) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("..."); // Indicating the list loops back System.out.println("\nTraversing backward:"); currentNode = node4; startNode = node4; System.out.print(currentNode.data + " -> "); currentNode = currentNode.prev; while (currentNode != startNode) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.prev; } System.out.println("..."); // Indicating the list loops back } } //Java
Python 结果
C 结果
Java 结果
向前遍历
3 -> 5 -> 13 -> 2 -> ...
向后遍历
2 -> 13 -> 5 -> 3 -> ...
向前遍历
3 -> 5 -> 13 -> 2 -> ...
向后遍历
2 -> 13 -> 5 -> 3 -> ...
向前遍历
3 -> 5 -> 13 -> 2 -> ...
向后遍历
2 -> 13 -> 5 -> 3 -> ...