Thread: Operator overloading Polynomials Doubly linked list

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Operator overloading Polynomials Doubly linked list

    Hi , I posted previously this problem, but dint get help , so i m posting it again , hoping i will be helped.
    I have created a doubly linked list of polynomials. Now I m done with the basics.
    I want to overload + operator.
    It add two polynomials list , the first element of the first list searches for all the nodes with the same exponent in the second list. if it finds it , its add them together and insert them in a new list.And else if the exponent is nt matched it inserts both the first and second list nodes in the third list.
    Now the problem is with the repetiton of nodes, like in the first iteration 2x^1 and 3x^1
    are matched and added but then when the first list searchs for the node of x^2 since 3x^1 doesnt match with it. it inserts it. although it has been added previously and shouldnt be in the list.
    I want to delete the nodes when i add them together but having problems with the implementation. Can anybuddy help. Its urgent!!
    Code:
    list operator + (const list& k)
    	{
    		node* first;
    		first = head;
    		node* f;
    		f = head;
    		node* second;
    		second = k.head;
    		node* s;
    		s = k.head;
    		node* result=head;
    		list r;
    		
    		while (first != NULL)
    		{
    			second=k.head;
    			while (second != NULL)
    			{
    				if (first->exponent == second->exponent )
    				{
    					
    					result->coefficient	=first->coefficient + second->coefficient;
    					result->exponent=first->exponent;
    					r.insert(result->coefficient,result->exponent);		
    					f=first->previous;
    					f->next=first->next;
    					first->next->previous=f;
    					delete first;
    				//	first=f;
    					s=second->previous;
    					s->next=second->next;
    					second->next->previous=s;
    					delete second;
    					//second=s;
    				}
    				else
    				{
     
    					r.insert(second->coefficient, second->exponent);
    					r.insert(first->coefficient, first->exponent);
     
    				}
    					
    					
    				second=second->next;
    			}
    			
    			first=first->next;
    		}
    		
    		return r;
    	}

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    30
    You do not have to delete any elements from any lists. You just have to advance the head pointers, isn't it. Whenever an element has been processed, whether it's in the first or second list, just advance its corresponding head pointer once, problem solved.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I haven't gone thoroughly into your code. But you delete second in the while loop. Then outside you do second = second->next; That seems like a bug. If the element second is pointing is deleted, then second->next points at nowhere. Thus second=second->next is bad.
    It would make sense if you uncommented the //second = s; line.
    Note that then you could do
    Code:
    second->next->previous = second->previous;
    second->previous->next = second->next;
    second = second->next; //do this inside the if rathern than outside
    delete second;
    and not need the pointer s
    (the same is for first and f of course)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 08:54 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM