fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct node {
  5. int data;
  6. node* prev;
  7. node* next;
  8. };
  9.  
  10. node* find(int,node*&);
  11. void afterElement(int,int,node*&,node*&,node* (*find)(int, node*&));
  12.  
  13. int main() {
  14. node* head = NULL;
  15. node* tail = NULL;
  16. // The program itself has a menu that allows for input of value in list but
  17. // for the sake of relevancy and shortness of code I dropped it out from here
  18.  
  19. int x, y;
  20. cout << "Insert 2 values: value you wish to insert, and value you wish to insert it after. ";
  21. cin >> x;
  22. cin >> y;
  23. afterElement(x,y,head,tail,find); // here is the error "overloaded function..."
  24. return 0;
  25. }
  26.  
  27. node* find(int x,node*& head) {
  28. node* curr = head;
  29. while ((curr != NULL) && (curr->data != x))
  30. curr = curr->next;
  31. return curr;
  32. }
  33.  
  34. void afterElement(int x,int after,node*& head,node*& tail,node* (*find)(int x, node*& head)) {
  35. node* N;
  36. node* compared = (*find)(after,head);
  37. N->data = x;
  38. if (compared == NULL)
  39. cout << "There is no element " << after << " in the list!\n";
  40. else {
  41. if (compared->next == NULL) {
  42. compared->next = N;
  43. N->prev = compared;
  44. N->next = NULL;
  45. tail = N;
  46. } else {
  47. compared->next->prev = N;
  48. N->next = compared->next;
  49. compared->next = N;
  50. N->prev = compared;
  51. }
  52. }
  53. }
  54.  
  55.  
Runtime error #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Insert 2 values: value you wish to insert, and value you wish to insert it after.