954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++

#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;
}
sairakhalid
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 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?

sairakhalid
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

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]?

sairakhalid
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: