C++ Program to Implement Adjacency List

This C++ Program demonstrates the implementation of Adjacency List.

Here is source code of the C++ Program to demonstrate the implementation of Adjacency List. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Adjacency List
  3.  */
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. /*
  9.  * Adjacency List Node
  10.  */ 
  11. struct AdjListNode
  12. {
  13.     int dest;
  14.     struct AdjListNode* next;
  15. };
  16.  
  17. /*
  18.  * Adjacency List
  19.  */  
  20. struct AdjList
  21. {
  22.     struct AdjListNode *head;
  23. };
  24.  
  25. /*
  26.  * Class Graph
  27.  */ 
  28. class Graph
  29. {
  30.     private:
  31.         int V;
  32.         struct AdjList* array;
  33.     public:
  34.         Graph(int V)
  35.         {
  36.             this->V = V;
  37.             array = new AdjList [V];
  38.             for (int i = 0; i < V; ++i)
  39.                 array[i].head = NULL;
  40.         }
  41.         /*
  42.          * Creating New Adjacency List Node
  43.          */ 
  44.         AdjListNode* newAdjListNode(int dest)
  45.         {
  46.             AdjListNode* newNode = new AdjListNode;
  47.             newNode->dest = dest;
  48.             newNode->next = NULL;
  49.             return newNode;
  50.         }
  51.         /*
  52.          * Adding Edge to Graph
  53.          */ 
  54.         void addEdge(int src, int dest)
  55.         {
  56.             AdjListNode* newNode = newAdjListNode(dest);
  57.             newNode->next = array[src].head;
  58.             array[src].head = newNode;
  59.             newNode = newAdjListNode(src);
  60.             newNode->next = array[dest].head;
  61.             array[dest].head = newNode;
  62.         }
  63.         /*
  64.          * Print the graph
  65.          */ 
  66.         void printGraph()
  67.         {
  68.             int v;
  69.             for (v = 0; v < V; ++v)
  70.             {
  71.                 AdjListNode* pCrawl = array[v].head;
  72.                 cout<<"\n Adjacency list of vertex "<<v<<"\n head ";
  73.                 while (pCrawl)
  74.                 {
  75.                     cout<<"-> "<<pCrawl->dest;
  76.                     pCrawl = pCrawl->next;
  77.                 }
  78.                 cout<<endl;
  79.             }
  80.         }
  81. };
  82.  
  83. /*
  84.  * Main
  85.  */ 
  86. int main()
  87. {
  88.     Graph gh(5);
  89.     gh.addEdge(0, 1);
  90.     gh.addEdge(0, 4);
  91.     gh.addEdge(1, 2);
  92.     gh.addEdge(1, 3);
  93.     gh.addEdge(1, 4);
  94.     gh.addEdge(2, 3);
  95.     gh.addEdge(3, 4);
  96.  
  97.     // print the adjacency list representation of the above graph
  98.     gh.printGraph();
  99.  
  100.     return 0;
  101. }

$ g++ adjacency_list.cpp
$ a.out
 
 Adjacency list of vertex 0
 head -> 4-> 1
 
 Adjacency list of vertex 1
 head -> 4-> 3-> 2-> 0
 
 Adjacency list of vertex 2
 head -> 3-> 1
 
 Adjacency list of vertex 3
 head -> 4-> 2-> 1
 
 Adjacency list of vertex 4
 head -> 3-> 1-> 0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.