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

making linked list

i want to know whats the error over here. i just want to store the three value in a node and making a linked list but after storing two nodes that is headptr and than temptr, when the third node is made it stops.
I think my error is somewhere in "while loop" bt i couldnt find it.

node* addnode(int x, int r,int c)// x = value,r = row, c = coloumn
	
	{
	
		node* nodeptr = new node;
		node* temptr;
		temptr = headptr;
		nodeptr->val = x;;
		nodeptr->rowval = r;
		nodeptr->colval = c;
	    nodeptr->next = NULL;
		
		cout<<"nodeprtr->value"<<nodeptr->val<<endl;
		cout<<"nodeptr->row"<<nodeptr->rowval<<endl;
		cout<<"nodeptr->col"<<nodeptr->colval<<endl;
		if ( headptr == NULL )
		{
			headptr = nodeptr;
			cout<<"headptr->value"<<" "<<headptr->val<<endl;
			cout<<"headptr->row"<<" "<<headptr->rowval<<endl;
		    cout<<"headptr->col"<<" "<<headptr->colval<<endl;
			return headptr;
			
		}
		else
		{   
			 if (headptr != NULL )
			{
				 while(temptr->next!= NULL )
				{
					temptr->next = temptr;
				}
				 
			 }
			temptr->next = nodeptr;
			temptr = nodeptr;
			cout<<"temptr->val"<<" "<<temptr->val<<endl;
		cout<<"temptr->rowval"<<" "<<temptr->rowval<<endl;
		cout<<"temptr->colval"<<" "<<temptr->colval<<endl;
		return nodeptr;
		}
	}
sairakhalid
Newbie Poster
12 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Your statement

temptr->next = temptr;


should probably be

temptr = temptr->next;


There may be other problems, but this is a good place to start.

That said, I would like to suggest that you learn how to use code tags properly.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

thanks it was the mistake. the code is running correctly

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

This question has already been solved

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