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

📄 lwg.h

📁 data structures, algorithms and Application书的源代码
💻 H
字号:
// file lwg.h
// linked weighted graphs
// final version

#ifndef LinkedWGraph_
#define LinkedWGraph_

#include "lwdg.h"
#include "undirect.h"
#include "unetwork.h"
#include "xcept.h"

template<class T>
class LinkedWGraph : public LinkedWDigraph<T>,
                     virtual public Undirected,
		     virtual public UNetwork<T> {
   public:
      LinkedWGraph(int Vertices = 10)
        : LinkedWDigraph<T> (Vertices) {}
      LinkedWGraph<T>& Add(int i, int j, const T& w);
      LinkedWGraph<T>& Delete(int i, int j);
      int Degree(int i) const {return InDegree(i);}
      int OutDegree(int i) const {return InDegree(i);}
   protected:
      LinkedWGraph<T>& AddNoCheck(int i, int j, const T& w);
};

template<class T>
LinkedWGraph<T>& LinkedWGraph<T>
           ::Add(int i, int j, const T& w)
{// Add edge (i,j).
   if (i < 1 || j < 1 || i > n || j > n || i == j
       || Exist(i, j)) throw BadInput();
   return AddNoCheck(i, j, w);
}

template<class T>
LinkedWGraph<T>& LinkedWGraph<T>
          ::AddNoCheck(int i, int j, const T& w)
{// Add edge (i,j).
   GraphNode<T> x;
   x.vertex = j; x.weight = w;
   h[i].Insert(0,x); // put on i's list
   x.vertex = i;
   try {h[j].Insert(0,x);}
   catch (...) // insert failed
      {// undo previous insert
       x.vertex = j; h[i].Delete(x);
       throw;} // throw same exception
   e++;
   return *this;
}

template<class T>
LinkedWGraph<T>& LinkedWGraph<T>
                 ::Delete(int i, int j)
{// Delete edge (i,j).
   LinkedWDigraph<T>::Delete(i,j);
   LinkedWDigraph<T>::Delete(j,i);
   e++; // compensate
   return *this;
}

#endif

⌨️ 快捷键说明

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