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