#include<iostream>
#include<stdlib.h>
using namespace std;
void make_node(int n);
void add_edges();
void display();
int n;
struct edge;
struct node
{
int data;
struct node *below;
struct edge *adj;
}*start,*temp,*last,*ptrx;
struct edge
{
struct node *parent;
struct edge *link;
}*first,*stop,*ptr;
int main()
{
cout<<"\nEnter the number of nodes:";
cin>>n;
make_node(n);
add_edges();
display();
return 0;
}
void make_node(int n)
{
for(int i=0;i<n;i++)
{
if(start==nullptr)
{
start=new node;
cout<<"\nEnter data in node "<<i+1<<" : ";
cin>>start->data;
start->below=nullptr;
start->adj=nullptr;
last=start;
}
else
{
temp=new node;
cout<<"\nEnter data in node "<<i+1<<" : ";
cin>>temp->data;
last->below=temp;
last=temp;
last->below=nullptr;
last->adj=nullptr;
temp=nullptr;
}
}
}
void add_edges()
{
int x,t;
last=start;
for(int i=0;i<n;i++,last=last->below)
{
cout<<"\nEnter number of edges in node "<<i+1<<" : ";
cin>>x;
ptrx=last;
first=nullptr;
for(int j=0;j<x;j++)
{
temp=start;
if(first==nullptr)
{
first=new edge;
cout<<"\nEnter the adjacent node "<<j+1<<" : ";
cin>>t;
for(int k=1;k<t;k++)
temp=temp->below;
first->parent=temp;
first->link=nullptr;
ptrx->adj=first;
stop=first;
}
else
{
first=new edge;
cout<<"\nEnter the adjacent node "<<j+1<<" : ";
cin>>t;
for(int k=1;k<t;k++)
temp=temp->below;
first->parent=temp;
first->link=nullptr;
stop->link=first;
stop=first;
}
}
}
}
void display()
{
system("cls");
cout<<"\nNode\t\t\tAdjacent node list";
temp=start;
for(int i=0;i<n;i++,temp=temp->below)
{
cout<<"\n"<<temp->data<<"\t\t\t\t";
ptr=temp->adj;
if(ptr!=nullptr)
{
while(ptr->link!=nullptr)
{
ptrx=ptr->parent;
cout<<ptrx->data<<";";
ptr=ptr->link;
}
ptrx=ptr->parent;
cout<<ptrx->data;
}
}
}
#include<stdlib.h>
using namespace std;
void make_node(int n);
void add_edges();
void display();
int n;
struct edge;
struct node
{
int data;
struct node *below;
struct edge *adj;
}*start,*temp,*last,*ptrx;
struct edge
{
struct node *parent;
struct edge *link;
}*first,*stop,*ptr;
int main()
{
cout<<"\nEnter the number of nodes:";
cin>>n;
make_node(n);
add_edges();
display();
return 0;
}
void make_node(int n)
{
for(int i=0;i<n;i++)
{
if(start==nullptr)
{
start=new node;
cout<<"\nEnter data in node "<<i+1<<" : ";
cin>>start->data;
start->below=nullptr;
start->adj=nullptr;
last=start;
}
else
{
temp=new node;
cout<<"\nEnter data in node "<<i+1<<" : ";
cin>>temp->data;
last->below=temp;
last=temp;
last->below=nullptr;
last->adj=nullptr;
temp=nullptr;
}
}
}
void add_edges()
{
int x,t;
last=start;
for(int i=0;i<n;i++,last=last->below)
{
cout<<"\nEnter number of edges in node "<<i+1<<" : ";
cin>>x;
ptrx=last;
first=nullptr;
for(int j=0;j<x;j++)
{
temp=start;
if(first==nullptr)
{
first=new edge;
cout<<"\nEnter the adjacent node "<<j+1<<" : ";
cin>>t;
for(int k=1;k<t;k++)
temp=temp->below;
first->parent=temp;
first->link=nullptr;
ptrx->adj=first;
stop=first;
}
else
{
first=new edge;
cout<<"\nEnter the adjacent node "<<j+1<<" : ";
cin>>t;
for(int k=1;k<t;k++)
temp=temp->below;
first->parent=temp;
first->link=nullptr;
stop->link=first;
stop=first;
}
}
}
}
void display()
{
system("cls");
cout<<"\nNode\t\t\tAdjacent node list";
temp=start;
for(int i=0;i<n;i++,temp=temp->below)
{
cout<<"\n"<<temp->data<<"\t\t\t\t";
ptr=temp->adj;
if(ptr!=nullptr)
{
while(ptr->link!=nullptr)
{
ptrx=ptr->parent;
cout<<ptrx->data<<";";
ptr=ptr->link;
}
ptrx=ptr->parent;
cout<<ptrx->data;
}
}
}
No comments:
Post a Comment