Advertisement
lisachu

Untitled

Feb 10th, 2022
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct wierzcholek
  6. {
  7.     int nr;
  8.     wierzcholek *nast;
  9. };
  10.  
  11. class graf
  12. {
  13.     int n;
  14.     wierzcholek *tab[100];
  15. public:
  16.     graf();
  17.     int LiczbaWierzcholkow();
  18.     int LiczbaKrawedzi();
  19.     int MaxStopien();
  20.     int MinStopien();
  21.     int getN();
  22.     void dodajPol(int wierzA, int wierzB);
  23.     friend istream &operator>>(istream& we, graf& G);
  24.     friend ostream& operator<<(ostream& wy, const graf& G);
  25. };
  26.  
  27. graf::graf()
  28. {
  29.     n=0;
  30.     for(int u=0; u<100; u++)
  31.     {
  32.         tab[u]=NULL;
  33.     }
  34. }
  35.  
  36. int graf::getN(){
  37.     return n;
  38. }
  39.  
  40. void graf::dodajPol(int wierzA, int wierzB){
  41.     wierzcholek* pom, *pom2;
  42.     pom = tab[wierzA];
  43.     pom2 = new wierzcholek;
  44.     pom2->nr = wierzB;
  45.     while(pom->nast != NULL) pom = pom->nast;
  46.     pom->nast = pom2;
  47.    
  48.     pom = tab[wierzB];
  49.     pom2 = new wierzcholek;
  50.     pom2->nr = wierzA;
  51.     while(pom->nast != NULL) pom = pom->nast;
  52.     pom->nast = pom2;
  53. }
  54.  
  55. ostream& operator<<(ostream &wy, const graf& G)
  56. {
  57.     wierzcholek *pom;
  58.     for(int u=0;u<G.n;u++)
  59.     {
  60.         pom = G.tab[u]->nast;
  61.         wy<<u<<": ";
  62.         while(pom != NULL){
  63.             wy<<pom->nr<<" ";
  64.             pom = pom->nast;
  65.         }
  66.         wy<<endl;
  67.     }
  68.     return wy;
  69. }
  70.  
  71. istream& operator>>(istream& we, graf& G)
  72. {
  73.     we>>G.n;
  74.     wierzcholek* pom;
  75.     for(int i=0 ; i<G.n ; i++){
  76.         pom = new wierzcholek;
  77.         pom->nr = i;
  78.         G.tab[i] = pom;
  79.     }
  80.     return we;
  81. }
  82.  
  83.  
  84. int main(){
  85.     graf G;
  86.     cin>>G;
  87.     G.dodajPol(1, 2);
  88.     cout<<G;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement