#include<iostream>
#include<time.h>
#include<conio.h>
using namespace std;



class adclist
{
	
	struct list
	{

	int data;
     list* next;
	};



	public:
	void makelist();






};

void adclist::makelist()
{

 list** table = new list* [10];
for(int j =0; j < 10 ; j++)
table[j]  = NULL;

for ( int m =0; m < 10; m++)
{

int x;
cout<<"enter your value"<<endl;
cin>>x;
list* newptr = new list;
newptr->data = x;
newptr->next = NULL;

int y =   rand() %9 ;
//.................................................
// WHATS THE MISTAKE IN THIS SEGMENT..........???????????
list* head = new list;
head = table[y] ;
if (head == NULL)
{
	 head->data = x;
	 head->next = NULL;
     table[y] = head;

}
//.................................
else
{

list* temp = table[y];
while( temp->next!= NULL)
{
	temp = temp->next; 	
}
temp->next = newptr;
}

}

int exit;
cout<<"IF  NO  MORE DATA  ...PLEASE PRESS 1 FOR DISPLAYING UR ADJACENCY LSIT"<<endl;
cin>>exit;
 list* temptr;
if ( exit  == 1)
{
	for ( int m =0; m < 10; m++)
	{
		temptr = table[m];
	while( temptr!= NULL) 
	{
		cout<<m<<" -> "<<temptr->data;
		temptr = temptr->next;

	}

	if ( temptr == NULL)
		cout<<endl<<endl;
	}
}




}

int main()
{

adclist l1;

l1.makelist();

getch();
	return 0;
}

if i use table[y] instead of head in the error part of the code segment.. than it works fine. But why it isn't working this way?

Please define "it works fine" and "it isn't working". Without a program description, we have no way of knowing what is correct and what isn't.

i mean if i write the error segment in the following way:
if ( table[y] == NULL )
{
table[y] = newptr;
}

the the adjacency list is printed . but if we don't write in this manner, nothing is printed and the screen remains blank. that means the adjacency list is not being made. I want to know whats the logic behind this? y we cannot use head pointer instead of table[y]?

This article has been dead for over six months. Start a new discussion instead.