Graph Adjacency List using Link list

#include<iostream.h>
#include<conio.h>

struct Adj_list
{

int dest;
struct Adj_list *next;

};
struct Adjlist
{
struct Adj_list *head;

};


class Graph
{
int v;
int i;
public:
struct Adjlist *array;

Graph(int v)
{
this->v=v;
array=new Adjlist[v];
array[i].head=NULL;
}

Adj_list *newAdj_list(int dest)
{

Adj_list *newnode =new Adj_list;
newnode->dest=dest;
newnode->next=NULL;
return newnode;
}



void addedge(int src,int dest)
{

Adj_list *newnode=newAdj_list(dest);

newnode->next=array[src].head;
array[src].head=newnode;
newnode=newAdj_list(src);
newnode->next=array[dest].head;
array[dest].head=newnode;
}

void display()
{

int i;
for(i=0; i< v; i++)
{
Adj_list *pcrawl=array[i].head;
cout<<" \n Adjancy List of vertex  "<<i<<" head";

while(pcrawl)
{
cout<<" -> "<<pcrawl->dest;
pcrawl=pcrawl->next;

}
cout<<endl;
}



}
};
void main()
{
clrscr();
Graph obj(5);
obj.addedge(0,1);
obj.addedge(0,4);
obj.addedge(1,2);
obj.addedge(1,3);
obj.addedge(1,4);
obj.addedge(2,3);
obj.addedge(3,4);

obj.display();
getch();
}

No comments

Powered by Blogger.