fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node {
  5. public:
  6. int data;
  7. Node* prev;
  8. Node* next;
  9.  
  10. Node(int x) {
  11. data = x;
  12. prev = NULL;
  13. next = NULL;
  14. }
  15. };
  16.  
  17. class DoubleList {
  18. public:
  19. Node* head;
  20.  
  21. DoubleList() {
  22. head = NULL;
  23. }
  24.  
  25. void insertFront(int x) {
  26. Node* n = new Node(x);
  27.  
  28. n->next = head; // connect forward
  29. if (head != NULL)
  30. head->prev = n; // connect backward
  31.  
  32. head = n; // update head
  33. }
  34.  
  35. void print() {
  36. Node* t = head;
  37. while (t != NULL) {
  38. cout << t->data << " ";
  39. t = t->next;
  40. }
  41. cout << endl;
  42. }
  43. };
  44.  
  45. int main() {
  46. DoubleList dl;
  47.  
  48. dl.insertFront(30);
  49. dl.insertFront(20);
  50. dl.insertFront(10);
  51.  
  52. dl.print(); // Output: 10 20 30
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
10 20 30