Tuesday, 17 September 2013

Graph Using List

HTML Online Editor Sample

 

#include<stdio.h>

#include<iostream>
using namespace std;
struct list
{
int v;
list* next;
}*root;
int * temp[10];
int main()
{int v,e;
cout<<"\n enter the no of vertex and edge you want in graph";
cin>>v>>e;
int i,j;
while(e--)
{
  cout<<"\n enter vertices b/w which u want an edge";
  cin>>i>>j;
 
if(temp[i]==NULL)
{
  list* t=new list();
  t->v=j;
  t->next=NULL;
  temp[i]=(int*)t;
}
else
{
  list *t=(list*)temp[i];
  while(t->next!=NULL)
  t=t->next;
  list *p=new list();
  p->v=j;
  p->next=NULL;
  t->next=p;
}
 
}
for(int i=1;i<=v;i++)
  if(temp[i]!=NULL)
  {
    list *t;
    t=(list*)temp[i];
    while(t!=NULL)
   {cout<<"edge between\t "<<i<<"\tand \t"<<t->v<<endl;t=t->next;}
  }
 
int x;
cin>>x;
return 0;
}

No comments:

Post a Comment