Implementation of Graph using Link list #include #include template struct node { T data; node *link; }; template class graph { private: int maxsize; node **first; public: void accept(); void display(); graph(int n) { maxsize=n; first=new node *[maxsize]; } graph(){} }; template void graph::accept() { node *temp,*t; char choice; for(int i=0;i { cout<<"Enter edges for "<<<" Vertex "< t=new node; t->link=NULL; first[i]=t; temp=t; do { int k; cout<<"Enter the Vertex no to which it has to connect :"; cin>>k; if(k { t=new node; t->data=k; t->link=NULL; temp->link=t; temp=temp->link; } else cout<<"Invalid vertex no "< cout<<"Enter another node (Y/N)";
cin>>choice; } while(choice!='n'); node *t1=first[i]; first[i]=first[i]->link; delete(t1); } } template void graph::display() { for(int i=0;i { node *t; t=first[i]; cout<<"Vertex "<<<" is conneted to the vertices "; while(t!=NULL) { coutlink; } cout< } }
void main() { clrscr(); graph g1(5); g1.accept(); g1.display(); getch(); }