/* In this program I have used an array of pointers and try to link every element of that with the node of same type but can't able to get the desired output....This program was written with the intention of storing a graph without using array for storing adjacency matrix.......but cant find a proper solution ..kindly help*/
Code:
#include<malloc.h>
#include<iostream.h>

struct node
{ int vertex;
  node *link;

};

int nodes;

void display(struct node *a)
{ struct node *temp;
  for(int i=0;i<nodes;i++)
  { cout<<a[i].vertex;
     temp = &a[i];
     while(temp->link!=NULL)
     { temp = temp->link;
        cout<<"->"<<temp->vertex;
     }
    cout<<"\n";
  }
}
void main()
{
  char ch;
  struct node *temp,*t1;
  cout<<"\n Enter the number of vertices : ";
  cin>>nodes;
  node *a= (struct node*)malloc((sizeof(struct node))*nodes);
  for(int i=1;i<=nodes;i++)
  {   a[i-1].vertex = i;
        cout<<"Is node "<<i<<" is connected to some other node(y/n):";
        cin>>ch;
        if((ch == 'y') || (ch == 'Y'))
        {
          temp = &a[i-1] ;
          cout<<"node no."<<i<<" is connected to :";
          while(ch=='Y' || ch=='y')
          { t1 = (struct node*)malloc(sizeof(struct node));
             cout<<"\nEnter vertex no.";
             cin>>t1->vertex;
             temp->link = t1;
             t1=temp;
             cout<<"\nAny other node(y/n):";
             cin>>ch;
          }
          t1->link=NULL;
        }
  }
  display(a);
}

Last edited by shabbir; 15Feb2012 at 15:32.. Reason: Code blocks