Advertisement
Mikhail-Podbolotov

Untitled

May 15th, 2024
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include "library.h"
  2.  
  3. List::List()
  4. {
  5.     Start = End = nullptr;
  6. }
  7.  
  8. List::~List()
  9. {
  10.     Point* prom;
  11.     Point* t = Start;
  12.     while (t != nullptr) {
  13.         prom = t;
  14.         t = t->NextPoint;
  15.         delete prom;
  16.     }
  17. }
  18.  
  19. Point* List::getStart()
  20. {
  21.     return Start;
  22. }
  23.  
  24. int List::GetLast()
  25. {
  26.     return End->number;
  27. }
  28.  
  29. void List::addPoint()
  30. {
  31.     Point* temp = new Point;
  32.     if (Start == nullptr) {
  33.         temp->number = 1;
  34.         Start = End = temp;
  35.     }
  36.     else {
  37.         temp->number = End->number + 1;
  38.         End->NextPoint = temp;
  39.         End = temp;
  40.     }
  41. }
  42.  
  43. void List::addAdjPoint(int x)
  44. {
  45.     Point* tempPoint = End;
  46.     AdjPoint* tempAdjPoint = new AdjPoint;
  47.     tempAdjPoint->value = x;
  48.     if (tempPoint->First == nullptr)
  49.         tempPoint->First = tempPoint->Last = tempAdjPoint;
  50.     else {
  51.         tempPoint->Last->next = tempAdjPoint;
  52.         tempPoint->Last = tempAdjPoint;
  53.     }
  54. }
  55.  
  56. void List::Print(std::ofstream& file)
  57. {
  58.     Point* CurPoint = Start;
  59.     while (CurPoint != nullptr) {
  60.         file << CurPoint->number << ": ";
  61.         AdjPoint* CurAdjPoint = CurPoint->First;
  62.         while (CurAdjPoint != nullptr) {
  63.             file << CurAdjPoint->value << " ";
  64.             CurAdjPoint = CurAdjPoint->next;
  65.         }
  66.         file << endl;
  67.         CurPoint = CurPoint->NextPoint;
  68.     }
  69. }
  70.  
  71. void List::Read(std::ifstream& file)
  72. {
  73.     string line;
  74.     while (!file.eof()) {
  75.         getline(file, line);
  76.         addPoint();
  77.         int tempvalue = 0;
  78.         for (char c : line) {
  79.             if (isdigit(c)) {
  80.                 tempvalue += tempvalue * 10 + ((int)c - (int)'0');
  81.             }
  82.             else {
  83.                 if (tempvalue > 0) {
  84.                     addAdjPoint(tempvalue);
  85.                 }
  86.                 tempvalue = 0;
  87.             }
  88.         }
  89.         if (tempvalue > 0) {
  90.             addAdjPoint(tempvalue);
  91.         }
  92.     }
  93. }
  94.  
  95. EdgeList::EdgeList()
  96. {
  97.     Start = End = nullptr;
  98. }
  99.  
  100. EdgeList::~EdgeList()
  101. {
  102.     Edge* prom;
  103.     Edge* t = Start;
  104.     while (t != nullptr) {
  105.         prom = t;
  106.         t = t->next;
  107.         delete prom;
  108.     }
  109. }
  110.  
  111. bool EdgeList::find(int x1, int x2)
  112. {
  113.     Edge* t = Start;
  114.     while (t != 0) {
  115.         if ((t->x1 == x1 and t->x2 == x2) or (t->x1==x2 and t->x2==x1)) {
  116.             return true;
  117.         }
  118.         t = t->next;
  119.     }
  120.     return false;
  121. }
  122.  
  123. void EdgeList::appendEdge(int x1, int x2)
  124. {
  125.     if (!find(x1, x2)) {
  126.         Edge* temp = new Edge;
  127.         temp->x1 = x1;
  128.         temp->x2 = x2;
  129.         if (Start == nullptr) {
  130.             Start = End = temp;
  131.         }
  132.         else {
  133.             End->next = temp;
  134.             End = temp;
  135.         }
  136.     }
  137. }
  138.  
  139. void EdgeList::readFromList(List& A)
  140. {
  141.     Point* CurPoint = A.getStart();
  142.     while (CurPoint != nullptr) {
  143.         AdjPoint* CurAdjPoint = CurPoint->First;
  144.         while (CurAdjPoint != nullptr){
  145.             appendEdge(CurPoint->number, CurAdjPoint->value);
  146.             CurAdjPoint = CurAdjPoint->next;
  147.         }
  148.         CurPoint = CurPoint->NextPoint;
  149.     }
  150. }
  151.  
  152. void EdgeList::print(std::ofstream& file)
  153. {
  154.     Edge* t = Start;
  155.     while (t != nullptr) {
  156.         file << t->x1 << "-" << t->x2 << endl;
  157.         t = t->next;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement