Seal_of_approval

p74e8

Jun 15th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream in("input.txt");
  7. ofstream out("output.txt");
  8.  
  9. template <class Item>
  10. class DoubleLinkedList
  11. {
  12.     struct Element
  13.     {
  14.         Item inf;
  15.         Element *next;
  16.         Element *prev;
  17.         Element (Item x): inf(x), next(0), prev(0)
  18.         {
  19.         }
  20.     };
  21.     Element *head;
  22.     Element *tail;
  23.     int size;
  24.     Element *Find(int index)
  25.     {
  26.         if(index < 0 || index > size)
  27.             return NULL;
  28.         else
  29.         {
  30.             Element *cur = head;
  31.             int i = 0;
  32.             while (i < index)
  33.                 {
  34.                     if (cur->next == NULL) break;
  35.                     i++;
  36.                     cur = cur->next;
  37.                 }
  38.             return cur;
  39.         }
  40.     }
  41. public:
  42.     DoubleLinkedList():head(0), tail(0), size(0) {}
  43.  
  44.     ~DoubleLinkedList()
  45.     {
  46.         while(!Empty())
  47.             Remove(0);
  48.     }
  49.     bool Empty()
  50.     {
  51.         return head == 0;
  52.     }
  53.     int GetLength()
  54.     {
  55.         return size;
  56.     }
  57.     Item Get(int index)
  58.     {
  59.         if (index < 0 || index > size)
  60.         {
  61.             cout << "Error";
  62.         }
  63.         else
  64.         {
  65.             Element *r = Find(index);
  66.             Item i = r->inf;
  67.             return i;
  68.         }
  69.         return 0;
  70.     }
  71.     void InsertHead(Item data, int index)
  72.     {
  73.         if (index < 0 || index > size)
  74.         {
  75.             cout << "Bug";
  76.         }
  77.         else
  78.         {
  79.             Element *newPtr = new Element(data);
  80.             size = GetLength() + 1;
  81.             Element *cur = Find(index-1);
  82.             if (cur == NULL)
  83.             {
  84.                 head = newPtr;
  85.                 tail = newPtr;
  86.             }
  87.             else
  88.             {
  89.                 newPtr->next = cur;
  90.                 newPtr->prev = cur->prev;
  91.                 cur->prev = newPtr;
  92.                 if(cur == head)
  93.                     head = newPtr;
  94.                 else newPtr->prev->next = newPtr;
  95.             }
  96.         }
  97.     }
  98.     void InsertTail(Item data, int index)
  99.     {
  100.         if ((index < 0 && head != NULL) || (index > size))
  101.         {
  102.             cout << "Bug";
  103.         }
  104.         else
  105.         {
  106.             Element *newPtr = new Element(data);
  107.             size = GetLength() + 1;
  108.             Element *cur = Find(index-1);
  109.             if (cur == NULL)
  110.             {
  111.                 head = newPtr;
  112.                 tail = newPtr;
  113.             }
  114.             else
  115.             {
  116.                 newPtr->next = cur->next;
  117.                 newPtr->prev = cur;
  118.                 cur->next = newPtr;
  119.                 if(cur == tail)
  120.                 {
  121.                     tail = newPtr;
  122.                 }
  123.                 else
  124.                     newPtr->next->prev = newPtr;
  125.             }
  126.         }
  127.     }
  128.     void Remove (int index)
  129.     {
  130.         if (index < 0 || index > size)
  131.         {
  132.             cout << "Error";
  133.         }
  134.         else
  135.         {
  136.             Element *cur = Find(index);
  137.             size--;
  138.             if (size == 0)
  139.             {
  140.                 head = NULL;
  141.                 tail = NULL;
  142.             }
  143.             else if (cur == head)
  144.             {
  145.                 head = head->next;
  146.                 head->prev = NULL;
  147.             }
  148.             else if (cur == tail)
  149.             {
  150.                 tail = tail->prev;
  151.                 tail->next = NULL;
  152.             }
  153.             else
  154.             {
  155.                 cur->prev->next = cur->next;
  156.                 cur->next->prev = cur->prev;
  157.             }
  158.             cur->next = NULL;
  159.             cur->prev = NULL;
  160.             delete cur;
  161.         }
  162.     }
  163.    
  164.     void Print(ofstream &out)
  165.     {
  166.         for(Element * cur = head; cur != NULL; cur = cur->next)
  167.             out << cur->inf << " ";
  168.         out << endl;
  169.     }
  170.    
  171. };
  172.  
  173. int main (void)
  174. {
  175.     DoubleLinkedList <int> l;
  176.     int i;
  177.  
  178.     while (in >> i)
  179.         {
  180.             l.InsertTail(i, l.GetLength());
  181.         }
  182.  
  183.     int k = l.GetLength();
  184.     for (int i = 0; i < k; i++)
  185.     {
  186.         if (l.Get(i) % 2 == 0)
  187.         {
  188.             l.InsertTail(l.Get(i), i);
  189.             k++;
  190.             i++;
  191.         }
  192.     }
  193.  
  194.     l.Print(out);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment