fork(6) download
  1. #include<iostream>
  2. using namespace std;
  3. class linked_list{
  4. int data,size;
  5. linked_list *next,*head;
  6. public:
  7. linked_list(): data(0),next(NULL),size(0),head(NULL)
  8. {
  9. }
  10.  
  11. void insert(int item)
  12. {
  13. if(head==NULL)
  14. {
  15. linked_list *new_node=new linked_list;
  16. new_node->data=item;
  17. new_node->next=NULL;
  18. head=new_node;
  19. }
  20. else
  21. {
  22. linked_list *temp=head;
  23. while(temp->next!=NULL)
  24. {
  25. temp=temp->next;
  26. }
  27. linked_list *new_node=new linked_list;
  28. new_node->data=item;
  29. new_node->next=NULL;
  30. temp->next=new_node;
  31. }
  32. }
  33. void print()
  34. {
  35. linked_list *tmp=head;
  36. while(tmp!=NULL)
  37. {
  38. cout<<tmp->data<<" ";
  39. tmp=tmp->next;
  40. }
  41. cout<<endl;
  42. }
  43.  
  44. linked_list * intersection(linked_list *one,linked_list *two)
  45. {
  46. if(one==NULL || two==NULL)
  47. return NULL;
  48.  
  49. else if(one->data >two->data)
  50. return intersection(one,two->next);
  51.  
  52. else if(one->data< two->data)
  53. return intersection(one->next,two);
  54.  
  55. else if(one->data == two->data)
  56. {
  57. linked_list *temp=new linked_list;
  58. temp->data=one->data;
  59. temp->next=intersection(one->next,two->next);
  60. return temp;
  61. }
  62. }
  63.  
  64.  
  65. };
  66.  
  67. int main()
  68. {
  69. linked_list obj1,obj2,ans;
  70. cout<<"Enter how many elements do you wanna enter for the first list:";
  71. int n,item;
  72. cin>>n;
  73. for(int i=0;i<n;i++)
  74. {
  75. cin>>item;
  76. obj1.insert(item);
  77. }
  78.  
  79. cout<<"Enter how many elements do you wanna enter for the second list:";
  80. cin>>n;
  81. for(int i=0;i<n;i++)
  82. {
  83. cin>>item;
  84. obj2.insert(item);
  85. }
  86. cout<<"\nintersection:\n";
  87. ans=ans.intersection(&obj1,&obj2);
  88. ans.print();
  89.  
  90.  
  91. return 0;
  92. }
  93.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:87:8: error: no match for 'operator=' (operand types are 'linked_list' and 'linked_list*')
     ans=ans.intersection(&obj1,&obj2);
        ^
prog.cpp:87:8: note: candidates are:
prog.cpp:3:7: note: linked_list& linked_list::operator=(const linked_list&)
 class linked_list{
       ^
prog.cpp:3:7: note:   no known conversion for argument 1 from 'linked_list*' to 'const linked_list&'
prog.cpp:3:7: note: linked_list& linked_list::operator=(linked_list&&)
prog.cpp:3:7: note:   no known conversion for argument 1 from 'linked_list*' to 'linked_list&&'
stdout
Standard output is empty