Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- class Exception
- {
- private:
- string s;
- public:
- Exception(string mes) : s(mes) {}
- string Messahe()
- {
- return s;
- }
- };
- template<class T> class dList
- {
- protected:
- class Element
- {
- public:
- T content;
- Element* next;
- Element* prev;
- Element(Element* prev, T data, Element* next)
- {
- this->content = data;
- this->next = next;
- this->prev = prev;
- }
- };
- class ElementWithIndex
- {
- public:
- Element* element;
- int index;
- ElementWithIndex(Element* elem, int index)
- {
- this->element = elem;
- this->index = index;
- }
- T Content()
- {
- return element->content;
- }
- inline Element* Next()
- {
- return element->next;
- }
- inline Element* Prev()
- {
- return element->prev;
- }
- inline void GoNext()
- {
- element = element->next;
- index++;
- }
- inline void GoPrev()
- {
- element = element->prev;
- index--;
- }
- };
- Element* head;
- Element* tail;
- ElementWithIndex* cur;
- int length;
- void offset_cur(int index)
- {
- if (!cur)
- {
- cur->element = head;
- cur->index = 0;
- }
- int lc = cur->index - index;
- lc = (lc < 0) ? -lc : lc;
- int lh = index;
- int lt = length - index;
- if (index == 0 || (lh <= lc && lh <= lt))
- {
- cur->element = head;
- cur->index = 0;
- }
- if (index == length - 1 || (lt <= lc && lt <= lh))
- {
- cur->element = tail;
- cur->index = length - 1;
- }
- while (cur->index != index)
- {
- if (cur->index > index)
- {
- cur->GoPrev();
- }
- else
- {
- cur->GoNext();
- }
- }
- }
- public:
- dList()
- {
- length = 0;
- head = 0;
- tail = 0;
- cur = new ElementWithIndex(0, 0);
- }
- void Add(T data)
- {
- if (length)
- {
- tail = (tail->next = new Element(tail, data, 0));
- length++;
- }
- else
- {
- cur->element = head = tail = new Element(0, data, 0);
- cur->index = 0;
- length++;
- }
- }
- int Length()
- {
- return length;
- }
- T* GetAll()
- {
- T* res = new T[length];
- Element* t = head;
- for (int i = 0; i < length; i++)
- {
- res[i] = t->content;
- t = t->next;
- }
- return res;
- }
- T Get(int index)
- {
- if (index < 0 || index >= length) throw new Exception("Index out of range");
- offset_cur(index);
- return cur->Content();
- }
- void Remove(int index)
- {
- if (index < 0 || index >= length) throw new Exception("Index out of range");
- offset_cur(index);
- if (!cur->Next() && !cur->Prev())
- {
- tail = head = 0;
- delete cur->element;
- cur->element = 0;
- length--;
- cur->index = 0;
- return;
- }
- if (!cur->Next())
- {
- tail = tail->prev;
- delete cur->element;
- cur->element = tail;
- length--;
- cur->index--;
- return;
- }
- if (!cur->Prev())
- {
- head = head->next;
- delete cur->element;
- cur->element = head;
- length--;
- cur->index++;
- return;
- }
- cur->Prev()->next = cur->Next();
- cur->Next()->prev = cur->Prev();
- Element* t = cur->element;
- cur->element = t->next;
- cur->index++;
- length--;
- delete t;
- }
- int FindObject(bool(*inspect)(T), int index = 0)
- {
- offset_cur(index);
- for (int i = index; i < length && cur; i++)
- {
- if (inspect(cur->Content())) return i;
- cur->GoNext();
- }
- return -1;
- }
- T* FindAll(bool(*inspect)(T), int& res_length)
- {
- offset_cur(0);
- int cnt = 0;
- for (int i = 0; i < length && cur; i++)
- {
- if (inspect(cur->Content())) cnt++;
- cur->GoNext();
- }
- res_length = cnt;
- if (!cnt) return 0;
- T* res = new T[cnt];
- offset_cur(0);
- for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
- {
- if (inspect(cur->Content()))
- {
- res[t] = cur->Content();
- t++;
- }
- cur->GoNext();
- }
- return res;
- }
- void ClearList()
- {
- for (int i = length - 1; i >= 0; i--) Remove(i);
- }
- ~dList()
- {
- ClearList();
- }
- };
- class InfoBaseClass
- {
- protected:
- string name;
- string info;
- void SetName(string val)
- {
- name = val;
- }
- void SetInfo(string val)
- {
- info = val;
- }
- public:
- InfoBaseClass(string name, string info) : name(name), info(info) {}
- string GetName()
- {
- return name;
- }
- string GetInfo()
- {
- return info;
- }
- };
- class Vacancy : public InfoBaseClass
- {
- public:
- Vacancy(string name, string specialty) : InfoBaseClass(name, specialty) {}
- };
- class Company : public InfoBaseClass, private dList<Vacancy*>
- {
- public:
- Company(string name) : InfoBaseClass(name, "Company"), dList<Vacancy*>() {}
- void AddVacancy(string name, string specialty)
- {
- this->Add(new Vacancy(name, specialty));
- }
- Vacancy** GetAllVacancy()
- {
- return this->GetAll();
- }
- Vacancy* GetVacancy(int index)
- {
- return this->Get(index);
- }
- void Remove(int index)
- {
- this->Remove(index);
- }
- Vacancy* FindVacancy_name(string name)
- {
- offset_cur(0);
- for (int i = 0; i < length && cur; i++)
- {
- if (cur->Content()->GetName() == name) return cur->Content();
- cur->GoNext();
- }
- return 0;
- }
- Vacancy* FindVacancy_specialty(string info)
- {
- offset_cur(0);
- for (int i = 0; i < length && cur; i++)
- {
- if (cur->Content()->GetInfo() == info) return cur->Content();
- cur->GoNext();
- }
- return 0;
- }
- Vacancy** FindAllVacancy_name(string name, int& res_length)
- {
- offset_cur(0);
- int cnt = 0;
- for (int i = 0; i < length && cur; i++)
- {
- if (cur->Content()->GetName() == name) cnt++;
- cur->GoNext();
- }
- res_length = cnt;
- if (!cnt) return 0;
- Vacancy** res = new Vacancy * [cnt];
- offset_cur(0);
- for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
- {
- if (cur->Content()->GetName() == name)
- {
- res[t] = cur->Content();
- t++;
- }
- cur->GoNext();
- }
- return res;
- }
- Vacancy** FindAllVacancy_specialty(string info, int& res_length)
- {
- offset_cur(0);
- int cnt = 0;
- for (int i = 0; i < length && cur; i++)
- {
- if (cur->Content()->GetInfo() == info) cnt++;
- cur->GoNext();
- }
- res_length = cnt;
- if (!cnt) return 0;
- Vacancy** res = new Vacancy * [cnt];
- offset_cur(0);
- for (int i = 0, t = 0; i < length && cur && t < cnt; i++)
- {
- if (cur->Content()->GetInfo() == info)
- {
- res[t] = cur->Content();
- t++;
- }
- cur->GoNext();
- }
- return res;
- }
- void Clear()
- {
- this->ClearList();
- }
- int GetVacancyCount()
- {
- return length;
- }
- };
- int main()
- {
- //getc(stdin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement