Tuesday, 26 August 2014

Adjacency List

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class Graph
{
private:
char name;
Graph *next,*down;
    Graph *root,*ptr;
public:
      Graph()
      {
         root=NULL;  
         ptr=NULL;
      }
 void set()
 {
 Graph *ptr1,*ptr2,*ptr3;
          int total_Graph;
 char vertex;
          cout<<"\nEnter No. of vertices in Graph : ";
          cin>>total_Graph;
          for(int i=0;i<total_Graph;i++)
  {
  cout<<"\nEnter Vertex Symbol: ";
               cin>>vertex;
               ptr1=new Graph;
               ptr1->name=vertex;
               ptr1->next=NULL;
               ptr1->down=NULL;
  if(root==NULL)
  {
                 root = ptr1;
  }
               else
  {
  ptr2=root;
                   while(ptr2->next)
                   ptr2=ptr2->next;
                   ptr2->next=ptr1;
  }
              int edge;
            cout<<"\nEnter edges connected with vertex "<<vertex<<"  :  ";
            cin>>edge;
            for(int j=0;j<edge; j++)
{
char n_edge;
cout<<"\nEnter symbol of edge "<<j+1<<"  :  ";
                cin >>n_edge;
                Graph *temp;
                ptr=ptr1->down;
                temp=new Graph;
                temp->name=n_edge;
                temp->down=NULL;
                if(ptr==NULL)
   {
  ptr1->down=temp;
                   ptr=temp;
   }
   else
   {
  ptr3=ptr;
                   while(ptr3->down)
                     ptr3=ptr3->down;
                     ptr3->down=temp;
    }
}
 }
 }
 void Display()
 {
 Graph *ptr1,*ptr2;
 ptr1=root;
 ptr2=root;
 cout<<"\n\n\t\tHere comes the output for you guys.......\n\n\n";
 while(ptr1!=NULL)
  {
     cout<<ptr1->name<<"->";
     ptr2=ptr1->down;
     while(ptr2!=NULL)
  {
cout<<ptr2->name<<"->";
ptr2=ptr2->down;
  }
  cout<<"NULL"<<endl;
  ptr1=ptr1->next;
}
 }
};
int main()
{
Graph s1;
s1.set();
s1.Display();
_getch();
return 0;
}

No comments:

Post a Comment