Menu Driven Program For DSA CPP
Menu Driven Program For DSA CPP
#include <iostream>
using namespace std;
struct Node {
int vertex;
Node* next;
};
class Graph
{
private:
int n; //num of vertex
int m;// num of edges
bool directed;
Node* adjlist[MAX];
public:
void createUnDirGraph(int num_v)
{
directed= false;
n=num_v;
m=0;
for(int i = 0; i<n; i++)
{
adjlist[i]=NULL;
}
}
if (adjlist[v1] == NULL) {
adjlist[v1] = v2node;
} else {
Node* temp = adjlist[v1];
while (temp->next != NULL)
temp = temp->next;
temp->next = v2node;
}
if (!directed) {
Node* v1node = new Node();
v1node->vertex = v1;
v1node->next = NULL;
if (adjlist[v2] == NULL) {
adjlist[v2] = v1node;
} else {
Node* temp = adjlist[v2];
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = v1node;
}
}
}
void printGraph() {
for (int i = 0; i < n; i++) {
cout << i << " -> ";
Node* temp = adjlist[i];
while (temp != NULL) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
}
cout << "Neighbours of vertex " << vertex << ": ";
Node* temp = adjlist[vertex]; //0
while (temp != NULL) {
cout << temp->vertex << " ";
temp = temp->next;
}
cout << endl;
}
void checkUniversalSink()
{
for (int i = 0; i < n; i++) {
if (inDegree(i) == n - 1 && outDegree(i) == 0) {
cout << "Vertex " << i << " is a universal sink." << endl;
return;
}
}
cout << "There is no universal sink." << endl;
}
void checkUniversalSource() {
for (int i = 0; i < n; i++) {
if (outDegree(i) == n - 1 && inDegree(i) == 0) {
cout << "Vertex " << i << " is a universal source." << endl;
return;
}
}
cout << "There is no universal source." << endl;
}
};
int main() {
int choice, num_v;
Graph g;
do {
cout << "1. Create an empty undirected graph" << endl;
cout << "2. Create an empty directed graph" << endl;
cout << "3. Insert an edge" << endl;
cout << "4. Print graph" << endl;
cout << "5. Find neighbours of a vertex" << endl;
cout << "6. Find incoming neighbours of a vertex" << endl;
cout << "7. Find outgoing neighbours of a vertex" << endl;
cout << "8. Find degree of a vertex" << endl;
cout << "9. Find in-degree of a vertex" << endl;
cout << "10. Find out-degree of a vertex" << endl;
cout << "11. Check if a vertex is isolated" << endl;
cout << "12. Check if a vertex is pendant" << endl;
cout << "13. Check if there is a universal sink" << endl;
cout << "14. Check if there is a universal source" << endl;
cout << "-1 to exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter number of vertices: ";
cin >> num_v;
g.createUnDirGraph(num_v);
cout << "Undirected graph created with " << num_v << " vertices." << endl;
break;
case 2:
cout << "Enter number of vertices: ";
cin >> num_v;
g.createDirGraph(num_v);
return 0;
}
Pseudo Code
createDirGraph()
directed=true;
n=numofvertex;
for i<n ;
adjlist[i]=NULL;
// Create new node for vertex2 and link it to vertex1's adjacency list
newNode1 = new Node
newNode1->vertex = vertex2
newNode1->next = adjlist[vertex1]
adjlist[vertex1] = newNode1
Outputs
TestCase 1
TESTCASE 2
TESTCASE 3
TESTCASE 4