Skip to content

Instantly share code, notes, and snippets.

@milon
Created January 19, 2018 01:46
Show Gist options
  • Select an option

  • Save milon/0d4622d710de9ce5c16a3fbed85ad984 to your computer and use it in GitHub Desktop.

Select an option

Save milon/0d4622d710de9ce5c16a3fbed85ad984 to your computer and use it in GitHub Desktop.
Data Structure: Adjacency List
//Adjacency list
//Author: Milon
#include<iostream>
#include<cstdlib>
using namespace std;
struct list{
int val;
struct list *next;
};
struct list head[6];
int main(){
list *ptr,*newnode;
int data[14][2] = {{1,2},{2,1},{1,5},{5,1},{2,3},{3,2},{2,4},
{4,2},{3,4},{4,3},{3,5},{5,3},{4,5},{5,4}};
//Adjacency list creation
cout<<"The adjacent list of the graph : "<<endl;
for(int i=1;i<6;i++){
head[i].val=i;
head[i].next=NULL;
cout<<"Vertix "<<i<<" => ";
ptr=&(head[i]);
for(int j=0;j<14;j++){
if(data[j][0]==i){
newnode=new list();
newnode->val=data[j][1];
newnode->next=NULL;
while(ptr!=NULL)
ptr=ptr->next;
ptr=newnode;
cout<<"["<<newnode->val<<"] ";
}
}
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment