fork download
  1. //Merge K Sorted Lists
  2.  
  3. #include<iostream>
  4. #include<queue>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. class node{
  9. public:
  10. int data;
  11. node* next;
  12.  
  13. node(int d) {
  14. data = d;
  15. next = NULL;
  16. }
  17. };
  18.  
  19. void insertAtTail(node* &head, int data){
  20. //if there is no data
  21. if(head == NULL){
  22. head = new node(data);
  23. return;
  24. }
  25.  
  26. //otherwise need to find tail point
  27. node*tail = head;
  28. while(tail->next != NULL){
  29. tail = tail->next;
  30. }
  31. tail->next = new node(data);
  32. return;
  33. }
  34.  
  35. void printList(node *head) {
  36. while(head != NULL) {
  37. cout << head->data << " ";
  38. head = head->next;
  39. }
  40. cout << endl;
  41. }
  42.  
  43. // Comparison object to be used to order the min-heap
  44. class compare{
  45. bool operator() (node *lhs, node *rhs) {
  46. return lhs->data < rhs->data;
  47. }
  48. };
  49.  
  50.  
  51. // The main function to merge given `k` sorted linked lists.
  52. // It takes array `lists` of size `k` and generates the sorted output
  53. node *mergeKSortedList(vector<node *> &lists) {
  54.  
  55. //create a empty min heap
  56. priority_queue<node*, vector<node* >, compare> pq;
  57.  
  58. // push the first node of each list into the min-heap
  59. for (node* list: lists) {
  60. pq.push(list);
  61. }
  62.  
  63. // take two pointers, head and tail, where the head points to the first node
  64. // of the output list and tail points to its last node
  65. node *head = NULL;
  66. node *last = NULL;
  67.  
  68. // run till min-heap is empty
  69. while (!pq.empty())
  70. {
  71. // extract the minimum node from the min-heap
  72. node* min = pq.top();
  73. pq.pop();
  74.  
  75. // add the minimum node to the output list
  76. if (head == NULL) {
  77. head = last = min;
  78. }
  79. else {
  80. last->next = min;
  81. last = min;
  82. }
  83.  
  84. // take the next node from the "same" list and insert it into the min-heap
  85. if (min->next != NULL) {
  86. pq.push(min->next);
  87. }
  88. }
  89. // return head node of the merged list
  90. return head;
  91. }
  92.  
  93. int main() {
  94.  
  95. int k;
  96. int n;
  97.  
  98. cin >> k >> n;
  99.  
  100. // an array to store the head nodes of the linked lists
  101. vector<node *> lists(k);
  102.  
  103. //input
  104.  
  105. node *head = mergeKSortedList(lists);
  106. printList(head);
  107. return 0;
  108. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/8/bits/stl_algobase.h:71,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:3:
/usr/include/c++/8/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Value = node*; _Compare = compare]’:
/usr/include/c++/8/bits/stl_heap.h:133:48:   required from ‘void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Distance = long int; _Tp = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_val<compare>]’
/usr/include/c++/8/bits/stl_heap.h:207:23:   required from ‘void std::push_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Compare = compare]’
/usr/include/c++/8/bits/stl_queue.h:610:16:   required from ‘void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = node*; _Sequence = std::vector<node*>; _Compare = compare; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = node*]’
prog.cpp:60:21:   required from here
/usr/include/c++/8/bits/predefined_ops.h:177:11: error: ‘bool compare::operator()(node*, node*)’ is private within this context
  { return bool(_M_comp(*__it, __val)); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:45:10: note: declared private here
     bool operator() (node *lhs, node *rhs) {
          ^~~~~~~~
In file included from /usr/include/c++/8/bits/stl_algobase.h:71,
                 from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:3:
/usr/include/c++/8/bits/predefined_ops.h: In instantiation of ‘constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Iterator2 = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Compare = compare]’:
/usr/include/c++/8/bits/stl_heap.h:222:14:   required from ‘void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Distance = long int; _Tp = node*; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<compare>]’
/usr/include/c++/8/bits/stl_heap.h:253:25:   required from ‘void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<compare>]’
/usr/include/c++/8/bits/stl_heap.h:320:19:   required from ‘void std::pop_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<node**, std::vector<node*> >; _Compare = compare]’
/usr/include/c++/8/bits/stl_queue.h:645:15:   required from ‘void std::priority_queue<_Tp, _Sequence, _Compare>::pop() [with _Tp = node*; _Sequence = std::vector<node*>; _Compare = compare]’
prog.cpp:73:16:   required from here
/usr/include/c++/8/bits/predefined_ops.h:143:18: error: ‘bool compare::operator()(node*, node*)’ is private within this context
         { return bool(_M_comp(*__it1, *__it2)); }
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cpp:45:10: note: declared private here
     bool operator() (node *lhs, node *rhs) {
          ^~~~~~~~
stdout
Standard output is empty