⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 equiv.cpp

📁 数据结构算法与应用 --C++语言描述 所有源代码
💻 CPP
字号:
// offline equivalence classes

#include <iostream.h>
#include <stdlib.h>
#include "chain.h"
#include "citer.h"
#include "lstack.h"

void main(void)
{// Offline equivalenece classes.
   int n, r;

   // input n and r
   cout << "Enter number of elements" << endl;
   cin >> n;
   if (n < 2) {cerr << "Too few elements" << endl;
               exit(1);}
   cout << "Enter number of relations" << endl;
   cin >> r;
   if (r < 1) {cerr << "Too few relations" << endl;
               exit(1);}

   // create an array of n chains
   Chain<int> *chain;
   try {chain = new Chain<int> [n+1];}
   catch (NoMem) {cerr << "Out of memory" << endl;
                  exit(1);}
              
   // input the r relations and put on chains
   for (int i = 1; i <= r; i++) {
      cout << "Enter next relation/pair" << endl;
      int a, b;
      cin >> a >> b;
      chain[a].Insert(0,b);
      chain[b].Insert(0,a);
      }
   
   // initialize to output classes
   LinkedStack<int> stack;
   bool *out;
   try {out = new bool [n+1];}
   catch (NoMem) {cerr << "Out of memory" << endl;
                  exit(1);}
   for (int i = 1; i <= n; i++)
      out[i] = false;
   
   // output classes
   for (int i = 1; i <= n; i++)
     if (!out[i]) {// start of a new class
        cout << "Next class is: " << i << ' ';
        out[i] = true;
        stack.Add(i);
        // get rest of class from stack
        while (!stack.IsEmpty()) {
           int *q, j;
           stack.Delete(j);

           // elements on chain[j] are in
           // same class, use iterator c
           // to get them
           ChainIterator<int> c;
           q = c.Initialize(chain[j]);
           while (q) {// q is in same class
              if (!out[*q]) {
                    cout << *q << ' ';
                    out[*q] = true;
                    stack.Add(*q);}
              q = c.Next();
              }
           }
        cout << endl;
        }

   cout << endl << "End of class list" << endl;
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -