Advertisement
myname0

двусписк

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