Advertisement
GieeF

Graf - Lista sąsiedztwa po mojemu v2

Jan 13th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class AdjNode {
  6. private:
  7.     int nodeNo;
  8.     AdjNode* next;
  9. public:
  10.     AdjNode();
  11.     AdjNode(int u, AdjNode* n);
  12.     int GetNode();
  13.     AdjNode* GetNext();
  14.     void insertAdjNode(int u);
  15.     void deleteAdjNode(int u);
  16.     friend ostream& operator<<(ostream& out, AdjNode* node);
  17. };
  18.  
  19. AdjNode::AdjNode() {
  20.     nodeNo = -1;
  21.     next = NULL;
  22. }
  23.  
  24. AdjNode::AdjNode(int u, AdjNode* n) {
  25.     nodeNo = u;
  26.     next = n;
  27. }
  28.  
  29. int AdjNode::GetNode() {
  30.     return nodeNo;
  31. }
  32.  
  33. AdjNode* AdjNode::GetNext() {
  34.     return next;
  35. }
  36.  
  37. void AdjNode::insertAdjNode(int u) {
  38.    
  39.     if (this->nodeNo == u)
  40.         return;
  41.  
  42.     AdjNode* temp = this->next;
  43.     AdjNode* prevTemp = NULL;
  44.  
  45.     if (temp == NULL) {
  46.         temp = new AdjNode(u, NULL);
  47.         this->next = temp;
  48.         return;
  49.     }
  50.     if (temp->next == NULL) {
  51.         AdjNode* newNode = new AdjNode(u, NULL);
  52.         temp->next = newNode;
  53.         return;
  54.     }
  55.     while (temp->next->nodeNo <= u) {
  56.         if (temp->next->nodeNo == u) {
  57.             return;
  58.         }
  59.         if (temp->next == NULL) {
  60.             AdjNode* newNode = new AdjNode(u, NULL);
  61.             temp->next = newNode;
  62.             return;
  63.         }
  64.         prevTemp = temp;
  65.         temp = temp->next;
  66.         if (temp->next == NULL) {
  67.             break;
  68.         }
  69.     }
  70.     AdjNode* newNode = new AdjNode(u, temp->next);
  71.     temp->next = newNode;
  72. }
  73.  
  74. void AdjNode::deleteAdjNode(int u) {
  75.     if (this->nodeNo == u)
  76.         return;
  77.  
  78.     AdjNode* temp = this->next;
  79.     AdjNode* prevTemp = NULL;
  80.  
  81.     if (temp == NULL)
  82.         return;
  83.  
  84.     while (temp->nodeNo != u) {
  85.         prevTemp = temp;
  86.         temp = temp->next;
  87.         if (temp == NULL)
  88.             return;
  89.     }
  90.     if(prevTemp != NULL)
  91.         prevTemp->next = temp->next;
  92.     this->next = temp->next;
  93.    
  94.     delete temp;
  95. }
  96.  
  97. ostream& operator<<(ostream& out, AdjNode* node) {
  98.     if (node->GetNext() == NULL)
  99.         return out;
  100.  
  101.     AdjNode* temp = node->GetNext();
  102.     out << node->GetNode() << ": ";
  103.     bool lolz1337 = true;
  104.     while (temp != NULL) {
  105.         if (!lolz1337)
  106.             out << ", ";
  107.         out << temp->GetNode();
  108.         temp = temp->GetNext();
  109.         lolz1337 = false;
  110.     }
  111.     return out;
  112. }
  113.  
  114. class AdjList {
  115. private:
  116.     AdjNode** nodes;
  117.     int amountOfNodes;
  118. public:
  119.     AdjList();
  120.     AdjList(int c);
  121.     void insertAdjNode(int primeNode, int secNode);
  122.     friend ostream& operator<<(ostream& out, AdjList* graph);
  123. };
  124.  
  125. AdjList::AdjList() {
  126.     nodes = NULL;
  127.     amountOfNodes = 0;
  128. }
  129.  
  130. AdjList::AdjList(int c) {
  131.     nodes = new AdjNode*[c];
  132.     amountOfNodes = c;
  133.     for (int i = 0; i < amountOfNodes; i++) {
  134.         nodes[i] = new AdjNode(i, NULL);
  135.     }
  136.    
  137. }
  138.  
  139. void AdjList::insertAdjNode(int primeNode, int secNode) {
  140.     if (primeNode >= amountOfNodes)
  141.         return;
  142.     nodes[primeNode]->insertAdjNode(secNode);
  143. }
  144.  
  145. ostream& operator<<(ostream& out, AdjList* graph) {
  146.     if (graph->amountOfNodes == 0)
  147.         return out;
  148.  
  149.     for (int i = 0; i < graph->amountOfNodes; i++) {
  150.         out << graph->nodes[i] << endl;
  151.     }
  152.     return out;
  153. }
  154.  
  155. int main() {
  156.    
  157.     AdjList* graf = new AdjList(5);
  158.  
  159.     graf->insertAdjNode(0, 1);
  160.     graf->insertAdjNode(0, 3);
  161.     graf->insertAdjNode(1, 3);
  162.     graf->insertAdjNode(1, 2);
  163.     graf->insertAdjNode(2, 4);
  164.     graf->insertAdjNode(2, 0);
  165.     graf->insertAdjNode(3, 2);
  166.     graf->insertAdjNode(3, 4);
  167.     graf->insertAdjNode(4, 0);
  168.  
  169.     cout << graf;
  170.    
  171.    
  172.     /*
  173.     AdjNode* node1 = new AdjNode(1, NULL);
  174.     AdjNode* node2 = new AdjNode(2, NULL);
  175.     AdjNode* node3 = new AdjNode(3, NULL);
  176.     AdjNode* node4 = new AdjNode(4, NULL);
  177.     AdjNode* node5 = new AdjNode(5, NULL);
  178.  
  179.     node1->insertAdjNode(2);
  180.     node1->insertAdjNode(5);
  181.     node2->insertAdjNode(4);
  182.     node2->insertAdjNode(3);
  183.     node3->insertAdjNode(5);
  184.     node3->insertAdjNode(1);
  185.     node4->insertAdjNode(3);
  186.     node4->insertAdjNode(5);
  187.     node5->insertAdjNode(1);
  188.  
  189.  
  190.     cout << node1 << endl;
  191.     cout << node2 << endl;
  192.     cout << node3 << endl;
  193.     cout << node4 << endl;
  194.     cout << node5 << endl;
  195.     */
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement