Advertisement
myname0

очередь

May 18th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 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 List
  9. {
  10.         struct Element
  11.         {
  12.                 X inf;
  13.                 Element *next;
  14.                 Element (X x): inf(x), next(0)
  15.                 {
  16.                 }
  17.         };
  18.         Element *head;
  19.         int size;
  20.         Element *Find( int index)
  21.         {
  22.                 if(index < 1||index > size)
  23.                         return NULL;
  24.                 else
  25.                 {
  26.                         Element *cur = head;
  27.                         for(int i = 1; i < index; i++)
  28.                                 cur = cur->next;
  29.                         return cur;
  30.                 }
  31.         }
  32. public:
  33.         List():head(0), size(0)
  34.         {
  35.         }
  36.         ~List()
  37.         {
  38.                 while(!Empty())
  39.                         Remove(1);
  40.         }
  41.         bool Empty()
  42.         {
  43.                 return head == 0;
  44.         }
  45.         int GetLength()
  46.         {
  47.                 return size;
  48.         }
  49.         X Get(int index)
  50.         {
  51.                 if (index < 1 || index > size)
  52.                 {
  53.                         throw ListException("ListException: get - list error");
  54.                 }
  55.                 else
  56.                 {
  57.                         Element *r = Find(index);
  58.                         X i = r->inf;
  59.                         return i;
  60.                 }
  61.         }
  62.         void Insert(X data, int index)
  63.         {
  64.                 if (index < 1 || index > size + 1)
  65.                 {
  66.                         throw ListException("ListException: get - list error");
  67.                 }
  68.                 else
  69.                 {
  70.                         Element *newPtr = new Element(data);
  71.                         size = GetLength() + 1;
  72.                         if (index == 1)
  73.                         {
  74.                                 newPtr->next = head;
  75.                                 head = newPtr;
  76.                         }
  77.                         else
  78.                         {
  79.                                 Element *prev = Find(index - 1);
  80.                                 newPtr->next = prev->next;
  81.                                 prev->next = newPtr;
  82.                         }
  83.                 }
  84.         }
  85.         void Remove (int index)
  86.         {
  87.                 if (index < 1 || index > size)
  88.                 {
  89.                         throw ListException("ListException: get - list error");
  90.                 }
  91.                 else
  92.                 {
  93.                         Element *cur;
  94.                         --size;
  95.                         if (index == 1)
  96.                         {
  97.                                 cur = head;
  98.                                 head = head->next;
  99.                         }
  100.                         else
  101.                         {
  102.                                 Element * prev = Find(index - 1);
  103.                                 cur = prev->next;
  104.                                 prev->next = cur->next;
  105.                         }
  106.                         cur->next = NULL;
  107.                         delete cur;
  108.                 }
  109.         }
  110.         void Change ()
  111.         {
  112.                 //Element *cur = head;
  113.                 for(Element *cur = head; cur != NULL; cur = cur -> next)
  114.                 {
  115.                         if((cur->inf) % 2 == 0)
  116.                                 cur->inf = (cur -> inf)*2;
  117.                 }
  118.         }
  119.         void Print(ofstream &out)
  120.         {
  121.                 for(Element * cur = head; cur != NULL; cur = cur->next)
  122.                         out << cur->inf << " ";
  123.                 out << endl;
  124.         }
  125.  
  126. };
  127.  
  128. int main()
  129. {
  130.         List <int> t;
  131.         int i;
  132.         ifstream in ("input.txt");
  133.         ofstream out ("output.txt");
  134.         while (in >> i)
  135.                 t.Insert(i, t.GetLength()+1);
  136.         in.close();
  137.         t.Change();
  138.         t.Print(out);
  139.         t.~List();
  140.         out.close();
  141.         return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement