fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct DNode{
  5. int data;
  6. DNode* pPrev, *pNext;
  7. };
  8.  
  9. void insertAtBeginning(DNode* &pHead, int x);
  10. void insertB4K(DNode* &pHead, int x, int k);
  11.  
  12. //-----------------------------------------------------//
  13.  
  14. void insertAtBeginning(DNode* &pHead, int x) {
  15. DNode* tmp = new DNode;
  16. tmp->data = x;
  17. tmp->pNext = pHead;
  18. tmp->pPrev = nullptr;
  19. if(pHead) pHead->pPrev = tmp;
  20. pHead = tmp;
  21. }
  22.  
  23. //-----------------------------------------------------//
  24.  
  25. void insertB4K(DNode* &pHead, int x, int k) {
  26. for(DNode* cur = pHead; cur != nullptr; cur = cur->pNext) {
  27. if(cur->data == k) {
  28. DNode* tmp = new DNode;
  29. tmp->data = x;
  30. tmp->pPrev = cur->pPrev;
  31.  
  32. if(cur->pPrev) cur->pPrev->pNext = tmp;
  33. else pHead = tmp;
  34.  
  35. tmp->pNext = cur;
  36. cur->pPrev = tmp;
  37. break;
  38. }
  39. }
  40. }
  41.  
  42. //-----------------------------------------------------//
  43.  
  44. int main() {}
Success #stdin #stdout 0.01s 5460KB
stdin
Standard input is empty
stdout
Standard output is empty