List of List Program

Discussion in 'C' started by arpit.jh001, Feb 15, 2012.

1. arpit.jh001New Member

Joined:
Feb 15, 2012
Messages:
10
0
Trophy Points:
0
/* In this program I have used an array of pointers and try to link every element of that with the node of same type but can't able to get the desired output....This program was written with the intention of storing a graph without using array for storing adjacency matrix.......but cant find a proper solution :nonod:..kindly help*/
Code:
```#include<malloc.h>
#include<iostream.h>

struct node
{ int vertex;

};

int nodes;

void display(struct node *a)
{ struct node *temp;
for(int i=0;i<nodes;i++)
{ cout<<a[i].vertex;
temp = &a[i];
cout<<"->"<<temp->vertex;
}
cout<<"\n";
}
}
void main()
{
char ch;
struct node *temp,*t1;
cout<<"\n Enter the number of vertices : ";
cin>>nodes;
node *a= (struct node*)malloc((sizeof(struct node))*nodes);
for(int i=1;i<=nodes;i++)
{   a[i-1].vertex = i;
cout<<"Is node "<<i<<" is connected to some other node(y/n):";
cin>>ch;
if((ch == 'y') || (ch == 'Y'))
{
temp = &a[i-1] ;
cout<<"node no."<<i<<" is connected to :";
while(ch=='Y' || ch=='y')
{ t1 = (struct node*)malloc(sizeof(struct node));
cout<<"\nEnter vertex no.";
cin>>t1->vertex;
t1=temp;
cout<<"\nAny other node(y/n):";
cin>>ch;
}
}
}
display(a);
}```

Last edited by a moderator: Feb 15, 2012
2. xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
What input did you give the program?
What output did you get?
What output did you expect?

arpit.jh001 likes this.
3. arpit.jh001New Member

Joined:
Feb 15, 2012
Messages:
10
0
Trophy Points:
0
Code:
```//I found the solution....Thanks for your reply:nice:
#include<malloc.h>
#include<iostream.h>

struct node
{ int vertex;
};

int nodes;

void display(struct node *a)
{ struct node *temp;
for(int i=0;i<nodes;i++)
{ cout<<a[i].vertex;
temp = &a[i];
cout<<"->"<<temp->vertex;
}
cout<<"\n";
}
}
void main()
{
char ch;
struct node *temp,*t1;
cout<<"\n Enter the number of vertices : ";
cin>>nodes;
node *a= (struct node*)malloc(nodes*(sizeof(struct node)));
for(int i=1;i<=nodes;i++)
{   a[i-1].vertex = i;
cout<<"Is node "<<i<<" is connected to some other node(y/n):";
cin>>ch;
if((ch == 'y') || (ch == 'Y'))
{
temp = &a[i-1] ;
cout<<"node no."<<i<<" is connected to :";
while(ch=='Y' || ch=='y')
{ t1 = (struct node*)malloc(sizeof(struct node));
cout<<"\nEnter vertex no.";
cin>>t1->vertex;
temp=t1;
cout<<"\nAny other node(y/n):";
cin>>ch;
}
}
}
display(a);
}```
//There was few silly mistakes in my program.But now it is fully correct....This Program is used for storing graph using Link List..First it asks for the number of vertices you want to store in the graph...then for each vertex(starting with vertex 1) it asks whether it is connected to some other vertex..if yes then it asks for the vertex number...few more corrections need to be done to make it more efficient and able to handle invalid inputs......

Last edited by a moderator: Feb 18, 2012
4. arpit.jh001New Member

Joined:
Feb 15, 2012
Messages:
10
0
Trophy Points:
0
Code:
```//This has more functionalities.....it can search whether two vertices are connected or  not :smug::D
#include<malloc.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>

struct node
{ int vertex;
};

int nodes;

void display(struct node *a)
{ struct node *temp;
for(int i=0;i<nodes;i++)
{ cout<<a[i].vertex;
temp = &a[i];
cout<<"->"<<temp->vertex;
}
cout<<"\n";
}
}
void search_connec(struct node *a ,int x,int y)
{  struct node *temp;
temp = &a[x-1];
if(temp->vertex == y)
{ cout<<"\nVertex "<<x<<" is connected to vertex "<<y<<"\n";
return;
}
}
cout<<"\nNot connected";

}
void main()
{ int x,y,choice;
char ch;
struct node *temp,*t1;
cout<<"\n Enter the number of vertices : ";
cin>>nodes;
node *a= (struct node*)malloc(nodes*(sizeof(struct node)));
for(int i=1;i<=nodes;i++)
{   a[i-1].vertex = i;
cout<<"Is node "<<i<<" is connected to some other node(y/n):";
cin>>ch;
if((ch == 'y') || (ch == 'Y'))
{
temp = &a[i-1] ;
cout<<"node no."<<i<<" is connected to :";
while(ch=='Y' || ch=='y')
{ t1 = (struct node*)malloc(sizeof(struct node));
cout<<"\nEnter vertex no.";
cin>>t1->vertex;
while(!((t1->vertex) <= nodes))
{ cout<<"\nInvalid node! Please re-enter correct node :";
cin>>t1->vertex;
}
temp=t1;
cout<<"\nAny other node(y/n):";
cin>>ch;
}
}
else
}
do{
cout<<"\n -------------------------- \n";
cout<<"\n -------------------------- \n";
cout<<"      1.Display Graph         \n";
cout<<"      2.Search Connection     \n";
cout<<"\n    Enter your choice :       ";
cin>>choice;
switch(choice)
{
case 1: display(a);
break;
case 2: clrscr();
cout<<"\n Enter source vertex :";
cin>>x;
cout<<"\n Enter destination vertex :";
cin>>y;
search_connec(a,x,y);
break;
default : cout<<"\n Invalid choice";
break;
}
}while(choice==1 ||choice==2);
}```

Last edited by a moderator: Feb 18, 2012

Joined:
Aug 6, 2004
Messages:
3,012