fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node {
  5. int data;
  6. Node* prev;
  7. Node* next;
  8. };
  9.  
  10. int main() {
  11. Node* head = new Node{10, NULL, NULL};
  12. head->next = new Node{20, head, NULL};
  13. head->next->next = new Node{30, head->next, NULL};
  14. head->next->next->next = new Node{40, head->next->next, NULL};
  15.  
  16. int pos = 2, value = 25;
  17.  
  18. Node* temp = head;
  19. for(int i = 0; i < pos-1; i++){
  20. temp = temp->next;
  21. }
  22.  
  23. Node* newNode = new Node{value, temp, temp->next};
  24. if(temp->next) temp->next->prev = newNode;
  25. temp->next = newNode;
  26.  
  27. for(Node* t = head; t; t = t->next)
  28. cout << t->data << " ";
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
10 20 25 30 40