How can i make addition in my linked lists if the nodes are stroing two values:
1) int element
2) int colomnvalue
i have to make the addition on the basis that the nodes having the same colomvalue, thier element should be added.
This addition is to be made in linked lists either by creating new linked lists or other way.. i am trying to make the logic but it stops on one or other way.

Recommended Answers

All 2 Replies

void addlist(node* headptr,int c)
{
	
	cout<<"headptr value after being passed"<<headptr->val<<endl;
			node* temptr11 = new node;
			node* temptr1 = headptr;
			cout<<"temptr val after being assigned to headptr"<<" "<<temptr1->val<<endl;
			
			for ( int v = 0; v < c; v++ )
			{
			
			node* nodeptr1 = new node;
                   
		    nodeptr1->colval = v;
			cout<<"nodeptr1 colvalue in addlist"<<" "<<nodeptr1->colval<<endl;

		           
		     
		 if( temptr1!= NULL )
		 
while(temptr1 != NULL )
		 {
			 
			if ( nodeptr1->colval == temptr1->colval)
			{
				cout<<"afetr entering in if consition nodeptr1 colvalue"<<" "<<nodeptr1->colval<<" and temptr1 value"<<" "<<temptr1->colval<<endl;
				nodeptr1->val += temptr1->val;
			}
			else
				temptr1= temptr1->next;
		}
		if ( headptr1 == NULL )
		{
			headptr1 = nodeptr1;
		}
		else
		{
			if ( headptr1 != NULL )
			{
			while(temptr11->next != NULL )
			{
				temptr11 = temptr11->next;
			}
			}
		}
		 
   }
}

whats the error in this code

I don't know what you are trying to do in your code? First, do you completely understand the problem before you attempt to implement it? Second, do you understand how a linked list is created & maintained? Last, your variable names are not meaningful; as a result, it is very difficult to follow.

I am think that the problem asking you to create a linked list, and use the incoming integer value to do so. One way to do is to iterate from front to back, and the other way is to iterate from back to front. The back to front is easier to handle in my opinion.

/*
When add a list, the first round would be...
head = null
int = 15

result of front to back:
  1 -> 5 -> null
  ^
 head

result of back to front:
  5 -> 1 -> null
  ^
 head

Then another addList is called with integer 345
head is not null
int = 345

result of front to back:
  3 -> 6 -> 0 -> null
  ^
 head

result of back to front:
  0 -> 6 -> 3 -> null
  ^
 head


When do the adding, read each value from the list and do addition with the
corresponding column of the incoming integer. Also, you need a carry flag
in order to deal with addition carry value.
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.