Advertisement
shabbyheart

Untitled

Oct 27th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. /*
  6.  * Adjacency List Node
  7.  */
  8. struct AdjListNode
  9. {
  10.     int data;
  11.     struct AdjListNode* next;
  12. };
  13.  
  14. /*
  15.  * Adjacency List
  16.  */
  17. struct AdjList
  18. {
  19.     struct AdjListNode *head;
  20. };
  21.  
  22. /*
  23.  * Class Graph
  24.  */
  25. class Graph
  26. {
  27.     private:
  28.         int V;
  29.         AdjList* array;
  30.     public:
  31.         Graph(int V)
  32.         {
  33.             this->V = V;
  34.             array = new AdjList [V];         //total vertices
  35.             for (int i = 0; i < V; ++i)
  36.                 array[i].head = NULL;       //linking head of all vertices (array) to NULL ,it doesn't store any number only stores HEAD
  37.         }
  38.  
  39.         /*
  40.          * Adding Edge to Graph
  41.          */
  42.         void addEdge(int src, int dest)
  43.         {
  44.                                                 // 0-->2
  45.                                                 // 1-->NULL
  46.                                                 // 2-->0
  47.  
  48.           // Add an edge from src to dest.  A new node is added to the adjacency
  49.         // list of src.  The node is added at the begining
  50.  
  51.             AdjListNode* newNode = new AdjListNode;  //newNode stores both data(dest) and *next pointer
  52.             newNode->data = dest;                   //consider src = 0 and dest = 1     0<----->1 for undirected graph
  53.             newNode->next = NULL;       //  1----->NULL
  54.                                                     //adding nodes at beginning of each list just like in linked list//
  55.             newNode->next = array[src].head;        //*next(of dst) storing address of head->next node i.e.. 1--->2 (first node from head)
  56.             array[src].head = newNode;              //  0-->1-->2
  57.  
  58.              // Since graph is undirected, add an edge from dest to src also
  59.             newNode = new AdjListNode;               //now newNode storing data(src)
  60.             newNode->data = src;
  61.             newNode->next = NULL;               // 0--->NULL
  62.  
  63.             newNode->next = array[dest].head;   // 0---->NULL (bcuz.. 1-->NULL)
  64.             array[dest].head = newNode;         // 1---->0
  65.         }
  66.         /*
  67.          * Print the graph
  68.          */
  69.         void printGraph()
  70.         {
  71.             int v;
  72.             for (v = 0; v < V; ++v)
  73.             {
  74.                 AdjListNode* tmp = array[v].head;       //tmp has the address of (0,1..)vertex head
  75.                 cout<<"\n Adjacency list of vertex "<<v<<"\n head ";
  76.                 while (tmp)
  77.                 {
  78.                     cout<<"-> "<<tmp->data;
  79.                     tmp = tmp->next;
  80.                 }
  81.                 cout<<endl;
  82.             }
  83.         }
  84. };
  85.  
  86. /*
  87.  * Main
  88.  */
  89. int main()
  90. {
  91.     Graph gh(5);
  92.     gh.addEdge(0, 1);
  93.     gh.addEdge(0, 4);
  94.     gh.addEdge(1, 2);
  95.     gh.addEdge(1, 3);
  96.     gh.addEdge(1, 4);
  97.     gh.addEdge(2, 3);
  98.     gh.addEdge(3, 4);
  99.  
  100.     // print the adjacency list representation of the above graph
  101.     gh.printGraph();
  102.  
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement