adjacency list representation of graph

Solutions on MaxInterview for adjacency list representation of graph by the best coders in the world

showing results for - "adjacency list representation of graph"
Guilhem
20 Mar 2019
1//adjacency list implementation;addedge,check if edge exist,delete an edge,display
2/*            0----->1--------
3              |        |     |-->4
4              |->2---->|->3---
5*/
6   
7#include <iostream>
8
9using namespace std;
10#define v 5
11class node
12{
13public:
14    int data;
15    node*next;
16};
17node*adjlist[v];
18void init()
19{
20    for(int i=0;i<v;i++)
21    {
22        adjlist[i]=NULL;
23    }
24}
25void addedge(int str,int en)
26{
27    node*temp=new node;
28    temp->data=en;
29    temp->next=adjlist[str];
30    adjlist[str]=temp;
31}
32void hasedge(int str,int en)
33{
34    node*temp=new node;
35    temp=adjlist[str];
36    while(temp!=NULL)
37    {
38        if(temp->data==en)
39        {
40            cout<<"yes:";
41        }
42        temp=temp->next;
43    }
44
45}
46void remove(int str,int en)
47{
48    if(adjlist[str]==NULL)
49    {
50        return;
51    }
52
53    if(adjlist[str]->data==en)
54    {
55        node*temp=new node;
56        temp=adjlist[str];
57        adjlist[str]=adjlist[str]->next;
58        delete temp;
59    }
60    else
61    {
62        node *ptr=new node;
63        ptr=adjlist[str];
64        while(ptr->next!=NULL)
65        {
66            node*temp=new node;
67            if(ptr->next->data==en)
68            {
69
70                temp=ptr->next;
71                ptr->next=ptr->next->next;
72                delete temp;
73                break;
74            }
75            ptr=ptr->next;
76        }
77    }
78}
79void display()
80{
81    for(int i=0;i<v;i++)
82    {
83        node*temp=new node;
84        cout<<"adjlist[]"<<i<<endl;
85        temp=adjlist[i];
86        while(temp!=NULL)
87        {
88            cout<<temp->data<<" ";
89            temp=temp->next;
90        }
91        cout<<endl;
92    }
93}
94
95int main()
96{
97    init();
98    addedge(0,1);
99    addedge(0,2);
100    addedge(0,3);
101    addedge(1,3);
102    addedge(1,4);
103    addedge(2,3);
104    addedge(3,4);
105    display();
106    hasedge(0,3);
107    hasedge(0,4);
108    remove(0,1);
109    remove(1,4);
110    display();
111    return 0;
112}
113
queries leading to this page
directed graph adjacency listadjacency list representation of graph pythongraph representation with 1 adjacency listgraph using adjacency listadjacency list vs graphimplement graph using adjacency listgraph adjacency lis representationhow to represent directed graph in adjacency listadjacency list representation of graph in pythonadjacency list representation of directed graphgraph representation using adjacency listdesign a graph using adjacency listadjacency list representation of graphrepresent the graph using 3a 1 adjacency matrix 2 adjacency listadjacency listof a graphadjacency list and adjacency matrix representation of graphadjacency list in graphadjacency list graph implementation how to write adjacency list for a graphgraph adjacency list representationadjacency list representation of graph codeadjacency list for representing the graphwhat is adjacency list of a graphadjacency list in graphsadjacency list of a graph in pythonadjacency list of a graphadjacency list representation of graph javaadjacency list of a directed graphcreating graph from adjacency listimplement graph from adjacency listhow to create adjacency list from graphuse an adjacency list to represent the given graph create graph from adjacency listadjacency list directed graphcreate a graph using adjacency listhow do you find the adjacency list on a graph 3fdesign a graph using adjacency list problemcreate a adjacency list graphare adjacency list directed graphcreate the adjacency list of a graphadjacency list graph representationadjacency list representation of graph gfggraph adjacency listadjacency list graph data structureadjacency list of directed graphgraph in adjacency listmplementation of the adjacency list representation of graphs 3aadjacency list of graphhow to represent a graph using adjacency list adjacency list representationof graphgraph with adjacency listadjacency list to represent a graphadjacency list representation of graph