Code:
//I found the solution....Thanks for your reply:nice:
#include<malloc.h>
#include<iostream.h>

struct node
{ int vertex;
  struct 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(nodes*(sizeof(struct node)));
  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;
             temp=t1;
             cout<<"\nAny other node(y/n):";
             cin>>ch;
          }
        }
        temp->link=NULL;
  }
  display(a);
}
//There was few silly mistakes in my program.But now it is fully correct....This Program is used for storing graph using Link List..First it asks for the number of vertices you want to store in the graph...then for each vertex(starting with vertex 1) it asks whether it is connected to some other vertex..if yes then it asks for the vertex number...few more corrections need to be done to make it more efficient and able to handle invalid inputs......

Last edited by shabbir; 18Feb2012 at 09:22.. Reason: Code blocks