"adjacency list representation of graph" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "adjacency list representation of graph" answered properly. Developers are finding an appropriate answer about adjacency list representation of graph related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like adjacency list representation of graph. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on adjacency list representation of graph.
adjacency list representation of graph
//adjacency list implementation;addedge,check if edge exist,delete an edge,display
/* 0----->1--------
| | |-->4
|->2---->|->3---
*/
#include <iostream>
using namespace std;
#define v 5
class node
{
public:
int data;
node*next;
};
node*adjlist[v];
void init()
{
for(int i=0;i<v;i++)
{
adjlist[i]=NULL;
}
}
void addedge(int str,int en)
{
node*temp=new node;
temp->data=en;
temp->next=adjlist[str];
adjlist[str]=temp;
}
void hasedge(int str,int en)
{
node*temp=new node;
temp=adjlist[str];
while(temp!=NULL)
{
if(temp->data==en)
{
cout<<"yes:";
}
temp=temp->next;
}
}
void remove(int str,int en)
{
if(adjlist[str]==NULL)
{
return;
}
if(adjlist[str]->data==en)
{
node*temp=new node;
temp=adjlist[str];
adjlist[str]=adjlist[str]->next;
delete temp;
}
else
{
node *ptr=new node;
ptr=adjlist[str];
while(ptr->next!=NULL)
{
node*temp=new node;
if(ptr->next->data==en)
{
temp=ptr->next;
ptr->next=ptr->next->next;
delete temp;
break;
}
ptr=ptr->next;
}
}
}
void display()
{
for(int i=0;i<v;i++)
{
node*temp=new node;
cout<<"adjlist[]"<<i<<endl;
temp=adjlist[i];
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
}
int main()
{
init();
addedge(0,1);
addedge(0,2);
addedge(0,3);
addedge(1,3);
addedge(1,4);
addedge(2,3);
addedge(3,4);
display();
hasedge(0,3);
hasedge(0,4);
remove(0,1);
remove(1,4);
display();
return 0;
}
All those coders who are working on the C++ based application and are stuck on adjacency list representation of graph can get a collection of related answers to their query. Programmers need to enter their query on adjacency list representation of graph related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about adjacency list representation of graph for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of adjacency list representation of graph while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "adjacency list representation of graph". Visit this developer's friendly online web community, CodeProZone, and get your queries like adjacency list representation of graph resolved professionally and stay updated to the latest C++ updates.