Click here to Skip to main content
15,886,629 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include<iostream>
using namespace std;

class Node {
    public:
        int data;
        Node* next;

        Node(int data) {
            this->data=data;
            this->next=NULL;
        }
};

class AdjList{
public:
        Node * head;
        AdjList() {
                head=NULL;
        }

        void add (int data) {
                Node * newNode=new Node(data);
                if(head==NULL) {
                        head=newNode;
                }else {
                        Node* temp=head;
                        while(temp!=NULL) {
                            temp->next=newNode;
                            temp=temp->next;
                        }
                }
        }
};

class Graph{
        int v;
        AdjList* adjList;

        public:
            Graph(int v) {
                this->v=v;
                adjList=new AdjList[v];
            }

            void addEdge(int src, int dest) {
                    adjList[src].add(dest);
            }

        void print(){
            for(int i=0;i<v;i++){
                Node *temp = adjList[i].head;
                cout << i << " -> ";
                while(temp != NULL)
                {
                    cout << temp->data << " ";
                    temp = temp->next;
                }
                cout << endl;
            }
        }
};

int main()
{
	// create the graph given in above fugure
    int V = 5;
	Graph *graph = new Graph(V);
	graph->addEdge(0,1);
    graph->addEdge(0, 4);
    graph->addEdge(1, 2);
    graph->addEdge(1, 3);
    graph->addEdge(1, 4);
    graph->addEdge(2, 3);
    graph->addEdge(3, 4);

    // print the adjacency list representation of the above graph
    graph->print();

    //return 0;
}


What I have tried:

I am facing problem while implementing graph without using any library. when i use two three classes for data structure i having issue. please anyone help me with that
Posted
Updated 9-Oct-18 2:52am
v2
Comments
ZurdoDev 5-Oct-18 9:48am    
What is your question?
Rick York 5-Oct-18 10:55am    
Two things will help you get an answer to your question : describe the issue you are having. Do you expect people to guess or to analyze your code and figure it out? The second is do NOT use text speak. If you expect people to spend their time to help you then at least spell out the word "please." It's only six letters.
aadit sah 6-Oct-18 16:31pm    
sorry sir. I am new here I am having the issue in print function in the graph class that i post above
Richard MacCutchan 7-Oct-18 3:18am    
What issue? Did you actually read what Rick York wrote above?

1 solution

Normally is it common style in C++ that code gots divided and so every class has its own header and cpp file. That improves readability.

When you need to implement a feature, you must write a function. Starting with an empty function body you must fill it with code which does the job.

Like for detecting a cycle you must implement some algorithm like in this video explained.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900