Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to 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 node2.prev = node1 node2.next = node3 node3.prev = node2 node3.next = node4 node4.prev = node3 print("\nTraversing forward:") currentNode = node1 while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("null") print("\nTraversing backward:") currentNode = node4 while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.prev print("null") #Python
#include
#include
typedef struct Node { int data; struct Node* next; struct Node* prev; } Node; int main() { Node* node1 = (Node*) malloc(sizeof(Node)); node1->data = 3; node1->next = NULL; node1->prev = NULL; Node* node2 = (Node*) malloc(sizeof(Node)); node2->data = 5; node2->next = NULL; node2->prev = node1; node1->next = node2; Node* node3 = (Node*) malloc(sizeof(Node)); node3->data = 13; node3->next = NULL; node3->prev = node2; node2->next = node3; Node* node4 = (Node*) malloc(sizeof(Node)); node4->data = 2; node4->next = NULL; node4->prev = node3; node3->next = node4; Node* currentNode = node1; printf("Forward: "); while (currentNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("NULL\n"); currentNode = node4; printf("Backward: "); while (currentNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->prev; } printf("NULL\n"); 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; this.next = null; this.prev = null; } } public static void main(String[] args) { Node node1 = new Node(3); Node node2 = new Node(5); node1.next = node2; node2.prev = node1; Node node3 = new Node(13); node2.next = node3; node3.prev = node2; Node node4 = new Node(2); node3.next = node4; node4.prev = node3; Node currentNode = node1; System.out.print("Forward: "); while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); currentNode = node4; System.out.print("Backward: "); while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.prev; } System.out.println("null"); } } //Java
Python result:
C result:
Java result:
Traversing forward:
3 -> 5 -> 13 -> 2 -> null
Traversing backward:
2 -> 13 -> 5 -> 3 -> null
Forward: 3 -> 5 -> 13 -> 2 -> NULL
Backward: 2 -> 13 -> 5 -> 3 -> NULL
Forward: 3 -> 5 -> 13 -> 2 -> null
Backward: 2 -> 13 -> 5 -> 3 -> null