Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include<Windows.h>
  4. using namespace std;
  5.  
  6. class List;
  7. class listElement {
  8. public:
  9.         listElement * next; // следующий элемент в цепи
  10.         listElement * prev; // предыдущий
  11.         listElement * link; // ссылка на элемент в цепи узлов
  12.         int val;
  13.         List * trList; // на схеме - ссылки вниз
  14.         listElement * tr; // на схеме - ссылки вверх
  15. };
  16.  
  17. class List {
  18. public:
  19.     listElement * head;
  20.     listElement * tail;
  21.     int count;
  22.  
  23.     List() {
  24.         this->count=0;
  25.         this->head = NULL;
  26.         this->head = NULL;
  27.     }
  28.  
  29.  
  30.     ~List() {
  31.  
  32.     }
  33. /*  ~List() {
  34.         cout<<"del";
  35.         listElement * link = this->head;
  36.         while (link) {
  37.             if(link->trList)
  38.                 delete [] link->trList;
  39.             listElement * temp = link->next;
  40.             delete [] link;
  41.             link = temp;
  42.         }
  43.     }
  44. */
  45.     void add(int val=1) {
  46.         if(this->head==NULL) {
  47.             this->head = new listElement;
  48.             this->head->val = val;
  49.             this->head->next=NULL;
  50.             this->head->prev=NULL;
  51.             this->head->link=NULL;
  52.             this->tail = this->head;
  53.         }
  54.         else {
  55.             this->tail->next=new listElement;
  56.             this->tail->next->val = val;
  57.             this->tail->next->next=NULL;
  58.             this->tail->next->link=NULL;
  59.             this->tail->next->prev = this->tail;
  60.             this->tail = this->tail->next;
  61.         }
  62.         this->count++;
  63.     }
  64.  
  65.  
  66.  
  67.     listElement * getElementN(int num = 0) {
  68.         if(num>this->count) return NULL;
  69.         if (num==0)
  70.             return this->head;
  71.         if(num==this->count)
  72.             return this->tail;
  73.  
  74.         listElement * link = head;
  75.         for (int i=1; i<=num; i++)
  76.             link=link->next;
  77.         return link;
  78.     }
  79.     listElement * getElement (int val) {
  80.         listElement * link = this->head;
  81.         do {
  82.             if(link->val==val)
  83.                 return link;
  84.             link=link->next;
  85.         } while(link);
  86.         return link;
  87.     }
  88.  
  89.  
  90.     int size () {
  91.         return this->count;
  92.     }
  93.  
  94.     listElement * last() {
  95.         return this->tail;
  96.     }
  97. };
  98.  
  99. int main() {
  100.     SetConsoleCP(1251);
  101.     SetConsoleOutputCP(1251);
  102.  
  103.     fstream f("graf.txt", fstream::in);
  104.     if(!f.is_open()) {
  105.         cout<<"Error opening file"<<endl;
  106.         return 1;
  107.     }
  108.     int amount;
  109.     f>>amount;
  110.  
  111.     List * main = new List;
  112.     for (int i=0; i<=amount; i++) {
  113.         main->add(i);
  114.         main->last()->trList = new List;
  115.     }
  116.  
  117.     while(!f.eof()) {
  118.         int a, b;
  119.         f>>a;
  120.         f>>b;
  121.         main->getElement(a)->trList->add();
  122.         main->getElement(a)->trList->last()->tr = main->getElement(b);
  123.     }
  124.     //cout<<main->getElement(7)->trList->last()->tr->val;
  125.  
  126.  
  127.     List * cur; // СПИСОК ТЕКУЩИХ СТАРТОВЫЙХ ВЕРШИН
  128.     List * marked = new List; // список обойдённых вершин
  129.     List * newList = new List; // список смежных со стартовыми вершинами
  130.  
  131.  
  132.     cur = new List;
  133.     cur->add(1);
  134.     marked->add(1);
  135.     int count = 0;
  136.     bool flag = true;
  137.  
  138.  
  139.     while(flag) {
  140.         count++;
  141.         newList = new List;
  142.         listElement * curEl = cur->head; // от стартовой вершины
  143.         while(curEl) {
  144.             listElement * curTr = main->getElement(curEl->val)->trList->head;
  145.             while(curTr) {
  146.                 if(!marked->getElement(curTr->tr->val)) {
  147.                     marked->add(curTr->tr->val);
  148.                     newList->add(curTr->tr->val);
  149.                     cout<<curTr->tr->val<<" ";
  150.                     if(curTr->tr->val==amount)
  151.                         flag = false;
  152.                 }
  153.                 curTr=curTr->next;
  154.             }
  155.             curEl=curEl->next;
  156.         }
  157.         cout<<endl;
  158.         delete cur;
  159.         cur = newList;
  160.     }
  161.  
  162.     /*
  163.     while(flag) {
  164.         count++;
  165.         newList = new List;
  166.         listElement * curEl = cur->head; // от стартовой вершины
  167.         while(curEl) {
  168.             listElement * curTr = curEl->trList->head;
  169.             while(curTr) {
  170.                 if(!marked->getElement(curTr->tr->val)) {
  171.                     marked->add(curTr->tr->val);
  172.                     newList->add(curTr->tr->val);
  173.                     cout<<curTr->tr->val<<" ";
  174.                     if(curTr->tr->val==amount)
  175.                         flag = false;
  176.                 }
  177.                 curTr=curTr->next;
  178.             }
  179.             curEl=curEl->next;
  180.         }
  181.         cout<<endl;
  182.         delete [] cur;
  183.         cur = newList;
  184.     }
  185.     */
  186.  
  187.    
  188.         cout<<"count: "<<count<<endl;
  189.  
  190.    
  191.  
  192.  
  193.     return 0;
  194. }
Add Comment
Please, Sign In to add comment