Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- ifstream in("input.txt");
- ofstream out("output.txt");
- template <class Item>
- class List
- {
- struct Element
- {
- Item inf;
- Element *next;
- Element (Item x): inf(x), next(0)
- {
- }
- };
- Element *head;
- int size;
- Element *Find(int index)
- {
- if(index < 0 || index > size)
- return NULL;
- else
- {
- Element *cur = head;
- int i = 0;
- while (i < index)
- {
- if (cur->next == NULL) break;
- i++;
- cur = cur->next;
- }
- /*for(int i = 0; i < index; i++)
- if (cur != NULL)
- cur = cur->next;
- else break;*/
- return cur;
- }
- }
- public:
- List():head(0), size(0)
- {
- }
- ~List()
- {
- while(!Empty())
- Remove(0);
- }
- bool Empty()
- {
- return head == 0;
- }
- int GetLength()
- {
- return size;
- }
- Item Get(int index)
- {
- if (index < 0 || index > size)
- {
- cout << "Error";
- }
- else
- {
- Element *r = Find(index);
- Item i = r->inf;
- return i;
- }
- return 0;
- }
- void Insert(Item data, int index)
- {
- if (index < 0 || index > size)
- {
- cout << "Bug";
- }
- else
- {
- Element *newPtr = new Element(data);
- size = GetLength() + 1;
- if (index == 0)
- {
- newPtr->next = head;
- head = newPtr;
- }
- else
- {
- Element *prev = Find(index-1);
- newPtr->next = prev->next;
- prev->next = newPtr;
- }
- }
- }
- void Remove (int index)
- {
- if (index < 0 || index > size)
- {
- cout << "ERROR at " << index << endl;
- }
- else
- {
- Element *cur;
- size--;
- if (index == 0)
- {
- cur = head;
- head = head->next;
- }
- else
- {
- Element * prev = Find(index-1);
- cur = prev->next;
- prev->next = cur->next;
- }
- cur->next = NULL;
- delete cur;
- }
- }
- void Print(ofstream &out)
- {
- for(Element * cur = head; cur != NULL; cur = cur->next)
- out << cur->inf << " ";
- out << endl;
- }
- };
- int main(void)
- {
- List <int> l;
- List <int> res;
- int i;
- while (in >> i)
- {
- l.Insert(i, l.GetLength());
- }
- int k = l.GetLength();
- for (int i = 0; i < k; i++)
- {
- if (l.Get(i) < 0)
- {
- res.Insert(l.Get(i), res.GetLength());
- l.Remove(i);
- i--;
- k--;
- }
- }
- for (int i = 0; i < k; i++)
- {
- res.Insert(l.Get(i),res.GetLength());
- l.Remove(i);
- i--;
- k--;
- }
- res.Print(out);
- }
Advertisement
Add Comment
Please, Sign In to add comment