Advertisement
ithoran

Retko Posednuta Matrica

Mar 26th, 2016
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. // .h
  2. #pragma once
  3. #include <iostream>
  4. #include "Cvor.h"
  5. using namespace std;
  6.  
  7. template <class T>
  8. class RetkoPosednutaMat {
  9.     Cvor<T>** kolone;
  10.     Cvor<T>** vrste;
  11.     int i;
  12.     int j;
  13.  
  14. public:
  15.  
  16.     RetkoPosednutaMat(int x, int y) {
  17.         i = x;
  18.         j = y;
  19.         kolone = new Cvor<T>*[j];
  20.         for (int k = 0; k < j; k++) {
  21.             kolone[k] = 0;
  22.         }
  23.         vrste = new Cvor<T>*[i];
  24.         for (int k = 0; k < i; k++) {
  25.             vrste[k] = 0;
  26.         }
  27.     }
  28.  
  29.     ~RetkoPosednutaMat() {
  30.         Cvor<T>* tmp;
  31.         Cvor<T>* pre;
  32.         for (int k = 0; k < j; k++) {
  33.             tmp = kolone[k];
  34.             pre = tmp;
  35.             while (tmp != 0) {
  36.                 pre = tmp;
  37.                 tmp = tmp->dole;
  38.                 delete pre;
  39.             }
  40.         }
  41.     }
  42.  
  43.     void setAt(T el, int x, int y) {
  44.         Cvor<T>* tmp;
  45.         Cvor<T>* preX;
  46.         Cvor<T>* preY;
  47.         Cvor<T>* c = new Cvor<T>;
  48.         c->info = el;
  49.         c->i = x - 1;
  50.         c->j = y - 1;
  51.         if (x > i || y > j) {
  52.             cout << "Preskocili ste dimenzije matrice!\n";
  53.             return;
  54.         }
  55.         tmp = kolone[y - 1];
  56.         preY = tmp;
  57.         while (tmp != 0 && tmp->i < x) {    // namesta cvor iznad (trazi kog je reda, posto je postavljen na svoju kolonu)
  58.             preY = tmp;
  59.             tmp = tmp->dole;
  60.         }
  61.         if (preY != 0 && preY->i == x - 1) {                // u slucaju da taj cvor vec postoji
  62.             preY->info = el;
  63.             return;
  64.         }
  65.         if (preY != 0 && preY->i < x - 1) {     // ne postoji cvor sa tim elementima i nalazi se posle nekog cvora
  66.             c->dole = preY->dole;            
  67.             preY->dole = c;
  68.         }
  69.         if (preY != 0 && preY->i > x - 1) {     // nalazi se prvi u koloni
  70.             c->dole = preY;
  71.             preY = c;
  72.         }
  73.         if (preY == 0) {
  74.             preY = c;
  75.         }
  76.  
  77.         tmp = vrste[x - 1];                 // namesta cvor sa leve strane
  78.         preX = tmp;
  79.         while (tmp != 0 && tmp->j > y) {
  80.             preX = tmp;
  81.             tmp = tmp->desno;
  82.         }
  83.         if (preX != 0 && preX->j < y - 1) {   // nalazi se posle nekog cvora
  84.             c->desno = preX->desno;      
  85.             preX->desno = c;
  86.         }
  87.         if (preX != 0 && preX->j > y - 1) {   // nalazi se prvi u vrsti
  88.             c->desno = preX;
  89.             preX = c;
  90.         }
  91.         if (preX == 0) {
  92.             preX = c;
  93.         }
  94.  
  95.         if (kolone[y - 1] == 0 || preY->i == x - 1)   // proverava da li postoji je do sada postajala ta vrsta/kolona
  96.             kolone[y - 1] = preY;                     // i slucaj kada se cvor stavlja na prvo mesto u vrsti/koloni
  97.         if (vrste[x - 1] == 0 || preX->j == y - 1)
  98.             vrste[x - 1] = preX;
  99.     }
  100.  
  101.     void stampaj() {
  102.         for (int k = 0; k < i; k++) {
  103.             Cvor<T>* tmp = vrste[k];
  104.             if (tmp == 0) {
  105.                 for (int z = 0; z < j; z++)
  106.                     cout << 0 << " ";
  107.                 cout << endl;
  108.                 continue;
  109.             }
  110.             int z = 0;
  111.             while (tmp != 0 && tmp->j < j) {
  112.                 for (z; z < tmp->j; z++)
  113.                     cout << 0 << " ";
  114.                 cout << tmp->info << " ";
  115.                 tmp = tmp->desno;
  116.                 z++;
  117.             }
  118.             for (z; z < j; z++) {
  119.                 cout << 0 << " ";
  120.             }
  121.             cout << endl;
  122.         }
  123.     }
  124.  
  125.     T getT(int x, int y) {
  126.         Cvor<T>* tmp = kolone[y - 1];
  127.         Cvor<T>* pre = tmp;
  128.         if (kolone[y - 1] == 0) {
  129.             cout << "Taj element ne postoji!\n";
  130.             return 0;
  131.         }
  132.         while (tmp != 0 && tmp->i < x) {
  133.             pre = tmp;
  134.             tmp = tmp->dole;
  135.         }
  136.         if (pre->i == x - 1)
  137.             return pre->info;
  138.         else {
  139.             cout << "Taj element ne postoji!\n";
  140.             return 0;
  141.         }
  142.     }
  143.  
  144. };
  145.  
  146. // cvor.h
  147. #pragma once
  148. #include <iostream>
  149. using namespace std;
  150. template<class T>
  151. class Cvor {
  152. public:
  153.     int i;
  154.     int j;
  155.     T info;
  156.     Cvor* desno;
  157.     Cvor* dole;
  158.  
  159.     Cvor() {
  160.         i = 0;
  161.         j = 0;
  162.         desno = 0;
  163.         dole = 0;
  164.         info = 0;
  165.     }
  166. };
  167.  
  168. // main
  169. #pragma once
  170. #include "RetkoPosednutaMat.h"
  171.  
  172. void main() {
  173.     RetkoPosednutaMat<int> a(5, 5);
  174.     a.setAt(1, 1, 2);
  175.     a.setAt(2, 1, 5);
  176.     a.setAt(4, 4, 1);
  177.     a.setAt(5, 4, 2);
  178.     a.setAt(6, 5, 5);
  179.     a.setAt(33, 2, 2);
  180.     a.setAt(35, 3, 3);
  181.     a.setAt(30, 3, 1);
  182.     a.setAt(30, 3, 2);
  183.     a.setAt(11, 3, 2);
  184.     a.setAt(66, 1, 4);
  185.     a.setAt(44, 5, 2);
  186.     cout << a.getT(3, 2) << endl;
  187.     cout << a.getT(5, 2) << endl;
  188.     cout << a.getT(1, 5) << endl;
  189.     cout << a.getT(5, 5) << endl;
  190.     a.stampaj();
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement