Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "library.h"
- List::List()
- {
- Start = End = nullptr;
- }
- List::~List()
- {
- Point* prom;
- Point* t = Start;
- while (t != nullptr) {
- prom = t;
- t = t->NextPoint;
- delete prom;
- }
- }
- Point* List::getStart()
- {
- return Start;
- }
- int List::GetLast()
- {
- return End->number;
- }
- void List::addPoint()
- {
- Point* temp = new Point;
- if (Start == nullptr) {
- temp->number = 1;
- Start = End = temp;
- }
- else {
- temp->number = End->number + 1;
- End->NextPoint = temp;
- End = temp;
- }
- }
- void List::addAdjPoint(int x)
- {
- Point* tempPoint = End;
- AdjPoint* tempAdjPoint = new AdjPoint;
- tempAdjPoint->value = x;
- if (tempPoint->First == nullptr)
- tempPoint->First = tempPoint->Last = tempAdjPoint;
- else {
- tempPoint->Last->next = tempAdjPoint;
- tempPoint->Last = tempAdjPoint;
- }
- }
- void List::Print(std::ofstream& file)
- {
- Point* CurPoint = Start;
- while (CurPoint != nullptr) {
- file << CurPoint->number << ": ";
- AdjPoint* CurAdjPoint = CurPoint->First;
- while (CurAdjPoint != nullptr) {
- file << CurAdjPoint->value << " ";
- CurAdjPoint = CurAdjPoint->next;
- }
- file << endl;
- CurPoint = CurPoint->NextPoint;
- }
- }
- void List::Read(std::ifstream& file)
- {
- string line;
- while (!file.eof()) {
- getline(file, line);
- addPoint();
- int tempvalue = 0;
- for (char c : line) {
- if (isdigit(c)) {
- tempvalue += tempvalue * 10 + ((int)c - (int)'0');
- }
- else {
- if (tempvalue > 0) {
- addAdjPoint(tempvalue);
- }
- tempvalue = 0;
- }
- }
- if (tempvalue > 0) {
- addAdjPoint(tempvalue);
- }
- }
- }
- EdgeList::EdgeList()
- {
- Start = End = nullptr;
- }
- EdgeList::~EdgeList()
- {
- Edge* prom;
- Edge* t = Start;
- while (t != nullptr) {
- prom = t;
- t = t->next;
- delete prom;
- }
- }
- bool EdgeList::find(int x1, int x2)
- {
- Edge* t = Start;
- while (t != 0) {
- if ((t->x1 == x1 and t->x2 == x2) or (t->x1==x2 and t->x2==x1)) {
- return true;
- }
- t = t->next;
- }
- return false;
- }
- void EdgeList::appendEdge(int x1, int x2)
- {
- if (!find(x1, x2)) {
- Edge* temp = new Edge;
- temp->x1 = x1;
- temp->x2 = x2;
- if (Start == nullptr) {
- Start = End = temp;
- }
- else {
- End->next = temp;
- End = temp;
- }
- }
- }
- void EdgeList::readFromList(List& A)
- {
- Point* CurPoint = A.getStart();
- while (CurPoint != nullptr) {
- AdjPoint* CurAdjPoint = CurPoint->First;
- while (CurAdjPoint != nullptr){
- appendEdge(CurPoint->number, CurAdjPoint->value);
- CurAdjPoint = CurAdjPoint->next;
- }
- CurPoint = CurPoint->NextPoint;
- }
- }
- void EdgeList::print(std::ofstream& file)
- {
- Edge* t = Start;
- while (t != nullptr) {
- file << t->x1 << "-" << t->x2 << endl;
- t = t->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement