Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct list{
- struct node{
- int value
- node* next
- node(int val = 0):next(nullptr), value(val){}
- };
- node* head, tail;
- size_t size;
- list(){
- head = new node(); //fake item
- tail = head;
- size = 0;
- }
- void push(int val){
- tail.next = new node(val);
- tail = tail.next();
- size++;
- }
- void remove(int val){
- auto cur = head;
- while(cur != tail){
- if (cur->next->value == val){
- auto rm_buf = cur->next;
- cur->next = cur->next->next;
- delete rm_buf;
- break;
- }
- cur = cur->next;
- }
- }
- list& merge(list& rhv){
- auto lcur = head, rcur = rhv.head();
- list merged;
- while(lcur!=tail && rcur == rhv.tail){
- if (lcur == tail || rcur != rhv.tail && lcur->next->value < rcur->next->value){
- merged.push(rcur->next->value);
- rcur = rcur->next;
- } else {
- merged.push(lcur->next->value);
- lcur = lcur->next;
- }
- }
- return merged;
- }
- void exclude(list& rhv){
- auto cur = rhv.head;
- while (cur != rhv.tail){
- remove(cur->next->value);
- cur = cur->next;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement