Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>
  2. #include "double_list.h"
  3.  
  4. using namespace std;
  5.  
  6. DoubleList::DoubleList()
  7. {
  8.     first = nullptr;
  9.     last = nullptr;
  10.     size = 0;
  11. }
  12.  
  13. DoubleList::DoubleList(const DoubleList &l2)
  14. {
  15.     if (l2.first == nullptr)
  16.     {
  17.         first = nullptr;
  18.         last = nullptr;
  19.         size = 0;
  20.     }
  21.     else
  22.     {
  23.         size = 0;
  24.         list_node *p = l2.first;
  25.         while (p != nullptr)
  26.         {
  27.             add_last_node(p->info);
  28.             p = p->next;
  29.         }
  30.     }
  31. }
  32.  
  33. DoubleList::~DoubleList()
  34. {
  35.     while (!empty())
  36.     delete_first_node();
  37. }
  38.  
  39. void DoubleList::add_first_node(int x)
  40. {
  41.     if (empty())
  42.     {
  43.         first = new list_node;
  44.         last = first;
  45.         first->info = x;
  46.         first->prev = nullptr;
  47.         first->next = nullptr;
  48.         size++;
  49.     }
  50.     else
  51.     {
  52.         list_node *p = new list_node;
  53.         first->prev = p;
  54.         p->next = first;
  55.         p->info = x;
  56.         p->prev = nullptr;
  57.         first = p;
  58.         size++;
  59.     }
  60. }
  61.  
  62. void DoubleList::add_last_node(int x)
  63. {
  64.     if (empty())
  65.     {
  66.         first = new list_node;
  67.         last = first;
  68.         first->info = x;
  69.         first->prev = nullptr;
  70.         first->next = nullptr;
  71.         size++;
  72.     }
  73.     else
  74.     {
  75.         list_node *p = new list_node;
  76.         p->prev = last;
  77.         p->info = x;
  78.         p->next = nullptr;
  79.         last->next = p;
  80.         last = p;
  81.         size++;
  82.     }
  83. }
  84.  
  85. void DoubleList::print_list()
  86. {
  87.     list_node *p = first;
  88.     while (p != nullptr)
  89.     {
  90.         cout << p->info << " ";
  91.         p = p->next;
  92.     }
  93.     cout << endl;
  94. }
  95.  
  96. int DoubleList::list_size()
  97. {
  98.     return size;
  99. }
  100.  
  101. bool DoubleList::empty()
  102. {
  103.     return (size == 0);
  104. }
  105.  
  106. DoubleList unite_lists(const DoubleList &l1, const DoubleList &l2)
  107. {
  108.     if (!l1.first && !l2.first)
  109.     {
  110.         DoubleList l;
  111.         return l;
  112.     }
  113.     if (!l1.first)
  114.     {
  115.         DoubleList l(l2);
  116.         return l;
  117.     }
  118.     if (!l2.first)
  119.     {
  120.         DoubleList l(l1);
  121.         return l;
  122.     }
  123.     DoubleList copy1(l1);
  124.     DoubleList copy2(l2);
  125.     copy1.last->next = copy2.first;
  126.     copy2.first->prev = copy1.last;
  127.     copy1.last = copy2.last;
  128.     copy1.size += copy2.size;
  129.     DoubleList l(copy1);
  130.     copy2.first = nullptr;
  131.     copy2.last = nullptr;
  132.     copy2.size = 0;
  133.     return l;
  134. }
  135.  
  136. void DoubleList::delete_first_node()
  137. {
  138.     if (first == last)
  139.     {
  140.         delete first;
  141.         size--;
  142.     }
  143.     else
  144.     {
  145.         first = first->next;
  146.         delete first->prev;
  147.         first->prev = nullptr;
  148.         size--;
  149.     }
  150. }
  151.  
  152. void DoubleList::delete_last_node()
  153. {
  154.     if (last == first)
  155.     {
  156.         delete last;
  157.         size--;
  158.     }
  159.     else
  160.     {
  161.         last = last->prev;
  162.         delete last->next;
  163.         last->next = nullptr;
  164.         size--;
  165.     }
  166. }
  167.  
  168. bool DoubleList::element_exists(int x)
  169. {
  170.     list_node *p = first;
  171.     while (p != nullptr)
  172.     {
  173.         if (p->info == x)
  174.             return true;
  175.         p = p->next;
  176.     }
  177.     return false;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement