123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- #include "List.h"
- #include <iostream>
- #include <fstream>
- List::List() :m_size(0)
- {
- /*ñôîðìèðîâàòü Head, Tail è m_size*/
- Head.pNext = &Tail;
- Tail.pPrev = &Head;
- }
- List::List(const List& other) :m_size(other.m_size)
- {
- Head.pNext = &Tail;
- Tail.pPrev = &Head;
- Node* pThis = Tail.pPrev;
- Node* pOther = other.Head.pNext;
- for (size_t i = 0; i < m_size; i++)
- {
- pThis = new Node(pThis, &pOther->m_Data);
- pOther = pOther->pNext;
- }
- }
- List::List(List&& other) :m_size(other.m_size)
- {
- if (m_size)
- {
- Head.pNext = other.Head.pNext;
- Tail.pPrev = other.Tail.pPrev;
- Head.pNext->pPrev = &Head;
- Tail.pPrev->pNext = &Tail;
- other.Head.pNext = &other.Tail;
- other.Tail.pPrev = &other.Head;
- other.m_size = 0;
- }
- else
- {
- Head.pNext = &Tail;
- Tail.pPrev = &Head;
- }
- }
- List::~List()
- {
- this->Remove_list();
- }
- void List::Ad_to(TO to, const Circle& circle)
- {
- Node* Prev = nullptr;
- switch (to)
- {
- case List::TO::to_head: Prev = &Head; break;
- case List::TO::to_tail: Prev = Tail.pPrev; break;
- //default: std::cout<<"Err" <<std::endl; break;
- }
- if (Prev)
- {
- new Node(Prev, &circle);
- m_size++;
- }
- }
- bool List::RemoveOne(const Circle& desired)
- {
- Node* p = Head.pNext;
- while (p != &Tail)
- {
- if (desired == p->m_Data)//find data
- {
- //if match - delete data, return true
- m_size--;
- delete p;
- return true;
- }
- //delete data, return true
- //else return false
- p = p->pNext;
- }
- return false;
- }
- int List::Remove_all_match(const Circle& desired)
- {
- static int deleted = 0;
- if (RemoveOne(desired))
- {
- deleted++;
- Remove_all_match(desired);
- }
- return deleted;
- }
- void List::Remove_list()
- {
- while (Head.pNext != &Tail)
- {
- delete Head.pNext;
- }
- m_size = 0;
- }
- void List::Swap(Node* replaceable, Node* movable)
- {
- if (replaceable == movable)return;
- if (replaceable->pNext == movable || replaceable->pPrev == movable)
- {
- Node* binding_tmp = replaceable->pPrev->pNext;
- replaceable->pPrev->pNext = movable->pNext->pPrev;
- movable->pNext->pPrev = binding_tmp;
-
- if (replaceable->pPrev == movable)
- {
- movable->pNext = replaceable->pNext;
- replaceable->pNext = movable;
- replaceable->pPrev = movable->pPrev;
- movable->pPrev = replaceable;
- return;
- }
- replaceable->pNext = movable->pNext;
- movable->pNext = replaceable;
-
- movable->pPrev = replaceable->pPrev;
- replaceable->pPrev = movable;
- return;
- }
-
- if (replaceable != &Tail &&
- replaceable != &Head &&
- movable != &Tail &&
- movable != &Head)
- {
- Node* tmpNext = replaceable->pNext;
- Node* tmpPrev = replaceable->pPrev;
- Node* binding_pNext = replaceable->pNext->pPrev;
- Node* binding_pPrev = replaceable->pPrev->pNext;
-
- replaceable->pNext->pPrev = movable->pNext->pPrev;
- replaceable->pPrev->pNext = movable->pPrev->pNext;
- movable->pNext->pPrev = binding_pNext;
- movable->pPrev->pNext = binding_pPrev;
- replaceable->pNext = movable->pNext;
- replaceable->pPrev = movable->pPrev;
- movable->pNext = tmpNext;
- movable->pPrev = tmpPrev;
- }
- }
- void List::Sort_list()
- {
- if (m_size <= 1) { return; }
- /*Node* left_node = Head.pNext;*/
- Node* min_node = Head.pNext;
- //åñëè ïëîùàäü âû÷èñëÿòü íå íàäî òî ìîæíî ñðàâíèâàòü ðàäèóñû
- /*double min = min_node->m_Data.Get_area();*/
-
- for (Node* left_node = Head.pNext; left_node->pNext != &Tail; left_node = left_node->pNext)
- {
- double min = left_node->m_Data.Get_area();
- bool fl = false;//ôëàæîê - áûëà ëè õîòÿ áû îäíà ïåðåñòàíîâêà
- for (Node* current = left_node->pNext; current != &Tail; current = current->pNext)
- {
- double cur = current->m_Data.Get_area();
- if (min > cur)
- {
- min = cur;
- min_node = current;
- fl = true;
- }
- }
- if (fl) { Swap(left_node, min_node); left_node = min_node; }
- /*else { break; }*/
- }
- }
- List& List::operator=(const List& other)
- {
- const Node* pOther_Head = &other.Head;
- Node* pOther = pOther_Head->pNext;
- Node* pThis = &Head;
- if (m_size == other.m_size || m_size > other.m_size)
- {
- while (pOther != &other.Tail)
- {
- pThis = pThis->pNext;
- pThis->m_Data = pOther->m_Data;
- pOther = pOther->pNext;
- }
- if (m_size > other.m_size)
- {
- pThis = pThis->pNext;
- while (pThis != &Tail)
- {
- Node* removable = pThis;
- pThis = pThis->pNext;
- delete removable;
- }
- m_size = other.m_size;
- }
- }
- if (m_size < other.m_size)
- {
- while (pThis->pNext != &Tail)
- {
- pThis = pThis->pNext;
- pThis->m_Data = pOther->m_Data;
- pOther = pOther->pNext;
- }
- while (pOther != &other.Tail)
- {
- pThis = new Node(pThis, &pOther->m_Data);
- pOther = pOther->pNext;
- }
- m_size = other.m_size;
- }
- return *this;
- }
- List& List::operator=(List&& other)
- {
- if (other.m_size)
- {
- if (m_size)
- {this->Remove_list();}
- Head.pNext = other.Head.pNext;
- Tail.pPrev = other.Tail.pPrev;
- Head.pNext->pPrev = &Head;
- Tail.pPrev->pNext = &Tail;
- m_size = other.m_size;
- other.Head.pNext = &other.Tail;
- other.Tail.pPrev = &other.Head;
- other.m_size = 0;
- }
- else
- {
- if (m_size != other.m_size)
- {
- this->Remove_list();
- }
- }
- return *this;
- }
- std::ostream& operator<<(std::ostream& os, const List& list)
- {
- os << "Size_of_list: " << list.m_size<<"\n";
- //Head.pNext - & 1 element
- //1 el.pNext - & Next el
- //if pNext ==ulptr - we are in Tait
- if (!list.m_size)
- {
- os << "Empty list";
- }
- else
- {
- Node* p = list.Head.pNext;
- while (p != &list.Tail)
- {
- os << *p << std::endl;// list.Head.pNext;
- p = p->pNext;
- }
- }
- os << "\n";
- return os;
- }
- List& operator>>(std::ifstream& file, List& list)
- {
- size_t tmp_size;
- Circle tmp_circ; //âðåìåííûé îáúåêò êðóæêà
- char buff[20];
- //file.getline(buff, 15) >> list.m_size; //why it is don't work?
- file >> buff >> tmp_size;
- for (size_t i = 0; i < tmp_size; i++)
- {
- file >> tmp_circ;
- list.Ad_to(List::TO::to_tail,tmp_circ);
- }
- return list;
- }
|