Seal_of_approval

p75e17

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