运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class Node: def __init__(self, data): self.data = data self.next = None node1 = Node(3) node2 = Node(5) node3 = Node(13) node4 = Node(2) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node1 # Circular link currentNode = node1 startNode = node1 print(currentNode.data, end=" -> ") currentNode = currentNode.next while currentNode != startNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("...") # Indicating the list loops back #Python
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } 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; node2->next = node3; node3->next = node4; node4->next = node1; // Circular link 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 free(node1); free(node2); free(node3); free(node4); return 0; } //C
public class Main { static class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } 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; node2.next = node3; node3.next = node4; node4.next = node1; // Circular link 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 } } //Java
Python 结果
C 结果
Java 结果
3 -> 5 -> 13 -> 2 -> ...
3 -> 5 -> 13 -> 2 -> ...
3 -> 5 -> 13 -> 2 -> ...