Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. struct list{
  2.   struct node{
  3.    int value
  4.    node* next
  5.    node(int val = 0):next(nullptr), value(val){}
  6.   };
  7.   node* head, tail;
  8.   size_t size;
  9.  
  10.   list(){
  11.     head = new node(); //fake item
  12.     tail = head;
  13.     size = 0;
  14.   }
  15.   void push(int val){
  16.     tail.next = new node(val);
  17.     tail = tail.next();
  18.     size++;
  19.   }
  20.  
  21.   void remove(int val){
  22.     auto cur = head;
  23.     while(cur != tail){
  24.       if (cur->next->value == val){
  25.         auto rm_buf = cur->next;
  26.         cur->next = cur->next->next;
  27.         delete rm_buf;
  28.         break;
  29.       }
  30.       cur = cur->next;
  31.     }
  32.   }
  33.  
  34.   list& merge(list& rhv){
  35.     auto lcur = head, rcur = rhv.head();
  36.     list merged;
  37.     while(lcur!=tail && rcur == rhv.tail){
  38.       if (lcur == tail || rcur != rhv.tail && lcur->next->value < rcur->next->value){
  39.          merged.push(rcur->next->value);
  40.          rcur = rcur->next;
  41.       } else {
  42.          merged.push(lcur->next->value);
  43.          lcur = lcur->next;
  44.       }
  45.     }
  46.     return merged;
  47.   }
  48.  
  49.   void exclude(list& rhv){
  50.     auto cur = rhv.head;
  51.     while (cur != rhv.tail){
  52.       remove(cur->next->value);
  53.       cur = cur->next;
  54.     }
  55.   }
  56.  
  57. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement