Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "QueType.h"
  2. template<class VertexType>
  3. GraphType<VertexType>::GraphType()
  4. // Post: Arrays of size 50 are dynamically allocated for
  5. //       marks and vertices.  numVertices is set to 0;
  6. //       maxVertices is set to 50.
  7. {
  8.     numVertices = 0;
  9.     maxVertices = 50;
  10.     vertices = new VertexType[50];
  11.     marks = new bool[50];
  12. }
  13.  
  14. template<class VertexType>
  15. GraphType<VertexType>::GraphType(int maxV)
  16. // Post: Arrays of size maxV are dynamically allocated for
  17. //       marks and vertices.
  18. //       numVertices is set to 0; maxVertices is set to maxV.
  19. {
  20.     numVertices = 0;
  21.     maxVertices = maxV;
  22.     vertices = new VertexType[maxV];
  23.     marks = new bool[maxV];
  24. }
  25.  
  26. template<class VertexType>
  27. // Post: arrays for vertices and marks have been deallocated.
  28. GraphType<VertexType>::~GraphType()
  29. {
  30.     delete[] vertices;
  31.     delete[] marks;
  32. }
  33. const int nullptr_EDGE = 0;
  34.  
  35. template<class VertexType>
  36. void GraphType<VertexType>::AddVertex(VertexType vertex)
  37. // Post: vertex has been stored in vertices.
  38. //       Corresponding row and column of edges has been set
  39. //       to nullptr_EDGE.
  40. //       numVertices has been incremented.
  41. {
  42.     vertices[numVertices] = vertex;
  43.  
  44.     for (int index = 0; index < numVertices; index++)
  45.     {
  46.         edges[numVertices][index] = nullptr_EDGE;
  47.         edges[index][numVertices] = nullptr_EDGE;
  48.     }
  49.     numVertices++;
  50. }
  51. template<class VertexType>
  52. int IndexIs(VertexType* vertices, VertexType vertex)
  53. // Post: Returns the index of vertex in vertices.
  54. {
  55.     int index = 0;
  56.  
  57.     while (!(vertex == vertices[index]))
  58.         index++;
  59.     return index;
  60. }
  61.  
  62. template<class VertexType>
  63. void GraphType<VertexType>::AddEdge(VertexType fromVertex,
  64.     VertexType toVertex, int weight)
  65.     // Post: Edge (fromVertex, toVertex) is stored in edges.
  66. {
  67.     int row;
  68.     int col;
  69.  
  70.     row = IndexIs(vertices, fromVertex);
  71.     col = IndexIs(vertices, toVertex);
  72.  
  73.     template<class VertexType>
  74.     // Assumption: VertexType is a type for which the "=",
  75.     // "==", and "<<" operators are defined
  76.     class GraphType
  77.     {
  78.     public:
  79.         GraphType();                  // Default of 50 vertices
  80.         GraphType(int maxV);          // maxV <= 50
  81.         ~GraphType();
  82.         //  void MakeEmpty();
  83.         //  bool IsEmpty() const;
  84.         //  bool IsFull() const;
  85.         void AddVertex(VertexType);
  86.         void AddEdge(VertexType, VertexType, int);
  87.         int WeightIs(VertexType, VertexType);
  88.         void GetToVertices(VertexType, QueType<VertexType>&);
  89.         bool EdgeExists(VertexType, VertexType);
  90.         void DeleteEdge(VertexType, VertexType);
  91.         //  void ClearMarks();
  92.         //  void MarkVertex(VertexType);
  93.         //  bool IsMarked(VertexType);
  94.     private:
  95.         int numVertices;
  96.         int maxVertices;
  97.         VertexType* vertices;
  98.         int edges[50][50];
  99.         bool* marks;    // marks[i] is mark for vertices[i].
  100.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement